srecord noeq FindRegionOutline(BWImage_FastRegions regions, int iRegion) { gettable new PtBuffer outline; int x, y, dx, dy; int polarity; run { Pt p = regions.samplePixel(iRegion); x = p.x; y = p.y; // move up until we're at the edge of the region // probably we don't actually need to do this because of the way // the image was scanned, but we'll do it anyway while (y > 0 && regions.inRegion(iRegion, x, y-1)) y--; // found our first outline pixel with background above outline.add(x, y); // remember first point firstX = x; firstY = y; // start searching to the right dx = 1; dy = 0; int safety = (regions.getWidth()+2)*(regions.getHeight()+2); do { if (!findNextDirection()) break; } while (x != firstX && y != firstY && l(outline) < safety); } void findNextDirection { // try directions (left, forward, right, turn around) Pt dir = turnLeft(dx, dy); repeat 4 { if (inRegion(x+dir.x, y+dir.y)) { dx = dir.x; dy = dir.y; true; } } false; } // forward? Pt left = turnLeft(dx, dy); if (inRegion(x+left.x, y+left.y)) { dx = left.x; dy = left.y; } } }