static bool majority(Bitmap_p bm1, int x, int y) { int i; int a; int ct; for (i = 2; i < 5; i++) { ct = 0; for (a = -i + 1; a <= i - 1; a++) { ct += bm1.at(x + a, y + i - 1) ? 1 : -1; ct += bm1.at(x + i - 1, y + a - 1) ? 1 : -1; ct += bm1.at(x + a - 1, y - i) ? 1 : -1; ct += bm1.at(x - i, y + a) ? 1 : -1; } if (ct > 0) { return(true); } else if (ct < 0) { return(false); } } return(false); }
/// <summary> /// Compute a path in the binary matrix. /// Start path at the point (x0,x1), which must be an upper left corner /// of the path. Also compute the area enclosed by the path. Return a /// new path_t object, or NULL on error (note that a legitimate path /// cannot have length 0). /// </summary> /// <param name="Matrix">Binary Matrix</param> /// <returns></returns> /// <param name="x">x index in the source Matrix</param> /// <param name="y">y index in the source Matrix</param> Path findPath(Bitmap_p bm1, Point point) { Path path = new Path(); int x = point.X; int y = point.Y; int dirx = 0; int diry = 1; int tmp = -1; path.sign = bm.at(point.X, point.Y) ? "+" : "-"; while (true) { path.pt.Add(new Point(x, y)); if (x > path.maxX) { path.maxX = x; } if (x < path.minX) { path.minX = x; } if (y > path.maxY) { path.maxY = y; } if (y < path.minY) { path.minY = y; } path.len++; x += dirx; y += diry; path.area -= x * diry; if (x == point.X && y == point.Y) { break; } bool l = bm1.at(x + (dirx + diry - 1) / 2, y + (diry - dirx - 1) / 2); bool r = bm1.at(x + (dirx - diry - 1) / 2, y + (diry + dirx - 1) / 2); if (r && !l) { if ((turnpolicy == TurnPolicy.right) || (((turnpolicy == TurnPolicy.black) && (path.sign == "+"))) || (((turnpolicy == TurnPolicy.white) && (path.sign == "-"))) || (((turnpolicy == TurnPolicy.majority) && (majority(bm1, x, y)))) || ((turnpolicy == TurnPolicy.minority && !majority(bm1, x, y)))) { tmp = dirx; dirx = -diry; diry = tmp; } else { tmp = dirx; dirx = diry; diry = -tmp; } } else if (r) { tmp = dirx; dirx = -diry; diry = tmp; } else if (!l) { tmp = dirx; dirx = diry; diry = -tmp; } } return(path); }