Snek v0.4.0 Snek.Board.Point View Source
A struct for representing points on a board's grid.
Link to this section Summary
Types
A direction from a point toward its adjascent or diagonal neighbor.
A point on a board.
A point's X coordinate.
A point's Y coordinate.
Functions
Returns a list of neighboring points adjascent to a point of origin.
Returns a list of neighboring points diagonal to a point of origin.
Returns the difference between two points, which could be used to find a vector between points, such as when using the neck and head of a snake to determine the point continuing in the last moved direction.
Returns true if and only if this point falls on an even square for an board, alternating like a checkerboard.
Returns the Manhattan distance between two points.
Returns a new point at the given X and Y coordinates.
Rotates a point 90 degrees clockwise.
Rotates a point 90 degrees counter-clockwise.
Returns the point that is one step toward a given direction from a point of origin.
Returns the sum of two points, which could be used to apply a vector point to a fixed points, such as when using the neck and head of a snake to determine the point continuing in the last moved direction.
Returns true if and only if both X and Y are zero, which could be used to determine if a point is a null vector.
Link to this section Types
Specs
direction() :: :north | :south | :east | :west | :northwest | :northeast | :southeast | :southwest
A direction from a point toward its adjascent or diagonal neighbor.
Specs
A point on a board.
May be relative or absolute.
Specs
x() :: integer()
A point's X coordinate.
Smaller values are toward the west side of the board, larger are toward the east.
For absolute coordinates on a board, use an integer between zero and the board width minus one.
For relative points, you may use a negative integer.
Specs
y() :: integer()
A point's Y coordinate.
Smaller values are toward the north side of the board, larger are toward the south.
For absolute coordinates on a board, use an integer between zero and the board height minus one.
For relative points, you may use a negative integer.
Link to this section Functions
Specs
Returns a list of neighboring points adjascent to a point of origin.
Examples
iex> Point.adjascent_neighbors(Point.new(1, 1))
[
%Point{x: 1, y: 0},
%Point{x: 1, y: 2},
%Point{x: 2, y: 1},
%Point{x: 0, y: 1}
]
iex> Point.adjascent_neighbors(Point.new(0, 0))
[
%Point{x: 0, y: -1},
%Point{x: 0, y: 1},
%Point{x: 1, y: 0},
%Point{x: -1, y: 0}
]
Specs
Returns a list of neighboring points diagonal to a point of origin.
Examples
iex> Point.diagonal_neighbors(Point.new(1, 1))
[
%Point{x: 0, y: 0},
%Point{x: 2, y: 0},
%Point{x: 2, y: 2},
%Point{x: 0, y: 2}
]
iex> Point.diagonal_neighbors(Point.new(0, 0))
[
%Point{x: -1, y: -1},
%Point{x: 1, y: -1},
%Point{x: 1, y: 1},
%Point{x: -1, y: 1}
]
Specs
Returns the difference between two points, which could be used to find a vector between points, such as when using the neck and head of a snake to determine the point continuing in the last moved direction.
Examples
iex> Point.difference(Point.new(1, 2), Point.new(1, 3))
%Point{x: 0, y: -1}
iex> Point.difference(Point.new(4, 4), Point.new(5, 4))
%Point{x: -1, y: 0}
Specs
Returns true if and only if this point falls on an even square for an board, alternating like a checkerboard.
Examples
iex> Point.even?(Point.new(0, 0))
true
iex> Point.even?(Point.new(0, 1))
false
iex> Point.even?(Point.new(0, 2))
true
iex> Point.even?(Point.new(1, 0))
false
iex> Point.even?(Point.new(1, 1))
true
iex> Point.even?(Point.new(1, 2))
false
Specs
Returns the Manhattan distance between two points.
Examples
iex> Point.manhattan_distance(Point.new(0, 0), Point.new(1, 2))
3
Specs
Returns a new point at the given X and Y coordinates.
Examples
iex> Point.new(0, 0)
%Point{x: 0, y: 0}
iex> Point.new(3, 1)
%Point{x: 3, y: 1}
iex> Point.new(-2, 0)
%Point{x: -2, y: 0}
Specs
Rotates a point 90 degrees clockwise.
This is useful for rotating vectors, which can help with relative directions
such as :left
and :right
.
Examples
iex> Point.rotate_clockwise(Point.new(0, 1))
%Point{x: -1, y: 0}
Specs
Rotates a point 90 degrees counter-clockwise.
This is useful for rotating vectors, which can help with relative directions
such as :left
and :right
.
Examples
iex> Point.rotate_counterclockwise(Point.new(-1, 0))
%Point{x: 0, y: 1}
Specs
Returns the point that is one step toward a given direction from a point of origin.
Examples
iex> Point.new(5, 5) |> Point.step(:north)
%Point{x: 5, y: 4}
iex> Point.new(5, 5) |> Point.step(:south)
%Point{x: 5, y: 6}
iex> Point.new(5, 5) |> Point.step(:east)
%Point{x: 6, y: 5}
iex> Point.new(5, 5) |> Point.step(:west)
%Point{x: 4, y: 5}
iex> Point.new(5, 5) |> Point.step(:northwest)
%Point{x: 4, y: 4}
iex> Point.new(5, 5) |> Point.step(:northeast)
%Point{x: 6, y: 4}
iex> Point.new(5, 5) |> Point.step(:southeast)
%Point{x: 6, y: 6}
iex> Point.new(5, 5) |> Point.step(:southwest)
%Point{x: 4, y: 6}
Specs
Returns the sum of two points, which could be used to apply a vector point to a fixed points, such as when using the neck and head of a snake to determine the point continuing in the last moved direction.
Examples
iex> Point.sum(Point.new(1, 2), Point.new(1, 0))
%Point{x: 2, y: 2}
iex> Point.sum(Point.new(4, 4), Point.new(-1, 1))
%Point{x: 3, y: 5}
Specs
Returns true if and only if both X and Y are zero, which could be used to determine if a point is a null vector.
Examples
iex> Point.zero?(Point.new(0, 0))
true
iex> Point.zero?(Point.new(0, 1))
false