Grids: Steven Skiena
Grids: Steven Skiena
http://www.cs.sunysb.edu/skiena
Rectilinear Grids
Rectilinear grids are typically dened 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 nding 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 d-dimensions, each vertex touches 2d edges and 2d 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 n m rectilinear grid. Any such traversal can be thought of as a mapping from each of the nm ordered pairs to a unique integer from 1 to nm. The most important traversal methods are Row Major Here we slice the matrix between rows, so the rst m elements visited belong to the rst row, the second m elements to the second row, and so forth. Column Major Here we slice the matrix between columns, so the rst n elements belong to the rst column, the second n 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 pointerarithmetic operations. Diagonal Order Here we march up and down diagonals. Note that an n m grid has m+n 1 diagonals, each with a variable number of elements. This is a trickier task than it appears at rst glance.
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 d ranges over four values (north, east, south, and west) which denote the edge directions from point (i, j ).
Triangular Lattices
Triangular lattices are constructed from three sets of equally spaced lines, consisting of a horizontal row axis, a column axis 60o from horizontal, and a diagonal axis 120o 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 v is connected to six others, those immediately above and below v 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 (0, 0). 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 (x, y ) follow by adding 1 to either the row or column coordinates. Although the intersection of three lines denes each grid vertex, in fact the row and column dimensions to specify location. A vertex (x, y ) lies x rows above the origin, and y (60o)columns to the right of the origin. The neighbors of a vertex v can be found by adding the following pairs to the coordinates of v , in counterclockwise order: (0, 1), (1, 0), (1, 1), (0, 1), (1, 0), and (1, 1). Geometrical Coordinates The vertices of a regular
triangular grid occur in half-staggered rows. Assume that each lattice point is a distance d from its six nearest neighbors, and that point (0, 0) in triangular coordinates in fact lies at geometric point (0, 0). Then triangular-coordinate point (xt, yt) must lie at geometric point (xg , yg ) = (d(xt + (yt cos(60o))), dyt sin(60o)) by simple trigonometry, where cos(60o) = 1/2 and sin(60o) = 3/2,
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 (0, 0). 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 r, half that of the diameter d 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 efciently store a patch of hexagons in a matrix m[row][column]. By using the index offsets described for triangular grids, we can easily nd the six neighbors of each hexagon. There is a problem, however. Under the hexagonal coordinate system, the set of hexagons dened by coordinates (hx, hy ),
where 0 hx xmax and 0 hy ymax, 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 dene array coordinates so that (ax,ay) refers to the position in an axis-oriented rectangle with (0, 0) 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); }
the intersection of a latitude line and a longitude line. For example, the center of the universe (Manhattan) lies at 40o47 North and 73o58 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 x and y turns out to be the arc length between x and y on the unique great circle which passes through x and y. Denote the position of point p by its longitude-latitude coordinates, (plat, plong), where all angles are measured in radians. Then the great-circle distance between points p and q is
d(f, s) = R arccos((sin(l[f ]) sin(l[s])) + (cos(l[f ]) cos(l[s]) cos(dg )))
where R is the radius of the Great circle, dg = longitude(f)longitude(s), and l[f ] = lattitude of f.
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 isnt in a given rectangle at a given time, identify where the robber might have been when.