Sponsored by AppSignal
Would you like to see your link here? Contact us
Notesclub

Blockr Points

5_blockr_project_canvas.livemd

Blockr Points

import IEx.Helpers

one-point

p =
  Point.new(1, 5)
  |> Point.move_down()
  |> Point.move_down()
  |> Point.move_down()
  |> Point.move_left()
  |> Point.move_right()

Canvas.new(p)

Multiple Point

points =
  [
    Point.new(2, 5),
    Point.new(2, 6),
    Point.new(3, 4),
    Point.new(3, 5)
  ]

Canvas.new(points)
# points = 
#   [
#     (Point.new(2,5)|> Point.move_right()),
#     (Point.new(2,6)|> Point.move_right()),
#     (Point.new(3,4)|> Point.move_right()),
#     (Point.new(3,5)|> Point.move_right()),
#   ]

# points_data = [{2, 5}, {2, 6}, {3, 4}, {3, 5}]

# points = Enum.map(points_data, fn {x, y} ->
#   Point.new(x, y) |> Point.move_right()
# end)

# points =
#   [{2, 5}, {2, 6}, {3, 4}, {3, 5}]
#   |> Enum.map(fn {x, y} -> Point.new(x, y) |> Point.move_right() end)
# Canvas.new(points)

# points =
#   [{2, 5}, {2, 6}, {3, 4}, {3, 5}]
#   |> Enum.map(&Point.new(elem(&1, 0), elem(&1, 1)) |> Point.move_right())
#   |> Canvas.new()

points =
  [
    Point.new(2, 5),
    Point.new(2, 6),
    Point.new(3, 4),
    Point.new(3, 5)
  ]

points
|> Enum.map(&Point.move_right/1)
|> Enum.map(&Point.move_right/1)
|> Enum.map(&Point.move_down/1)
|> Canvas.new()

Big Tetromino

points =
  [
    Point.new(2, 2),
    Point.new(3, 2),
    Point.new(4, 2),
    Point.new(4, 3)
  ]

Canvas.tetromino(points)
defmodule Spinner do
  def swap({row, col}) do
    {col, row}
  end

  def flip_left_right({row, col}) do
    {row, 5 - col}
  end

  def flip_top_bottom({row, col}) do
    {5 - row, col}
  end

  def rotate(point, 0) do
    point
  end

  def rotate(point, 90) do
    point |> swap() |> flip_left_right()
  end

  def rotate(point, 180) do
    point |> flip_top_bottom() |> flip_left_right()
  end

  def rotate(point, 270) do
    point |> swap() |> flip_top_bottom()
  end

  def rotate(point, 360) do
    point
  end
end
{:module, Spinner, <<70, 79, 82, 49, 0, 0, 8, ...>>, {:rotate, 2}}
Canvas.tetromino(points)
points
|> Enum.map(&amp;Spinner.rotate(&amp;1, 90))
|> Canvas.tetromino()
points
|> Enum.map(&amp;Spinner.rotate(&amp;1, 180))
|> Canvas.tetromino()
points
|> Enum.map(&amp;Spinner.rotate(&amp;1, 270))
|> Canvas.tetromino()
nil
points
|> Enum.map(&amp;Spinner.swap/1)
|> Canvas.tetromino()
points
|> Enum.map(&amp;Spinner.flip_left_right/1)
|> Canvas.tetromino()
points
|> Enum.map(&amp;Spinner.flip_top_bottom/1)
|> Canvas.tetromino()

Adding swap and all to Points module

Point.new(1, 2) |> Point.swap()
{2, 1}

Shapes

:i shape

Canvas.tetromino([{1, 2}, {2, 2}, {3, 2}, {4, 2}])

:l shape

Canvas.tetromino([{1, 2}, {2, 2}, {3, 2}, {3, 3}])

:j shape

Canvas.tetromino([{1, 3}, {2, 3}, {3, 2}, {3, 3}])

:o shape

Canvas.tetromino([{2, 2}, {3, 2}, {2, 3}, {3, 3}])

:t shape

Canvas.tetromino([{1, 2}, {2, 2}, {3, 2}, {2, 3}])

:s shape

Canvas.tetromino([{2, 3}, {2, 4}, {3, 2}, {3, 3}])

:z shape

Canvas.tetromino([{2, 2}, {2, 3}, {3, 3}, {3, 4}])
Tetromino.new(:z)
|> Tetromino.to_group()
|> Canvas.tetromino()