+ if(!circle_order) {
+ circle_order_size = (max_radius*(max_radius+1))/2;
+ circle_order = malloc(sizeof(circle_coord_t)*circle_order_size);
+ int x,y;
+ int i = 0;
+ for(y=0;y<max_radius;y++) {
+ for(x=0;x<=y;x++) {
+ circle_order[i].x=x;
+ circle_order[i].y=y;
+ i++;
+ }
+ }
+ assert(i==circle_order_size);
+ qsort(circle_order, circle_order_size, sizeof(circle_coord_t), compare_circle_coord);
+ }
+
+ int t;
+ void**data = context->group;
+ int signx[4] = {-1,1,-1,1};
+ int signy[4] = {-1,-1,1,1};
+ for(t=1;t<circle_order_size;t++) {
+ int xx = circle_order[t].x;
+ int yy = circle_order[t].y;
+ int s;
+ for(s=0;s<4;s++) {
+ int x=h->x+xx*signx[s];
+ int y=h->y+yy*signy[s];
+ if(x>=0 && y>=0 && x<context->width && y<context->height) {
+ int pos = y*context->width+x;
+ if(data[pos]) {
+ while(!POINTS_TO_HEAD(data[pos])) {
+ pos=(void**)data[pos]-(void**)data;
+ }
+ head_t*new_head = (head_t*)data[pos];
+ if(new_head != h) {
+ return new_head;
+ }
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+static void fix_small_boxes(context_t*context)
+{
+ double sintab[256];
+ double costab[256];
+ int t;
+ for(t=0;t<256;t++) {
+ sintab[t] = sin(t*M_PI/128);
+ costab[t] = cos(t*M_PI/128);
+ }
+
+ head_t*h = context->heads;
+ while(h) {
+ h->seen = 0;
+ h = h->next;
+ }
+
+ char changed;
+ do {
+ changed = 0;
+ head_t*h = context->heads;
+ while(h) {
+ head_t*next = h->next;
+ if(!h->seen) {
+ if(h->bbox.xmax - h->bbox.xmin < 32
+ || h->bbox.ymax - h->bbox.ymin < 32) {
+ head_t*other = search_vicinity(context, h, 64, costab, sintab);
+ if(other) {
+ merge(context, h->pos, other->pos);
+ changed = 1;
+ break;
+ } else {
+ //printf("nothing in the vicinity of %d,%d,%d,%d\n", h->bbox);
+ h->seen = 1;
+ }
+ } /*else {
+ printf("area %d,%d,%d,%d is large enough (%dx%d)\n",
+ h->bbox.xmin,
+ h->bbox.ymin,
+ h->bbox.xmax,
+ h->bbox.ymax,
+ h->bbox.xmax - h->bbox.xmin,
+ h->bbox.ymax - h->bbox.ymin);
+ } */
+ }
+ h = next;
+ }
+ } while(changed);
+}
+
+static void display(context_t*context)
+{
+ int width = context->width;
+ int height = context->height;
+ void**group = context->group;