# Geometry

This page describes a collection of classes specialized for holding data relevant to the geometry of the game world. It contains descriptions of the following types: Direction, Movement, Orientation, View, Vector, Rectangle, CellFace, CellView, LongDirectionMap, and BoolDirectionMap.

## Direction

The class `Direction` represents one of the four cardinal directions. The global values `UP`, `RIGHT`, `DOWN`, and `LEFT` are of this type.

## Movement

The class `Movement` represents a possible movement. In particular, a movement is either a direction or the value `NEUTRAL` which indicates no movement.

### Constructors

Movement.new()
Movement.new(Direction direction)
Constructs either the neutral movement (first overload) or the movement corresponding to a given direction (second overload).

### Member Functions

Direction? as_direction()
Returns the corresponding direction to this movement, unless the movement is neutral, in which case returns `nil`.

## Orientation

The class `Orientation` represents the eight possible ways for a block to be oriented with respect to another. Mathematically speaking, members of this class are elements of the dihedral group of order eight (the set of symmetries of a square). There are eight global values, corresponding to the identity symmetry, three rotations, and four reflections. Possible values are: `ID`, `R1`, `R2`, `R3`, `F0`, `F1`, `F2`, and `F3`.

### Member Functions

Orientation inverse()
Returns the inverse to this orientation.
bool is_rotation()
Returns true if the orientation is a rotation (i.e. one of `ID`, `R1`, `R2`, or `R3`)

### Special Functions

Orientation operator mul(Orientation a, Orientation b)
Returns the composition of two symmetries; in particular, the return value is the result of applying symmetry `b` followed by symmetry `a`.

## View

The class `View` is a light wrapper around an Orientation which is used to represent how one frame of reference relates to another; it is used most prominently to specify how to transform game coordinates to the local coordinates of a block.

### Members

Orientation object_to_view
The transformation taking place to take an object's true coordinates to the viewed coordinates.

### Member Functions

View reverse()
Returns a view that reverses the role of viewer and object. This view instead transforms viewed coordinates to true coordinates.
View transform(Orientation transformation)
Returns a view representing the result of taking the current frame of reference and applying the passed orientation to it; equivalent to multiplying `objectToView` by `transformation.inverse()`.

### Special Functions

View operator call(View)
Direction operator call(Direction)
Vector operator call(Vector)
Given some object relating to the viewed object, returns that object transformed into the view's coordinate system.

## Vector

The class `Vector` represents a two dimensional vector with integer coordinates. The typical coordinate system in the game has its origin in the bottom-left corner, with positive y coordinates facing upwards and positive x coordinates going leftwards.

### Constructors

Vector.new()
Vector.new(Int x, Int y)
Vector.new(Direction)
Vector.new(Movement)
The first constructor gives (0, 0). The second gives (x, y). The third constructor gives a unit vector in the given direction. The last one is like the third, but gives (0, 0) if `NEUTRAL` is passed.

### Members

Int x
The x coordinate of the vector. Can also be accessed as `vector`.
Int y
The y coordinate of the vector. Can also be accessed as `vector`.
String type
Always equal to "Vector"

### Member Functions

Vectord as_vectord()
Returns a floating point vector with the same coordinates.

### Special Functions

Vector operator sub(Vector, Vector)
Vector operator mul(Vector, Int)
Vector operator mul(Int, Vector)
Vector operator unm(Vector)

## Vectord

The class `Vectord` represents a two dimensional vector with floating point coordinates. The typical coordinate system in the game has its origin in the bottom-left corner, with positive y coordinates facing upwards and positive x coordinates going leftwards.

### Constructors

Vectord.new()
Vectord.new(Float x, Float y)
Vectord.new(Direction)
Vectord.new(Movement)
The first constructor gives (0, 0). The second gives (x, y). The third constructor gives a unit vector in the given direction. The last one is like the third, but gives (0, 0) if `NEUTRAL` is passed.

### Members

Float x
The x coordinate of the vector. Can also be accessed as `vector`.
Float y
The y coordinate of the vector. Can also be accessed as `vector`.
String type
Always equal to "Vectord"

