Skip to content

Instantly share code, notes, and snippets.

@jonatanklosko
Created December 18, 2021 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonatanklosko/7f2e7f3d6ca6fc2a7641999c59638262 to your computer and use it in GitHub Desktop.
Save jonatanklosko/7f2e7f3d6ca6fc2a7641999c59638262 to your computer and use it in GitHub Desktop.

AoC 17

Setup

Mix.install([
  {:kino, "~> 0.4.1"},
  {:vega_lite, "~> 0.1.1"}
])
alias VegaLite, as: Vl

Scene

Here's the static scene

position = %{x: 0, y: 0}
{area_x1, area_x2, area_y1, area_y2} = {150, 180, -150, -180}

span =
  [area_x1, area_x2, area_y1, area_y2]
  |> Enum.map(&abs/1)
  |> Enum.max()
  |> Kernel.*(1.5)

graph =
  Vl.new(width: 600, height: 600)
  |> Vl.data_from_values([position])
  |> Vl.layers([
    Vl.new()
    |> Vl.mark(:circle, size: 200, fill: "black")
    |> Vl.encode_field(:x, "x",
      type: :quantitative,
      scale: [domain: [-span, span]],
      axis: [orient: "top", title: "x"]
    )
    |> Vl.encode_field(:y, "y",
      type: :quantitative,
      scale: [domain: [-span, span]],
      axis: [title: "y"]
    ),
    Vl.new()
    |> Vl.data_from_values([%{x1: area_x1, x2: area_x2, y1: area_y1, y2: area_y2}])
    |> Vl.mark(:rect, fill: nil, stroke: "tomato", stroke_width: 2)
    |> Vl.encode_field(:x, "x1", type: :quantitative)
    |> Vl.encode_field(:x2, "x2")
    |> Vl.encode_field(:y, "y1", type: :quantitative)
    |> Vl.encode_field(:y2, "y2")
  ])
  |> Vl.resolve(:scale, x: :shared, y: :shared)

Animation

widget = Kino.VegaLite.new(graph)
Kino.VegaLite.clear(widget)

for i <- 1..10 do
  Kino.VegaLite.push(widget, %{x: 20 * i, y: -20 * i})
  Process.sleep(250)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment