Rectilinear Grids

Rectilinear grids are typically defined by regularly spaced horizontal and vertical lines.

There are three important components of the planar grid: the vertices, the edges, and the cell interiors. Sometimes we are interested in the interiors of the cells, as in geometric applications where each cell describes a region in space. Sometimes we are interested in the vertices of the grid, such as in addressing the pieces on a chessboard. Sometimes we are interested in the edges of the grid, such as when finding routes to travel in a city where buildings occupy the interior of the cells.

Vertices in planar grids each touch four edges and the interiors of four cells, except for vertices on the boundaries. Vertices in 3D grids touch on six edges and eight cells. In -dimensions, each vertex touches edges and cells. Cells in a planar grid each touch eight faces, four diagonally through vertices and four through edges. Cells in a 3D grid each touch 26 other cells, sharing a face with 6 of them, an edge with 12 of them, and just a vertex with the other 8.

Traversal

It is often necessary to traverse all the cells of an rectilinear grid. Any such traversal can be thought of as a mapping from each of the ordered pairs to a unique integer from to .

The most important traversal methods are --

• Row Major - Here we slice the matrix between rows, so the first elements visited belong to the first row, the second elements to the second row, and so forth.

• Column Major - Here we slice the matrix between columns, so the first elements belong to the first column, the second elements to the second column, and so forth. This can be done by interchanging the order of the nested loops from row-major ordering. Knowing whether your compiler uses row-major or column-major ordering for matrices is important when optimizing for cache performance and when attempting certain pointer-arithmetic operations.

• Diagonal Order - Here we march up and down diagonals. Note that an grid has diagonals, each with a variable number of elements. This is a trickier task than it appears at first glance.

Dual Graphs and Representations

Two-dimensional arrays are the natural choice to represent planar rectilinear grids. We can let m[i][j] denote either the th vertex or the th face, depending on which we are interested in. The four neighbors of any cell follow by adding to either of the coordinates.

A useful concept in thinking about problems on planar subdivisions is that of the dual graph, which has one vertex for each region in the subdivision, and edges between the vertices of any two regions which are neighbors of each other.

Observe that the dual graphs of both rectangular and hexagonal lattices are slightly smaller rectangular and hexagonal lattices. This is why whatever structure we use to represent vertex connectivities can also be used to represent face connectivities.

An adjacency representation is the natural way to represent an edge-weighted rectilinear grid. This might be most easily done by creating a three-dimensional array m[i][j][d], where ranges over four values (north, east, south, and west) which denote the edge directions from point .

Triangular Lattices

Triangular lattices are constructed from three sets of equally spaced lines, consisting of a horizontal row'' axis, a column'' axis from horizontal, and a diagonal'' axis from horizontal.

Vertices of this lattice are formed by the intersection of three axis lines, so each face of the lattice is an equilateral triangle. Each vertex is connected to six others, those immediately above and below on each of the three axes.

To identify the proper neighbors of each vertex requires keeping track of two types of coordinate systems:

• Triangular/Hexagonal Coordinates - Here, one vertex is designated as the origin of the grid, point . We must assign the coordinates such that the logical neighbors of each vertex are easily obtainable. In a standard rectilinear coordinate system, the four neighbors of follow by adding to either the row or column coordinates.

Although the intersection of three lines defines each grid vertex, in fact the row and column dimensions to specify location.

A vertex lies rows above the origin, and -columns to the right of the origin. The neighbors of a vertex can be found by adding the following pairs to the coordinates of , in counterclockwise order: , , , , , and .

• Geometrical Coordinates - The vertices of a regular triangular grid occur in half-staggered rows.

Assume that each lattice point is a distance from its six nearest neighbors, and that point in triangular coordinates in fact lies at geometric point . Then triangular-coordinate point must lie at geometric point

by simple trigonometry, where and ,

Hexagonal Lattices

Deleting every other vertex from a triangular lattice leaves us with a hexagonal lattice. Now the faces of the lattice are regular hexagons, and each hexagon is adjacent to six other hexagons. The vertices of the lattice now have degree 3, because this lattice is the dual graph of the triangular lattice.

Hexagonal lattices have many interesting and useful properties, primarily because hexagons are rounder'' than squares.

To convert between triangular/hexagonal coordinates and geometrical coordinates, we assume that the origin of both systems is the center of a disk at .

The hexagonal coordinate (xh,yh) refers to the center of the disk on the horizontal row xh and diagonal column yh. The geometric coordinate of such a point is a function of the radius of the disk , half that of the diameter described in the previous section:


hex_to_geo(int xh, int yh, double r, double *xg, double *yg)
{
*yg = (2.0 * r) * xh * (sqrt(3)/2.0);
*xg = (2.0 * r) * xh * (1.0/2.0) + (2.0 * r) * yh;
}

geo_to_hex(double xg, double yg, double r, double *xh, double *yh)
{
*xh = (2.0/sqrt(3)) * yg / (2.0 * r);
*yh = (xg - (2.0 * r) * (*xh) * (1.0/2.0) ) / (2.0 * r);
}


The row-column nature of the hexagonal coordinate system implies a very useful property, namely that we can efficiently store a patch of hexagons in a matrix m[row][column]. By using the index offsets described for triangular grids, we can easily find the six neighbors of each hexagon.

There is a problem, however. Under the hexagonal coordinate system, the set of hexagons defined by coordinates , where and , forms a diamond-shaped patch, not a conventional axis-oriented rectangle. However, for many applications we are interested in rectangles instead of diamonds.

To solve this problem, we define array coordinates so that (ax,ay) refers to the position in an axis-oriented rectangle with as the lower-left-hand point in the matrix:


array_to_hex(int xa, int ya, int *xh, int *yh)
{
*xh = xa;
*yh = ya - xa + ceil(xa/2.0);
}

hex_to_array(int xh, int yh, int *xa, int *ya)
{
*xa = xh;
*ya = yh + xh - ceil(xh/2.0);
}


Longitude and Latitude

A particularly important coordinate grid is the system of longitude and latitude which uniquely positions every location on the surface of the Earth.

The lines that run east-west, parallel to the equator, are called lines of latitude. The equator has a latitude of , while the north and south poles have latitudes of North and South, respectively.

The lines that run north-south are called lines of longitude or meridians. The prime meridian passes through Greenwich, England, and has longitude , with the entire range of longitudes spanning from West to East.

Every location on the surface of the Earth is described by the intersection of a latitude line and a longitude line. For example, the center of the universe (Manhattan) lies at North and West.

A great circle is a circular cross-section of a sphere which passes through the center of the sphere. The shortest distance between points and turns out to be the arc length between and on the unique great circle which passes through and .

Denote the position of point by its longitude-latitude coordinates, , where all angles are measured in radians. Then the great-circle distance between points and is

Assigned Problems

111202 (The Monocycle) - Find the fastest way to move across a grid with blocked cells, where (1) you pay for each move and turn, and (2) you must start and end on the same color of a 5-color wheel. What is the right graph to capture the full structure?

111203 (Star) - Given row, column, and diagonal sums across a chinese checker board, what are the maximum and minimum possible sums of all the squares on the board?

111205 (Robbery) - Given observations of the form the robber isn't in a given rectangle at a given time'', identify where the robber might have been when.

111207 (Dermuba Triangle) - Find the shortest path (distance) between two houses on a triangular grid.