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.

t()

A point on a board.

x()

A point's X coordinate.

y()

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

Link to this type

direction()

View Source (since 0.1.0)

Specs

direction() ::
  :north
  | :south
  | :east
  | :west
  | :northwest
  | :northeast
  | :southeast
  | :southwest

A direction from a point toward its adjascent or diagonal neighbor.

Specs

t() :: %Snek.Board.Point{x: x(), y: y()}

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

Link to this function

adjascent_neighbors(origin)

View Source (since 0.1.0)

Specs

adjascent_neighbors(t()) :: [t()]

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}
]
Link to this function

diagonal_neighbors(origin)

View Source (since 0.1.0)

Specs

diagonal_neighbors(t()) :: [t()]

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}
]
Link to this function

difference(point1, point2)

View Source (since 0.1.0)

Specs

difference(t(), t()) :: t()

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}
Link to this function

even?(point)

View Source (since 0.1.0)

Specs

even?(t()) :: boolean()

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
Link to this function

manhattan_distance(point_a, point_b)

View Source (since 0.1.0)

Specs

manhattan_distance(t(), t()) :: integer()

Returns the Manhattan distance between two points.

Examples

iex> Point.manhattan_distance(Point.new(0, 0), Point.new(1, 2))
3

Specs

new(x(), y()) :: t()

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}
Link to this function

rotate_clockwise(point)

View Source (since 0.1.0)

Specs

rotate_clockwise(t()) :: t()

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}
Link to this function

rotate_counterclockwise(point)

View Source (since 0.1.0)

Specs

rotate_counterclockwise(t()) :: t()

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}
Link to this function

step(origin, direction)

View Source (since 0.1.0)

Specs

step(t(), direction()) :: t()

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}
Link to this function

sum(point1, point2)

View Source (since 0.1.0)

Specs

sum(t(), t()) :: t()

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}
Link to this function

zero?(point)

View Source (since 0.1.0)

Specs

zero?(t()) :: boolean()

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