Skip to content

Commit

Permalink
add chained moment seed
Browse files Browse the repository at this point in the history
  • Loading branch information
jraregris committed Jul 9, 2021
1 parent afca451 commit 74b86ad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/petrix_web/live/page_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule PetrixWeb.PageLive do

@impl true
def mount(_params, _session, socket) do
seed_algo = :chain
seed_algo = :chain_moment

if connected?(socket) do
Process.send_after(self(), :tick, @tick)
Expand Down Expand Up @@ -36,6 +36,7 @@ defmodule PetrixWeb.PageLive do
"normal" -> :normal
"random" -> :random
"chain" -> :chain
"chain_moment" -> :chain_moment
end

{:noreply, assign(socket, :seed_algo, value)}
Expand Down Expand Up @@ -65,6 +66,34 @@ defmodule PetrixWeb.PageLive do
nodes
end

defp make_nodes(n, :chain_moment) do
{nodes, _} =
0..n
|> Enum.map_reduce(nil, &make_nodes_chain_moment_reducer/2)

nodes
end

defp make_nodes_chain_moment_reducer(x, acc) do
case {x, acc} do
{_, nil} ->
x = 500
y = 500
{%{x: x, y: y}, %{x: x, y: y, xm: 0, ym: 0}}

{_, prev} ->
range = -100..100
x = prev.x + Enum.random(range) + prev.xm
y = prev.y + Enum.random(range) + prev.ym
node = %{x: x, y: y}

xm = prev.x - x
ym = prev.y - y

{node, %{x: x, y: y, xm: xm, ym: ym}}
end
end

defp make_nodes_reducer(x, acc) do
case {x, acc} do
{_, nil} ->
Expand Down
10 changes: 10 additions & 0 deletions lib/petrix_web/live/page_live.html.leex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@
phx-click="set_seed_algo"
>
<label for="chain" style="background-color: white;">Chained</label>
<br>
<input
type="radio"
id="chain_moment"
name="seed_algo"
value="chain_moment"
<%= if @seed_algo == :chain_moment, do: "checked" %>
phx-click="set_seed_algo"
>
<label for="chain_moment" style="background-color: white;">Chained with moment</label>
<br />
<br />
<button type="button" phx-click="reseed">reseed</button>
Expand Down

0 comments on commit 74b86ad

Please sign in to comment.