Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,21 @@
defmodule Random do
defp generate_point(0, _, _, set), do: set
defp generate_point(n, f, range, set) do
point = {x,y} = {f.(), f.()}
if x*x + y*y in range and not Set.member?(set, point),
do: generate_point(n-1, f, range, Set.put(set, point)),
else: generate_point(n, f, range, set)
end
def circle do
f = fn -> :rand.uniform(31) - 16 end
points = generate_point(100, f, 10*10..15*15, HashSet.new)
for x <- -15..15 do
for y <- -15..15 do
IO.write if Set.member?(points, {x,y}), do: "x", else: " "
end
IO.puts ""
end
end
end
Random.circle

View file

@ -0,0 +1,17 @@
defmodule Constrain do
defp precalculate(range, condition) do
for x <- range, y <- range, x*x + y*y in condition, do: {x,y}
end
def circle do
range = -15..15
all_points = precalculate(range, 10*10..15*15)
IO.puts length(all_points)
points = Enum.shuffle(all_points) |> Enum.take(100)
Enum.each(-15..15, fn x ->
IO.puts Enum.map(range, fn y -> if Enum.member?(points, {x,y}), do: "o ", else: " " end)
end)
end
end
Constrain.circle

View file

@ -1,30 +0,0 @@
defmodule Random do
def init() do
:random.seed(:erlang.now())
end
def generate_point() do
x = :random.uniform(31) - 16
y = :random.uniform(31) - 16
if 10*10 <= x*x + y*y and x*x + y*y <= 15*15 do
{x, y}
else
generate_point()
end
end
def circle() do
points = for _ <- 1..100, do: generate_point()
for x <- -15..15 do
for y <- -15..15 do
if Enum.member?(points, {x, y}) do
IO.write "x"
else
IO.write " "
end
end
IO.puts ""
end
end
end
Random.init()
Random.circle()