Grid Layer¶
The Grid Layer is responsible for several things - a) drawing the squared grid for a drawing, b) establishing “snap to grid” fundamental manipulation of points and rects, c) managing the ruler setup and d) establishing the “real world” coordinate system of a drawing. It is implemented as a layer so that it can be placed anywhere relative to your other content - behind, in front, it’s up to you. By default the grid draws a three-part grid designed to simulate common real-world graph paper, using transparency and very fine lines to avoid it “filling up” the display too much. Previously, most drawing programs have had to compromise by drawing unrealistic grids simply because the display resolution was not up to anything better.
Grids are draw in three parts, corresponding to the three types of “squares” that make up the grid. The fundamental grid unit is called its span. This is divided into a whole number of divisions which are the smallest squares in the grid. A whole number of spans makes up a major interval. Thus on a typical metric grid (in fact the default metric grid) the span is set to 1cm, divided into 5 parts (thus each is 2mm square) and there is a major interval every 10 spans, or 10cm. Each part of the grid can be drawn using a different colour, though usually you will probably set a single “theme” colour from which all three are derived.
The grid’s span establishes the basic conversion between the underlying Quartz drawing coordinates and whatever “real world” coordinate system you define. The units can be whatever you want - centimetres, metres, kilometres, miles, light-years... While DrawKit always works in the underlying coordinate system of Quartz points, the user of an application is most likely to want to work in some real system - DKGridLayer is responsible for that mapping. DKDrawing provides a number of convenient methods to access some of the grid’s features, since it helps isolate you from the grid’s position and even its existence (which is optional - some drawing systems will prefer to simply do without a grid).
Snapping points and rectangles to the grid is also performed by fundamental methods in DKGridLayer, but again, you’ll probably access these through DKDrawing or even DKDrawableObject itself, which provides a higher-level interface. DKGridLayer‘s most basic snapping method is - nearestGridIntersectionToPoint:, which given any point in Quartz coordinates, returns the nearest grid point (based on the divisions). You can also convert any length to and from the grid’s coordinate system easily.
DKGridLayer only supports square grids, where the span in the horizontal and vertical dimensions are equal. For alternative grids (polar, for example, or other non-square grid) you would need to subclass DKGridLayer.