Table of Contents

Lines

Lines are important in many grid algorithms. Grids 2 uses a very general conception of a line.

Lines in Grids 2 are represented with a map: a two-way function that gives the two points next-in-line to the given point when applied in the forward and reverse directions respectively. We can generate more points from the line by repeatedly applying the function (either forward or in reverse).

For example, the following creates a line that represents the x-axis:

var xAxis = Map.Func<GridPoint2, GridPoint2>(p => p + RectPoint.East, p => p - RectPoint.East);

To generate 10 points in one direction, we can use the following code:

var points = new List<GridPoint2>();
var point = new GridPoint(0, 0);

for(int i = 0; i < 10; i++)
{
    points.Add(point);
    point = xAxis.Forward(point);
}

And to create 10 points in the other direction, we can use the following code:

var points = new List<GridPoint2>();
var point = new GridPoint(0, 0);

for(int i = 0; i < 10; i++)
{
    points.Add(point);
    point = xAxis.Reverse(point);
}

At times we only need to produce a line in one direction. This is called a "ray". For rays, we don't need a full map, we can use either a IForwardMap<TInput, TOutput> or IReverseMap<TInput, TOutput>, and call the rays "forward rays" or "reverse rays" depending on the direction of the one-way maps.

A vector line (or ray) is a map like the one above, where the map simply adds or subtracts a fixed point. These are the lines used most often in grid games. For these lines, we provide utility functions, so you won't need to create them yourself.