### Special Functions

Vectord operator sub(Vectord, Vectord)
Vectord operator mul(Vectord, Float)
Vectord operator mul(Float, Vectord)
Vectord operator unm(Vectord)

## Rectangle

The class `Rectangle` represents a rectangle with integer coordinates.

### Constructor

Rectangle.new(Vector, Vector)
Rectangle.new((Vector, Vector))
Rectangle.new(((Int,Int), (Int,Int)))
Rectangle.new((Int,Int), Vector)
Rectangle.new(Vector, (Int,Int))
Rectangle.new(Int, Int, Int, Int)
Rectangle.new((Int,Int))
Rectangle.new(Int, Int)
Rectangle.new(Vector)
Returns a rectangle spanning from one argument to the other. Pairs of integers are interpreted as vectors when passed to this function. If only a single vector or pair of integers is passed, a rectangle with both `lesser` and `greater` is produced.

This method automatically reconfigures the coordinates so that the coordinates of `lesser` do not exceed those of `greater`.

Rectangle.raw(Vector, Vector)
Rectangle.raw((Vector, Vector))
Rectangle.raw(((Int,Int), (Int,Int)))
Rectangle.raw((Int,Int), Vector)
Rectangle.raw(Vector, (Int,Int))
Rectangle.raw(Int, Int, Int, Int)
Rectangle.raw((Int,Int))
Rectangle.raw(Int, Int)
Rectangle.raw(Vector)
Returns a rectangle spanning from one argument to the other. Pairs of integers are interpreted as vectors when passed to this function. If only a single vector or pair of integers is passed, a rectangle with both `lesser` and `greater` is produced.

The first pair passed to this method will become `lesser` and the second pair will become `greater`. No adjustment is made, even if the coordinates of `lesser` are greater than those of `greater`.

### Members

Vector lesser
The corner of the rectangle with the smaller x and y coordinates.
Vector greater
The corner of the rectangle with the greater x and y coordinates.

### Member Functions

Rectangle translate(Vector amount)
Returns a rectangle that has been translated by `amount`.
Boolean contains(Vector point)
Boolean contains(Rectangle rect)
Tests whether the argument is fully contained within the rectangle.
Vector center()
Returns a center point of the rectangle (rounded if no integer center exists).
Vector size()
Returns the size of the rectangle. Equal to `greater-lesser`.
Vector lower_right()
Returns the lower right corner of the rectangle.
Vector upper_left()
Returns the upper left corner of the rectangle.
Rectangle join(Rectangle other)
Returns the smallest rectangle containing both this and `other`.
Rectangle expand(Int amount)
Grows the rectangle in all directions by the specified amount.
Rectangled as_rectangled()
Returns a rectangle with the same coordinates, but with floating points instead of integers. Note that you may often wish to expand rectangles after converting them since integer rectangles are interpreted to include their extreme points, whereas a floating point rectangles are not.
String type
Always equal to "Rectangle"

## Rectangled

The class `Rectangled` represents a rectangle with floating point coordinates.

### Constructor

