2 * (c) Lambros Lambrou 2008
4 * Code for working with general grids, which can be any planar graph
5 * with faces, edges and vertices (dots). Includes generators for a few
6 * types of grid, including square, hexagonal, triangular and others.
20 /* Debugging options */
26 /* ----------------------------------------------------------------------
27 * Deallocate or dereference a grid
29 void grid_free(grid
*g
)
34 if (g
->refcount
== 0) {
36 for (i
= 0; i
< g
->num_faces
; i
++) {
37 sfree(g
->faces
[i
].dots
);
38 sfree(g
->faces
[i
].edges
);
40 for (i
= 0; i
< g
->num_dots
; i
++) {
41 sfree(g
->dots
[i
].faces
);
42 sfree(g
->dots
[i
].edges
);
51 /* Used by the other grid generators. Create a brand new grid with nothing
52 * initialised (all lists are NULL) */
53 static grid
*grid_new(void)
59 g
->num_faces
= g
->num_edges
= g
->num_dots
= 0;
60 g
->middle_face
= NULL
;
62 g
->lowest_x
= g
->lowest_y
= g
->highest_x
= g
->highest_y
= 0;
66 /* Helper function to calculate perpendicular distance from
67 * a point P to a line AB. A and B mustn't be equal here.
69 * Well-known formula for area A of a triangle:
71 * 2A = determinant of matrix | px ax bx |
74 * Also well-known: 2A = base * height
75 * = perpendicular distance * line-length.
77 * Combining gives: distance = determinant / line-length(a,b)
79 static double point_line_distance(long px
, long py
,
83 long det
= ax
*by
- bx
*ay
+ bx
*py
- px
*by
+ px
*ay
- ax
*py
;
86 len
= sqrt(SQ(ax
- bx
) + SQ(ay
- by
));
90 /* Determine nearest edge to where the user clicked.
91 * (x, y) is the clicked location, converted to grid coordinates.
92 * Returns the nearest edge, or NULL if no edge is reasonably
95 * This algorithm is nice and generic, and doesn't depend on any particular
96 * geometric layout of the grid:
97 * Start at any dot (pick one next to middle_face).
98 * Walk along a path by choosing, from all nearby dots, the one that is
99 * nearest the target (x,y). Hopefully end up at the dot which is closest
100 * to (x,y). Should work, as long as faces aren't too badly shaped.
101 * Then examine each edge around this dot, and pick whichever one is
102 * closest (perpendicular distance) to (x,y).
103 * Using perpendicular distance is not quite right - the edge might be
104 * "off to one side". So we insist that the triangle with (x,y) has
105 * acute angles at the edge's dots.
112 * | edge2 is OK, but edge1 is not, even though
113 * | edge1 is perpendicularly closer to (x,y)
117 grid_edge
*grid_nearest_edge(grid
*g
, int x
, int y
)
120 grid_edge
*best_edge
;
121 double best_distance
= 0;
124 cur
= g
->middle_face
->dots
[0];
128 long dist
= SQ((long)cur
->x
- (long)x
) + SQ((long)cur
->y
- (long)y
);
129 /* Look for nearer dot - if found, store in 'new'. */
132 /* Search all dots in all faces touching this dot. Some shapes
133 * (such as in Cairo) don't quite work properly if we only search
134 * the dot's immediate neighbours. */
135 for (i
= 0; i
< cur
->order
; i
++) {
136 grid_face
*f
= cur
->faces
[i
];
139 for (j
= 0; j
< f
->order
; j
++) {
141 grid_dot
*d
= f
->dots
[j
];
142 if (d
== cur
) continue;
143 new_dist
= SQ((long)d
->x
- (long)x
) + SQ((long)d
->y
- (long)y
);
144 if (new_dist
< dist
) {
146 break; /* found closer dot */
150 break; /* found closer dot */
154 /* Didn't find a closer dot among the neighbours of 'cur' */
160 /* 'cur' is nearest dot, so find which of the dot's edges is closest. */
163 for (i
= 0; i
< cur
->order
; i
++) {
164 grid_edge
*e
= cur
->edges
[i
];
165 long e2
; /* squared length of edge */
166 long a2
, b2
; /* squared lengths of other sides */
169 /* See if edge e is eligible - the triangle must have acute angles
170 * at the edge's dots.
171 * Pythagoras formula h^2 = a^2 + b^2 detects right-angles,
172 * so detect acute angles by testing for h^2 < a^2 + b^2 */
173 e2
= SQ((long)e
->dot1
->x
- (long)e
->dot2
->x
) + SQ((long)e
->dot1
->y
- (long)e
->dot2
->y
);
174 a2
= SQ((long)e
->dot1
->x
- (long)x
) + SQ((long)e
->dot1
->y
- (long)y
);
175 b2
= SQ((long)e
->dot2
->x
- (long)x
) + SQ((long)e
->dot2
->y
- (long)y
);
176 if (a2
>= e2
+ b2
) continue;
177 if (b2
>= e2
+ a2
) continue;
179 /* e is eligible so far. Now check the edge is reasonably close
180 * to where the user clicked. Don't want to toggle an edge if the
181 * click was way off the grid.
182 * There is room for experimentation here. We could check the
183 * perpendicular distance is within a certain fraction of the length
184 * of the edge. That amounts to testing a rectangular region around
186 * Alternatively, we could check that the angle at the point is obtuse.
187 * That would amount to testing a circular region with the edge as
189 dist
= point_line_distance((long)x
, (long)y
,
190 (long)e
->dot1
->x
, (long)e
->dot1
->y
,
191 (long)e
->dot2
->x
, (long)e
->dot2
->y
);
192 /* Is dist more than half edge length ? */
193 if (4 * SQ(dist
) > e2
)
196 if (best_edge
== NULL
|| dist
< best_distance
) {
198 best_distance
= dist
;
204 /* ----------------------------------------------------------------------
209 /* Show the basic grid information, before doing grid_make_consistent */
210 static void grid_print_basic(grid
*g
)
212 /* TODO: Maybe we should generate an SVG image of the dots and lines
213 * of the grid here, before grid_make_consistent.
214 * Would help with debugging grid generation. */
216 printf("--- Basic Grid Data ---\n");
217 for (i
= 0; i
< g
->num_faces
; i
++) {
218 grid_face
*f
= g
->faces
+ i
;
219 printf("Face %d: dots[", i
);
221 for (j
= 0; j
< f
->order
; j
++) {
222 grid_dot
*d
= f
->dots
[j
];
223 printf("%s%d", j
? "," : "", (int)(d
- g
->dots
));
227 printf("Middle face: %d\n", (int)(g
->middle_face
- g
->faces
));
229 /* Show the derived grid information, computed by grid_make_consistent */
230 static void grid_print_derived(grid
*g
)
234 printf("--- Derived Grid Data ---\n");
235 for (i
= 0; i
< g
->num_edges
; i
++) {
236 grid_edge
*e
= g
->edges
+ i
;
237 printf("Edge %d: dots[%d,%d] faces[%d,%d]\n",
238 i
, (int)(e
->dot1
- g
->dots
), (int)(e
->dot2
- g
->dots
),
239 e
->face1
? (int)(e
->face1
- g
->faces
) : -1,
240 e
->face2
? (int)(e
->face2
- g
->faces
) : -1);
243 for (i
= 0; i
< g
->num_faces
; i
++) {
244 grid_face
*f
= g
->faces
+ i
;
246 printf("Face %d: faces[", i
);
247 for (j
= 0; j
< f
->order
; j
++) {
248 grid_edge
*e
= f
->edges
[j
];
249 grid_face
*f2
= (e
->face1
== f
) ? e
->face2
: e
->face1
;
250 printf("%s%d", j
? "," : "", f2
? (int)(f2
- g
->faces
) : -1);
255 for (i
= 0; i
< g
->num_dots
; i
++) {
256 grid_dot
*d
= g
->dots
+ i
;
258 printf("Dot %d: dots[", i
);
259 for (j
= 0; j
< d
->order
; j
++) {
260 grid_edge
*e
= d
->edges
[j
];
261 grid_dot
*d2
= (e
->dot1
== d
) ? e
->dot2
: e
->dot1
;
262 printf("%s%d", j
? "," : "", (int)(d2
- g
->dots
));
265 for (j
= 0; j
< d
->order
; j
++) {
266 grid_face
*f
= d
->faces
[j
];
267 printf("%s%d", j
? "," : "", f
? (int)(f
- g
->faces
) : -1);
272 #endif /* DEBUG_GRID */
274 /* Helper function for building incomplete-edges list in
275 * grid_make_consistent() */
276 static int grid_edge_bydots_cmpfn(void *v1
, void *v2
)
282 /* Pointer subtraction is valid here, because all dots point into the
283 * same dot-list (g->dots).
284 * Edges are not "normalised" - the 2 dots could be stored in any order,
285 * so we need to take this into account when comparing edges. */
287 /* Compare first dots */
288 da
= (a
->dot1
< a
->dot2
) ? a
->dot1
: a
->dot2
;
289 db
= (b
->dot1
< b
->dot2
) ? b
->dot1
: b
->dot2
;
292 /* Compare last dots */
293 da
= (a
->dot1
< a
->dot2
) ? a
->dot2
: a
->dot1
;
294 db
= (b
->dot1
< b
->dot2
) ? b
->dot2
: b
->dot1
;
301 /* Input: grid has its dots and faces initialised:
302 * - dots have (optionally) x and y coordinates, but no edges or faces
303 * (pointers are NULL).
304 * - edges not initialised at all
305 * - faces initialised and know which dots they have (but no edges yet). The
306 * dots around each face are assumed to be clockwise.
308 * Output: grid is complete and valid with all relationships defined.
310 static void grid_make_consistent(grid
*g
)
313 tree234
*incomplete_edges
;
314 grid_edge
*next_new_edge
; /* Where new edge will go into g->edges */
320 /* ====== Stage 1 ======
324 /* We know how many dots and faces there are, so we can find the exact
325 * number of edges from Euler's polyhedral formula: F + V = E + 2 .
326 * We use "-1", not "-2" here, because Euler's formula includes the
327 * infinite face, which we don't count. */
328 g
->num_edges
= g
->num_faces
+ g
->num_dots
- 1;
329 g
->edges
= snewn(g
->num_edges
, grid_edge
);
330 next_new_edge
= g
->edges
;
332 /* Iterate over faces, and over each face's dots, generating edges as we
333 * go. As we find each new edge, we can immediately fill in the edge's
334 * dots, but only one of the edge's faces. Later on in the iteration, we
335 * will find the same edge again (unless it's on the border), but we will
336 * know the other face.
337 * For efficiency, maintain a list of the incomplete edges, sorted by
339 incomplete_edges
= newtree234(grid_edge_bydots_cmpfn
);
340 for (i
= 0; i
< g
->num_faces
; i
++) {
341 grid_face
*f
= g
->faces
+ i
;
343 for (j
= 0; j
< f
->order
; j
++) {
344 grid_edge e
; /* fake edge for searching */
345 grid_edge
*edge_found
;
350 e
.dot2
= f
->dots
[j2
];
351 /* Use del234 instead of find234, because we always want to
352 * remove the edge if found */
353 edge_found
= del234(incomplete_edges
, &e
);
355 /* This edge already added, so fill out missing face.
356 * Edge is already removed from incomplete_edges. */
357 edge_found
->face2
= f
;
359 assert(next_new_edge
- g
->edges
< g
->num_edges
);
360 next_new_edge
->dot1
= e
.dot1
;
361 next_new_edge
->dot2
= e
.dot2
;
362 next_new_edge
->face1
= f
;
363 next_new_edge
->face2
= NULL
; /* potentially infinite face */
364 add234(incomplete_edges
, next_new_edge
);
369 freetree234(incomplete_edges
);
371 /* ====== Stage 2 ======
372 * For each face, build its edge list.
375 /* Allocate space for each edge list. Can do this, because each face's
376 * edge-list is the same size as its dot-list. */
377 for (i
= 0; i
< g
->num_faces
; i
++) {
378 grid_face
*f
= g
->faces
+ i
;
380 f
->edges
= snewn(f
->order
, grid_edge
*);
381 /* Preload with NULLs, to help detect potential bugs. */
382 for (j
= 0; j
< f
->order
; j
++)
386 /* Iterate over each edge, and over both its faces. Add this edge to
387 * the face's edge-list, after finding where it should go in the
389 for (i
= 0; i
< g
->num_edges
; i
++) {
390 grid_edge
*e
= g
->edges
+ i
;
392 for (j
= 0; j
< 2; j
++) {
393 grid_face
*f
= j
? e
->face2
: e
->face1
;
395 if (f
== NULL
) continue;
396 /* Find one of the dots around the face */
397 for (k
= 0; k
< f
->order
; k
++) {
398 if (f
->dots
[k
] == e
->dot1
)
399 break; /* found dot1 */
401 assert(k
!= f
->order
); /* Must find the dot around this face */
403 /* Labelling scheme: as we walk clockwise around the face,
404 * starting at dot0 (f->dots[0]), we hit:
405 * (dot0), edge0, dot1, edge1, dot2,...
415 * Therefore, edgeK joins dotK and dot{K+1}
418 /* Around this face, either the next dot or the previous dot
419 * must be e->dot2. Otherwise the edge is wrong. */
423 if (f
->dots
[k2
] == e
->dot2
) {
424 /* dot1(k) and dot2(k2) go clockwise around this face, so add
425 * this edge at position k (see diagram). */
426 assert(f
->edges
[k
] == NULL
);
430 /* Try previous dot */
434 if (f
->dots
[k2
] == e
->dot2
) {
435 /* dot1(k) and dot2(k2) go anticlockwise around this face. */
436 assert(f
->edges
[k2
] == NULL
);
440 assert(!"Grid broken: bad edge-face relationship");
444 /* ====== Stage 3 ======
445 * For each dot, build its edge-list and face-list.
448 /* We don't know how many edges/faces go around each dot, so we can't
449 * allocate the right space for these lists. Pre-compute the sizes by
450 * iterating over each edge and recording a tally against each dot. */
451 for (i
= 0; i
< g
->num_dots
; i
++) {
452 g
->dots
[i
].order
= 0;
454 for (i
= 0; i
< g
->num_edges
; i
++) {
455 grid_edge
*e
= g
->edges
+ i
;
459 /* Now we have the sizes, pre-allocate the edge and face lists. */
460 for (i
= 0; i
< g
->num_dots
; i
++) {
461 grid_dot
*d
= g
->dots
+ i
;
463 assert(d
->order
>= 2); /* sanity check */
464 d
->edges
= snewn(d
->order
, grid_edge
*);
465 d
->faces
= snewn(d
->order
, grid_face
*);
466 for (j
= 0; j
< d
->order
; j
++) {
471 /* For each dot, need to find a face that touches it, so we can seed
472 * the edge-face-edge-face process around each dot. */
473 for (i
= 0; i
< g
->num_faces
; i
++) {
474 grid_face
*f
= g
->faces
+ i
;
476 for (j
= 0; j
< f
->order
; j
++) {
477 grid_dot
*d
= f
->dots
[j
];
481 /* Each dot now has a face in its first slot. Generate the remaining
482 * faces and edges around the dot, by searching both clockwise and
483 * anticlockwise from the first face. Need to do both directions,
484 * because of the possibility of hitting the infinite face, which
485 * blocks progress. But there's only one such face, so we will
486 * succeed in finding every edge and face this way. */
487 for (i
= 0; i
< g
->num_dots
; i
++) {
488 grid_dot
*d
= g
->dots
+ i
;
489 int current_face1
= 0; /* ascends clockwise */
490 int current_face2
= 0; /* descends anticlockwise */
492 /* Labelling scheme: as we walk clockwise around the dot, starting
493 * at face0 (d->faces[0]), we hit:
494 * (face0), edge0, face1, edge1, face2,...
506 * So, for example, face1 should be joined to edge0 and edge1,
507 * and those edges should appear in an anticlockwise sense around
508 * that face (see diagram). */
510 /* clockwise search */
512 grid_face
*f
= d
->faces
[current_face1
];
516 /* find dot around this face */
517 for (j
= 0; j
< f
->order
; j
++) {
521 assert(j
!= f
->order
); /* must find dot */
523 /* Around f, required edge is anticlockwise from the dot. See
524 * the other labelling scheme higher up, for why we subtract 1
530 d
->edges
[current_face1
] = e
; /* set edge */
532 if (current_face1
== d
->order
)
536 d
->faces
[current_face1
] =
537 (e
->face1
== f
) ? e
->face2
: e
->face1
;
538 if (d
->faces
[current_face1
] == NULL
)
539 break; /* cannot progress beyond infinite face */
542 /* If the clockwise search made it all the way round, don't need to
543 * bother with the anticlockwise search. */
544 if (current_face1
== d
->order
)
545 continue; /* this dot is complete, move on to next dot */
547 /* anticlockwise search */
549 grid_face
*f
= d
->faces
[current_face2
];
553 /* find dot around this face */
554 for (j
= 0; j
< f
->order
; j
++) {
558 assert(j
!= f
->order
); /* must find dot */
560 /* Around f, required edge is clockwise from the dot. */
564 if (current_face2
== -1)
565 current_face2
= d
->order
- 1;
566 d
->edges
[current_face2
] = e
; /* set edge */
569 if (current_face2
== current_face1
)
571 d
->faces
[current_face2
] =
572 (e
->face1
== f
) ? e
->face2
: e
->face1
;
573 /* There's only 1 infinite face, so we must get all the way
574 * to current_face1 before we hit it. */
575 assert(d
->faces
[current_face2
]);
579 /* ====== Stage 4 ======
580 * Compute other grid settings
583 /* Bounding rectangle */
584 for (i
= 0; i
< g
->num_dots
; i
++) {
585 grid_dot
*d
= g
->dots
+ i
;
587 g
->lowest_x
= g
->highest_x
= d
->x
;
588 g
->lowest_y
= g
->highest_y
= d
->y
;
590 g
->lowest_x
= min(g
->lowest_x
, d
->x
);
591 g
->highest_x
= max(g
->highest_x
, d
->x
);
592 g
->lowest_y
= min(g
->lowest_y
, d
->y
);
593 g
->highest_y
= max(g
->highest_y
, d
->y
);
598 grid_print_derived(g
);
602 /* Helpers for making grid-generation easier. These functions are only
603 * intended for use during grid generation. */
605 /* Comparison function for the (tree234) sorted dot list */
606 static int grid_point_cmp_fn(void *v1
, void *v2
)
611 return p2
->y
- p1
->y
;
613 return p2
->x
- p1
->x
;
615 /* Add a new face to the grid, with its dot list allocated.
616 * Assumes there's enough space allocated for the new face in grid->faces */
617 static void grid_face_add_new(grid
*g
, int face_size
)
620 grid_face
*new_face
= g
->faces
+ g
->num_faces
;
621 new_face
->order
= face_size
;
622 new_face
->dots
= snewn(face_size
, grid_dot
*);
623 for (i
= 0; i
< face_size
; i
++)
624 new_face
->dots
[i
] = NULL
;
625 new_face
->edges
= NULL
;
628 /* Assumes dot list has enough space */
629 static grid_dot
*grid_dot_add_new(grid
*g
, int x
, int y
)
631 grid_dot
*new_dot
= g
->dots
+ g
->num_dots
;
633 new_dot
->edges
= NULL
;
634 new_dot
->faces
= NULL
;
640 /* Retrieve a dot with these (x,y) coordinates. Either return an existing dot
641 * in the dot_list, or add a new dot to the grid (and the dot_list) and
643 * Assumes g->dots has enough capacity allocated */
644 static grid_dot
*grid_get_dot(grid
*g
, tree234
*dot_list
, int x
, int y
)
653 ret
= find234(dot_list
, &test
, NULL
);
657 ret
= grid_dot_add_new(g
, x
, y
);
658 add234(dot_list
, ret
);
662 /* Sets the last face of the grid to include this dot, at this position
663 * around the face. Assumes num_faces is at least 1 (a new face has
664 * previously been added, with the required number of dots allocated) */
665 static void grid_face_set_dot(grid
*g
, grid_dot
*d
, int position
)
667 grid_face
*last_face
= g
->faces
+ g
->num_faces
- 1;
668 last_face
->dots
[position
] = d
;
671 /* ------ Generate various types of grid ------ */
673 /* General method is to generate faces, by calculating their dot coordinates.
674 * As new faces are added, we keep track of all the dots so we can tell when
675 * a new face reuses an existing dot. For example, two squares touching at an
676 * edge would generate six unique dots: four dots from the first face, then
677 * two additional dots for the second face, because we detect the other two
678 * dots have already been taken up. This list is stored in a tree234
679 * called "points". No extra memory-allocation needed here - we store the
680 * actual grid_dot* pointers, which all point into the g->dots list.
681 * For this reason, we have to calculate coordinates in such a way as to
682 * eliminate any rounding errors, so we can detect when a dot on one
683 * face precisely lands on a dot of a different face. No floating-point
687 grid
*grid_new_square(int width
, int height
)
693 /* Upper bounds - don't have to be exact */
694 int max_faces
= width
* height
;
695 int max_dots
= (width
+ 1) * (height
+ 1);
699 grid
*g
= grid_new();
701 g
->faces
= snewn(max_faces
, grid_face
);
702 g
->dots
= snewn(max_dots
, grid_dot
);
704 points
= newtree234(grid_point_cmp_fn
);
706 /* generate square faces */
707 for (y
= 0; y
< height
; y
++) {
708 for (x
= 0; x
< width
; x
++) {
714 grid_face_add_new(g
, 4);
715 d
= grid_get_dot(g
, points
, px
, py
);
716 grid_face_set_dot(g
, d
, 0);
717 d
= grid_get_dot(g
, points
, px
+ a
, py
);
718 grid_face_set_dot(g
, d
, 1);
719 d
= grid_get_dot(g
, points
, px
+ a
, py
+ a
);
720 grid_face_set_dot(g
, d
, 2);
721 d
= grid_get_dot(g
, points
, px
, py
+ a
);
722 grid_face_set_dot(g
, d
, 3);
727 assert(g
->num_faces
<= max_faces
);
728 assert(g
->num_dots
<= max_dots
);
729 g
->middle_face
= g
->faces
+ (height
/2) * width
+ (width
/2);
731 grid_make_consistent(g
);
735 grid
*grid_new_honeycomb(int width
, int height
)
738 /* Vector for side of hexagon - ratio is close to sqrt(3) */
742 /* Upper bounds - don't have to be exact */
743 int max_faces
= width
* height
;
744 int max_dots
= 2 * (width
+ 1) * (height
+ 1);
748 grid
*g
= grid_new();
750 g
->faces
= snewn(max_faces
, grid_face
);
751 g
->dots
= snewn(max_dots
, grid_dot
);
753 points
= newtree234(grid_point_cmp_fn
);
755 /* generate hexagonal faces */
756 for (y
= 0; y
< height
; y
++) {
757 for (x
= 0; x
< width
; x
++) {
764 grid_face_add_new(g
, 6);
766 d
= grid_get_dot(g
, points
, cx
- a
, cy
- b
);
767 grid_face_set_dot(g
, d
, 0);
768 d
= grid_get_dot(g
, points
, cx
+ a
, cy
- b
);
769 grid_face_set_dot(g
, d
, 1);
770 d
= grid_get_dot(g
, points
, cx
+ 2*a
, cy
);
771 grid_face_set_dot(g
, d
, 2);
772 d
= grid_get_dot(g
, points
, cx
+ a
, cy
+ b
);
773 grid_face_set_dot(g
, d
, 3);
774 d
= grid_get_dot(g
, points
, cx
- a
, cy
+ b
);
775 grid_face_set_dot(g
, d
, 4);
776 d
= grid_get_dot(g
, points
, cx
- 2*a
, cy
);
777 grid_face_set_dot(g
, d
, 5);
782 assert(g
->num_faces
<= max_faces
);
783 assert(g
->num_dots
<= max_dots
);
784 g
->middle_face
= g
->faces
+ (height
/2) * width
+ (width
/2);
786 grid_make_consistent(g
);
790 /* Doesn't use the previous method of generation, it pre-dates it!
791 * A triangular grid is just about simple enough to do by "brute force" */
792 grid
*grid_new_triangular(int width
, int height
)
796 /* Vector for side of triangle - ratio is close to sqrt(3) */
802 /* convenient alias */
805 grid
*g
= grid_new();
806 g
->tilesize
= 18; /* adjust to your taste */
808 g
->num_faces
= width
* height
* 2;
809 g
->num_dots
= (width
+ 1) * (height
+ 1);
810 g
->faces
= snewn(g
->num_faces
, grid_face
);
811 g
->dots
= snewn(g
->num_dots
, grid_dot
);
815 for (y
= 0; y
<= height
; y
++) {
816 for (x
= 0; x
<= width
; x
++) {
817 grid_dot
*d
= g
->dots
+ index
;
818 /* odd rows are offset to the right */
822 d
->x
= x
* 2 * vec_x
+ ((y
% 2) ? vec_x
: 0);
830 for (y
= 0; y
< height
; y
++) {
831 for (x
= 0; x
< width
; x
++) {
832 /* initialise two faces for this (x,y) */
833 grid_face
*f1
= g
->faces
+ index
;
834 grid_face
*f2
= f1
+ 1;
837 f1
->dots
= snewn(f1
->order
, grid_dot
*);
840 f2
->dots
= snewn(f2
->order
, grid_dot
*);
842 /* face descriptions depend on whether the row-number is
845 f1
->dots
[0] = g
->dots
+ y
* w
+ x
;
846 f1
->dots
[1] = g
->dots
+ (y
+ 1) * w
+ x
+ 1;
847 f1
->dots
[2] = g
->dots
+ (y
+ 1) * w
+ x
;
848 f2
->dots
[0] = g
->dots
+ y
* w
+ x
;
849 f2
->dots
[1] = g
->dots
+ y
* w
+ x
+ 1;
850 f2
->dots
[2] = g
->dots
+ (y
+ 1) * w
+ x
+ 1;
852 f1
->dots
[0] = g
->dots
+ y
* w
+ x
;
853 f1
->dots
[1] = g
->dots
+ y
* w
+ x
+ 1;
854 f1
->dots
[2] = g
->dots
+ (y
+ 1) * w
+ x
;
855 f2
->dots
[0] = g
->dots
+ y
* w
+ x
+ 1;
856 f2
->dots
[1] = g
->dots
+ (y
+ 1) * w
+ x
+ 1;
857 f2
->dots
[2] = g
->dots
+ (y
+ 1) * w
+ x
;
863 /* "+ width" takes us to the middle of the row, because each row has
864 * (2*width) faces. */
865 g
->middle_face
= g
->faces
+ (height
/ 2) * 2 * width
+ width
;
867 grid_make_consistent(g
);
871 grid
*grid_new_snubsquare(int width
, int height
)
874 /* Vector for side of triangle - ratio is close to sqrt(3) */
878 /* Upper bounds - don't have to be exact */
879 int max_faces
= 3 * width
* height
;
880 int max_dots
= 2 * (width
+ 1) * (height
+ 1);
884 grid
*g
= grid_new();
886 g
->faces
= snewn(max_faces
, grid_face
);
887 g
->dots
= snewn(max_dots
, grid_dot
);
889 points
= newtree234(grid_point_cmp_fn
);
891 for (y
= 0; y
< height
; y
++) {
892 for (x
= 0; x
< width
; x
++) {
895 int px
= (a
+ b
) * x
;
896 int py
= (a
+ b
) * y
;
898 /* generate square faces */
899 grid_face_add_new(g
, 4);
901 d
= grid_get_dot(g
, points
, px
+ a
, py
);
902 grid_face_set_dot(g
, d
, 0);
903 d
= grid_get_dot(g
, points
, px
+ a
+ b
, py
+ a
);
904 grid_face_set_dot(g
, d
, 1);
905 d
= grid_get_dot(g
, points
, px
+ b
, py
+ a
+ b
);
906 grid_face_set_dot(g
, d
, 2);
907 d
= grid_get_dot(g
, points
, px
, py
+ b
);
908 grid_face_set_dot(g
, d
, 3);
910 d
= grid_get_dot(g
, points
, px
+ b
, py
);
911 grid_face_set_dot(g
, d
, 0);
912 d
= grid_get_dot(g
, points
, px
+ a
+ b
, py
+ b
);
913 grid_face_set_dot(g
, d
, 1);
914 d
= grid_get_dot(g
, points
, px
+ a
, py
+ a
+ b
);
915 grid_face_set_dot(g
, d
, 2);
916 d
= grid_get_dot(g
, points
, px
, py
+ a
);
917 grid_face_set_dot(g
, d
, 3);
920 /* generate up/down triangles */
922 grid_face_add_new(g
, 3);
924 d
= grid_get_dot(g
, points
, px
+ a
, py
);
925 grid_face_set_dot(g
, d
, 0);
926 d
= grid_get_dot(g
, points
, px
, py
+ b
);
927 grid_face_set_dot(g
, d
, 1);
928 d
= grid_get_dot(g
, points
, px
- a
, py
);
929 grid_face_set_dot(g
, d
, 2);
931 d
= grid_get_dot(g
, points
, px
, py
+ a
);
932 grid_face_set_dot(g
, d
, 0);
933 d
= grid_get_dot(g
, points
, px
+ a
, py
+ a
+ b
);
934 grid_face_set_dot(g
, d
, 1);
935 d
= grid_get_dot(g
, points
, px
- a
, py
+ a
+ b
);
936 grid_face_set_dot(g
, d
, 2);
940 /* generate left/right triangles */
942 grid_face_add_new(g
, 3);
944 d
= grid_get_dot(g
, points
, px
+ a
, py
);
945 grid_face_set_dot(g
, d
, 0);
946 d
= grid_get_dot(g
, points
, px
+ a
+ b
, py
- a
);
947 grid_face_set_dot(g
, d
, 1);
948 d
= grid_get_dot(g
, points
, px
+ a
+ b
, py
+ a
);
949 grid_face_set_dot(g
, d
, 2);
951 d
= grid_get_dot(g
, points
, px
, py
- a
);
952 grid_face_set_dot(g
, d
, 0);
953 d
= grid_get_dot(g
, points
, px
+ b
, py
);
954 grid_face_set_dot(g
, d
, 1);
955 d
= grid_get_dot(g
, points
, px
, py
+ a
);
956 grid_face_set_dot(g
, d
, 2);
963 assert(g
->num_faces
<= max_faces
);
964 assert(g
->num_dots
<= max_dots
);
965 g
->middle_face
= g
->faces
+ (height
/2) * width
+ (width
/2);
967 grid_make_consistent(g
);
971 grid
*grid_new_cairo(int width
, int height
)
974 /* Vector for side of pentagon - ratio is close to (4+sqrt(7))/3 */
978 /* Upper bounds - don't have to be exact */
979 int max_faces
= 2 * width
* height
;
980 int max_dots
= 3 * (width
+ 1) * (height
+ 1);
984 grid
*g
= grid_new();
986 g
->faces
= snewn(max_faces
, grid_face
);
987 g
->dots
= snewn(max_dots
, grid_dot
);
989 points
= newtree234(grid_point_cmp_fn
);
991 for (y
= 0; y
< height
; y
++) {
992 for (x
= 0; x
< width
; x
++) {
998 /* horizontal pentagons */
1000 grid_face_add_new(g
, 5);
1002 d
= grid_get_dot(g
, points
, px
+ a
, py
- b
);
1003 grid_face_set_dot(g
, d
, 0);
1004 d
= grid_get_dot(g
, points
, px
+ 2*b
- a
, py
- b
);
1005 grid_face_set_dot(g
, d
, 1);
1006 d
= grid_get_dot(g
, points
, px
+ 2*b
, py
);
1007 grid_face_set_dot(g
, d
, 2);
1008 d
= grid_get_dot(g
, points
, px
+ b
, py
+ a
);
1009 grid_face_set_dot(g
, d
, 3);
1010 d
= grid_get_dot(g
, points
, px
, py
);
1011 grid_face_set_dot(g
, d
, 4);
1013 d
= grid_get_dot(g
, points
, px
, py
);
1014 grid_face_set_dot(g
, d
, 0);
1015 d
= grid_get_dot(g
, points
, px
+ b
, py
- a
);
1016 grid_face_set_dot(g
, d
, 1);
1017 d
= grid_get_dot(g
, points
, px
+ 2*b
, py
);
1018 grid_face_set_dot(g
, d
, 2);
1019 d
= grid_get_dot(g
, points
, px
+ 2*b
- a
, py
+ b
);
1020 grid_face_set_dot(g
, d
, 3);
1021 d
= grid_get_dot(g
, points
, px
+ a
, py
+ b
);
1022 grid_face_set_dot(g
, d
, 4);
1025 /* vertical pentagons */
1027 grid_face_add_new(g
, 5);
1029 d
= grid_get_dot(g
, points
, px
, py
);
1030 grid_face_set_dot(g
, d
, 0);
1031 d
= grid_get_dot(g
, points
, px
+ b
, py
+ a
);
1032 grid_face_set_dot(g
, d
, 1);
1033 d
= grid_get_dot(g
, points
, px
+ b
, py
+ 2*b
- a
);
1034 grid_face_set_dot(g
, d
, 2);
1035 d
= grid_get_dot(g
, points
, px
, py
+ 2*b
);
1036 grid_face_set_dot(g
, d
, 3);
1037 d
= grid_get_dot(g
, points
, px
- a
, py
+ b
);
1038 grid_face_set_dot(g
, d
, 4);
1040 d
= grid_get_dot(g
, points
, px
, py
);
1041 grid_face_set_dot(g
, d
, 0);
1042 d
= grid_get_dot(g
, points
, px
+ a
, py
+ b
);
1043 grid_face_set_dot(g
, d
, 1);
1044 d
= grid_get_dot(g
, points
, px
, py
+ 2*b
);
1045 grid_face_set_dot(g
, d
, 2);
1046 d
= grid_get_dot(g
, points
, px
- b
, py
+ 2*b
- a
);
1047 grid_face_set_dot(g
, d
, 3);
1048 d
= grid_get_dot(g
, points
, px
- b
, py
+ a
);
1049 grid_face_set_dot(g
, d
, 4);
1055 freetree234(points
);
1056 assert(g
->num_faces
<= max_faces
);
1057 assert(g
->num_dots
<= max_dots
);
1058 g
->middle_face
= g
->faces
+ (height
/2) * width
+ (width
/2);
1060 grid_make_consistent(g
);
1064 grid
*grid_new_greathexagonal(int width
, int height
)
1067 /* Vector for side of triangle - ratio is close to sqrt(3) */
1071 /* Upper bounds - don't have to be exact */
1072 int max_faces
= 6 * (width
+ 1) * (height
+ 1);
1073 int max_dots
= 6 * width
* height
;
1077 grid
*g
= grid_new();
1079 g
->faces
= snewn(max_faces
, grid_face
);
1080 g
->dots
= snewn(max_dots
, grid_dot
);
1082 points
= newtree234(grid_point_cmp_fn
);
1084 for (y
= 0; y
< height
; y
++) {
1085 for (x
= 0; x
< width
; x
++) {
1087 /* centre of hexagon */
1088 int px
= (3*a
+ b
) * x
;
1089 int py
= (2*a
+ 2*b
) * y
;
1094 grid_face_add_new(g
, 6);
1095 d
= grid_get_dot(g
, points
, px
- a
, py
- b
);
1096 grid_face_set_dot(g
, d
, 0);
1097 d
= grid_get_dot(g
, points
, px
+ a
, py
- b
);
1098 grid_face_set_dot(g
, d
, 1);
1099 d
= grid_get_dot(g
, points
, px
+ 2*a
, py
);
1100 grid_face_set_dot(g
, d
, 2);
1101 d
= grid_get_dot(g
, points
, px
+ a
, py
+ b
);
1102 grid_face_set_dot(g
, d
, 3);
1103 d
= grid_get_dot(g
, points
, px
- a
, py
+ b
);
1104 grid_face_set_dot(g
, d
, 4);
1105 d
= grid_get_dot(g
, points
, px
- 2*a
, py
);
1106 grid_face_set_dot(g
, d
, 5);
1108 /* square below hexagon */
1109 if (y
< height
- 1) {
1110 grid_face_add_new(g
, 4);
1111 d
= grid_get_dot(g
, points
, px
- a
, py
+ b
);
1112 grid_face_set_dot(g
, d
, 0);
1113 d
= grid_get_dot(g
, points
, px
+ a
, py
+ b
);
1114 grid_face_set_dot(g
, d
, 1);
1115 d
= grid_get_dot(g
, points
, px
+ a
, py
+ 2*a
+ b
);
1116 grid_face_set_dot(g
, d
, 2);
1117 d
= grid_get_dot(g
, points
, px
- a
, py
+ 2*a
+ b
);
1118 grid_face_set_dot(g
, d
, 3);
1121 /* square below right */
1122 if ((x
< width
- 1) && (((x
% 2) == 0) || (y
< height
- 1))) {
1123 grid_face_add_new(g
, 4);
1124 d
= grid_get_dot(g
, points
, px
+ 2*a
, py
);
1125 grid_face_set_dot(g
, d
, 0);
1126 d
= grid_get_dot(g
, points
, px
+ 2*a
+ b
, py
+ a
);
1127 grid_face_set_dot(g
, d
, 1);
1128 d
= grid_get_dot(g
, points
, px
+ a
+ b
, py
+ a
+ b
);
1129 grid_face_set_dot(g
, d
, 2);
1130 d
= grid_get_dot(g
, points
, px
+ a
, py
+ b
);
1131 grid_face_set_dot(g
, d
, 3);
1134 /* square below left */
1135 if ((x
> 0) && (((x
% 2) == 0) || (y
< height
- 1))) {
1136 grid_face_add_new(g
, 4);
1137 d
= grid_get_dot(g
, points
, px
- 2*a
, py
);
1138 grid_face_set_dot(g
, d
, 0);
1139 d
= grid_get_dot(g
, points
, px
- a
, py
+ b
);
1140 grid_face_set_dot(g
, d
, 1);
1141 d
= grid_get_dot(g
, points
, px
- a
- b
, py
+ a
+ b
);
1142 grid_face_set_dot(g
, d
, 2);
1143 d
= grid_get_dot(g
, points
, px
- 2*a
- b
, py
+ a
);
1144 grid_face_set_dot(g
, d
, 3);
1147 /* Triangle below right */
1148 if ((x
< width
- 1) && (y
< height
- 1)) {
1149 grid_face_add_new(g
, 3);
1150 d
= grid_get_dot(g
, points
, px
+ a
, py
+ b
);
1151 grid_face_set_dot(g
, d
, 0);
1152 d
= grid_get_dot(g
, points
, px
+ a
+ b
, py
+ a
+ b
);
1153 grid_face_set_dot(g
, d
, 1);
1154 d
= grid_get_dot(g
, points
, px
+ a
, py
+ 2*a
+ b
);
1155 grid_face_set_dot(g
, d
, 2);
1158 /* Triangle below left */
1159 if ((x
> 0) && (y
< height
- 1)) {
1160 grid_face_add_new(g
, 3);
1161 d
= grid_get_dot(g
, points
, px
- a
, py
+ b
);
1162 grid_face_set_dot(g
, d
, 0);
1163 d
= grid_get_dot(g
, points
, px
- a
, py
+ 2*a
+ b
);
1164 grid_face_set_dot(g
, d
, 1);
1165 d
= grid_get_dot(g
, points
, px
- a
- b
, py
+ a
+ b
);
1166 grid_face_set_dot(g
, d
, 2);
1171 freetree234(points
);
1172 assert(g
->num_faces
<= max_faces
);
1173 assert(g
->num_dots
<= max_dots
);
1174 g
->middle_face
= g
->faces
+ (height
/2) * width
+ (width
/2);
1176 grid_make_consistent(g
);
1180 grid
*grid_new_octagonal(int width
, int height
)
1183 /* b/a approx sqrt(2) */
1187 /* Upper bounds - don't have to be exact */
1188 int max_faces
= 2 * width
* height
;
1189 int max_dots
= 4 * (width
+ 1) * (height
+ 1);
1193 grid
*g
= grid_new();
1195 g
->faces
= snewn(max_faces
, grid_face
);
1196 g
->dots
= snewn(max_dots
, grid_dot
);
1198 points
= newtree234(grid_point_cmp_fn
);
1200 for (y
= 0; y
< height
; y
++) {
1201 for (x
= 0; x
< width
; x
++) {
1204 int px
= (2*a
+ b
) * x
;
1205 int py
= (2*a
+ b
) * y
;
1207 grid_face_add_new(g
, 8);
1208 d
= grid_get_dot(g
, points
, px
+ a
, py
);
1209 grid_face_set_dot(g
, d
, 0);
1210 d
= grid_get_dot(g
, points
, px
+ a
+ b
, py
);
1211 grid_face_set_dot(g
, d
, 1);
1212 d
= grid_get_dot(g
, points
, px
+ 2*a
+ b
, py
+ a
);
1213 grid_face_set_dot(g
, d
, 2);
1214 d
= grid_get_dot(g
, points
, px
+ 2*a
+ b
, py
+ a
+ b
);
1215 grid_face_set_dot(g
, d
, 3);
1216 d
= grid_get_dot(g
, points
, px
+ a
+ b
, py
+ 2*a
+ b
);
1217 grid_face_set_dot(g
, d
, 4);
1218 d
= grid_get_dot(g
, points
, px
+ a
, py
+ 2*a
+ b
);
1219 grid_face_set_dot(g
, d
, 5);
1220 d
= grid_get_dot(g
, points
, px
, py
+ a
+ b
);
1221 grid_face_set_dot(g
, d
, 6);
1222 d
= grid_get_dot(g
, points
, px
, py
+ a
);
1223 grid_face_set_dot(g
, d
, 7);
1226 if ((x
> 0) && (y
> 0)) {
1227 grid_face_add_new(g
, 4);
1228 d
= grid_get_dot(g
, points
, px
, py
- a
);
1229 grid_face_set_dot(g
, d
, 0);
1230 d
= grid_get_dot(g
, points
, px
+ a
, py
);
1231 grid_face_set_dot(g
, d
, 1);
1232 d
= grid_get_dot(g
, points
, px
, py
+ a
);
1233 grid_face_set_dot(g
, d
, 2);
1234 d
= grid_get_dot(g
, points
, px
- a
, py
);
1235 grid_face_set_dot(g
, d
, 3);
1240 freetree234(points
);
1241 assert(g
->num_faces
<= max_faces
);
1242 assert(g
->num_dots
<= max_dots
);
1243 g
->middle_face
= g
->faces
+ (height
/2) * width
+ (width
/2);
1245 grid_make_consistent(g
);
1249 grid
*grid_new_kites(int width
, int height
)
1252 /* b/a approx sqrt(3) */
1256 /* Upper bounds - don't have to be exact */
1257 int max_faces
= 6 * width
* height
;
1258 int max_dots
= 6 * (width
+ 1) * (height
+ 1);
1262 grid
*g
= grid_new();
1264 g
->faces
= snewn(max_faces
, grid_face
);
1265 g
->dots
= snewn(max_dots
, grid_dot
);
1267 points
= newtree234(grid_point_cmp_fn
);
1269 for (y
= 0; y
< height
; y
++) {
1270 for (x
= 0; x
< width
; x
++) {
1272 /* position of order-6 dot */
1278 /* kite pointing up-left */
1279 grid_face_add_new(g
, 4);
1280 d
= grid_get_dot(g
, points
, px
, py
);
1281 grid_face_set_dot(g
, d
, 0);
1282 d
= grid_get_dot(g
, points
, px
+ 2*b
, py
);
1283 grid_face_set_dot(g
, d
, 1);
1284 d
= grid_get_dot(g
, points
, px
+ 2*b
, py
+ 2*a
);
1285 grid_face_set_dot(g
, d
, 2);
1286 d
= grid_get_dot(g
, points
, px
+ b
, py
+ 3*a
);
1287 grid_face_set_dot(g
, d
, 3);
1289 /* kite pointing up */
1290 grid_face_add_new(g
, 4);
1291 d
= grid_get_dot(g
, points
, px
, py
);
1292 grid_face_set_dot(g
, d
, 0);
1293 d
= grid_get_dot(g
, points
, px
+ b
, py
+ 3*a
);
1294 grid_face_set_dot(g
, d
, 1);
1295 d
= grid_get_dot(g
, points
, px
, py
+ 4*a
);
1296 grid_face_set_dot(g
, d
, 2);
1297 d
= grid_get_dot(g
, points
, px
- b
, py
+ 3*a
);
1298 grid_face_set_dot(g
, d
, 3);
1300 /* kite pointing up-right */
1301 grid_face_add_new(g
, 4);
1302 d
= grid_get_dot(g
, points
, px
, py
);
1303 grid_face_set_dot(g
, d
, 0);
1304 d
= grid_get_dot(g
, points
, px
- b
, py
+ 3*a
);
1305 grid_face_set_dot(g
, d
, 1);
1306 d
= grid_get_dot(g
, points
, px
- 2*b
, py
+ 2*a
);
1307 grid_face_set_dot(g
, d
, 2);
1308 d
= grid_get_dot(g
, points
, px
- 2*b
, py
);
1309 grid_face_set_dot(g
, d
, 3);
1311 /* kite pointing down-right */
1312 grid_face_add_new(g
, 4);
1313 d
= grid_get_dot(g
, points
, px
, py
);
1314 grid_face_set_dot(g
, d
, 0);
1315 d
= grid_get_dot(g
, points
, px
- 2*b
, py
);
1316 grid_face_set_dot(g
, d
, 1);
1317 d
= grid_get_dot(g
, points
, px
- 2*b
, py
- 2*a
);
1318 grid_face_set_dot(g
, d
, 2);
1319 d
= grid_get_dot(g
, points
, px
- b
, py
- 3*a
);
1320 grid_face_set_dot(g
, d
, 3);
1322 /* kite pointing down */
1323 grid_face_add_new(g
, 4);
1324 d
= grid_get_dot(g
, points
, px
, py
);
1325 grid_face_set_dot(g
, d
, 0);
1326 d
= grid_get_dot(g
, points
, px
- b
, py
- 3*a
);
1327 grid_face_set_dot(g
, d
, 1);
1328 d
= grid_get_dot(g
, points
, px
, py
- 4*a
);
1329 grid_face_set_dot(g
, d
, 2);
1330 d
= grid_get_dot(g
, points
, px
+ b
, py
- 3*a
);
1331 grid_face_set_dot(g
, d
, 3);
1333 /* kite pointing down-left */
1334 grid_face_add_new(g
, 4);
1335 d
= grid_get_dot(g
, points
, px
, py
);
1336 grid_face_set_dot(g
, d
, 0);
1337 d
= grid_get_dot(g
, points
, px
+ b
, py
- 3*a
);
1338 grid_face_set_dot(g
, d
, 1);
1339 d
= grid_get_dot(g
, points
, px
+ 2*b
, py
- 2*a
);
1340 grid_face_set_dot(g
, d
, 2);
1341 d
= grid_get_dot(g
, points
, px
+ 2*b
, py
);
1342 grid_face_set_dot(g
, d
, 3);
1346 freetree234(points
);
1347 assert(g
->num_faces
<= max_faces
);
1348 assert(g
->num_dots
<= max_dots
);
1349 g
->middle_face
= g
->faces
+ 6 * ((height
/2) * width
+ (width
/2));
1351 grid_make_consistent(g
);
1355 /* ----------- End of grid generators ------------- */