Rectangled.new(Vectord, Vectord)
Rectangled.new((Vectord, Vectord))
Rectangled.new(((Float, Float),)
Rectangled.new((Float, Float), Vectord)
Rectangled.new(Vectord, (Float, Float))
Rectangled.new(Float, Float, Float, Float)
Rectangled.new((Float, Float))
Rectangled.new(Float, Float)
Rectangled.new(Vectord)
Returns a rectangle spanning from one argument to the other. Pairs of numbers are interpreted as vectors when passed to this function. If only a single vector or pair of numbers is passed, a rectangle with both `lesser` and `greater` is produced.

This method automatically reconfigures the coordinates so that the coordinates of `lesser` do not exceed those of `greater`.

Rectangled.raw(Vectord, Vectord)
Rectangled.raw((Vectord, Vectord))
Rectangled.raw(((Int,Int), (Int,Int)))
Rectangled.raw((Int,Int), Vectord)
Rectangled.raw(Vectord, (Int,Int))
Rectangled.raw(Int, Int, Int, Int)
Rectangled.raw((Int,Int))
Rectangled.raw(Int, Int)
Rectangled.raw(Vectord)
Returns a rectangle spanning from one argument to the other. Pairs of numbers are interpreted as vectors when passed to this function. If only a single vector or pair of numbers is passed, a rectangle with both `lesser` and `greater` is produced.

The first pair passed to this method will become `lesser` and the second pair will become `greater`. No adjustment is made, even if the coordinates of `lesser` are greater than those of `greater`.

### Members

Vectord lesser
The corner of the rectangle with the smaller x and y coordinates.
Vectord greater
The corner of the rectangle with the greater x and y coordinates.

### Member Functions

Rectangled translate(Vectord amount)
Returns a rectangle that has been translated by `amount`.
Boolean contains(Vectord point)
Boolean contains(Rectangled rect)
Tests whether the argument is fully contained within the rectangle.
Vectord center()
Returns a center point of the rectangle.
Vectord size()
Returns the size of the rectangle. Equal to `greater-lesser`.
Vectord lower_right()
Returns the lower right corner of the rectangle.
Vectord upper_left()
Returns the upper left corner of the rectangle.
Rectangled join(Rectangled other)
Returns the smallest rectangle containing both this and `other`.
Rectangled expand(Float amount)
Grows the rectangle in all directions by the specified amount.
String type
Always equal to "Rectangled"

## CellFace

The class `CellFace` consists of a position along with one direction, representing a face of a cell on the grid.

### Constructor

CellFace.new(Vector position, Direction face)
Returns a CellFace representing the face given by `face` of the cell at `position`
CellFace.new(CellFace)
Copies an instance of CellFace.

### Members

Vector position
The position of the block to which the face belongs.
Direction face
The direction of the face represented from the block's center.

## CellView

The class `CellView` consists of a position along with a view, representing a particular frame of reference centered on a particular cell..

### Constructor

CellView.new(Vector position, View view)
Returns a CellView representing the view of the cell at `position` by the transformation `view`.
CellView.new(CellView)
Copies an instance of CellView.

### Members

Vector position
The position of the viewed cell.
View view
The view represented.

### Member Functions

CellView transform(Orientation transformation)
Returns a CellView representing the view of the cell at `position` by the transformation `view`.
CellFace getFace(Direction direction)
Returns, in global coordinates, the face in the given local direction.

## LongDirectionMap

The class `LongDirectionMap` is a container associating directions to integer values.

### Constructor

LongDirectionMap.new()
LongDirectionMap.new(Int up, Int right, Int down, Int left)
Constructs an object with the given associated values. Arguments default to zero.

### Special

The expressions `map[UP]`, `map[RIGHT]`, `map[DOWN]`, and `map[LEFT]` are integers which may be accessed and assigned.

## BoolDirectionMap

The class `BoolDirectionMap` is a container associating directions to boolean values.

### Constructor

BoolDirectionMap.new()
BoolDirectionMap.new(Boolean up, Boolean right, Boolean down, Boolean left)
Constructs an object with the given associated values. Arguments default to false.

### Special

The expressions `map[UP]`, `map[RIGHT]`, `map[DOWN]`, and `map[LEFT]` are booleans which may be accessed and assigned.

## CoordinateTransform

CoordinateTransform is a type representing symmetries of the grid consisting of a rotation and a translation.

### Constructor

CoordinateTransform.new(Vector offset)
CoordinateTransform.new(Vector offset, Orientation rotation)
CoordinateTransform.new(Orientation rotation)
Constructs a transformation with the specified offset (where the origin vector goes to under transformation) and rotation (how input vectors are transformed prior to translation).

### Members

Vector offset
The translational component of the transformation.
Orientation orientation
The rotational component of the transformation.

### Member Functions

Vector transform(Vector input)
CellFace transform(CellFace input)
Applies the transformation to a given object.
CoordinateTransform inverse()
Returns a coordinate transformation that undoes the current one.

### Special Functions

CoordinateTransform operator mul(CoordinateTransform a, CoordinateTransform b)
Returns the composition of two coordinate transforms, which is the transform obtained by performing `b` then performing `a`.