Exemplo n.º 1
0
 private static void circlePointImpl(int x, int y, int x0, int y0, PointPredicate op) {
   op.on(x + x0, y + y0);
   op.on(y + x0, x + y0);
   op.on(-x + x0, y + y0);
   op.on(-y + x0, x + y0);
   op.on(-x + x0, -y + y0);
   op.on(-y + x0, -x + y0);
   op.on(x + x0, -y + y0);
   op.on(y + x0, -x + y0);
 }
Exemplo n.º 2
0
  /** Pseudo-recursive generic flood fill algorithm */
  public static void fill(int x, int y, PointPredicate op) {
    // using a Deque (stack) to simulate recursion
    Deque<Point> points = new ArrayDeque<>();
    points.push(new Point(x, y));

    while (points.size() != 0) {
      Point p = points.pop();

      if (op.on(p.x, p.y + 1)) points.push(new Point(p.x, p.y + 1));
      if (op.on(p.x, p.y - 1)) points.push(new Point(p.x, p.y - 1));
      if (op.on(p.x + 1, p.y)) points.push(new Point(p.x + 1, p.y));
      if (op.on(p.x - 1, p.y)) points.push(new Point(p.x - 1, p.y));
    }
  }
Exemplo n.º 3
0
  /** A version of line() that doesn't check to stop if it's reached the end point */
  public static void ray(int x1, int y1, int x2, int y2, PointPredicate op) {
    if (x1 == x2 && y1 == y2) return; // avoid infinite loop if starting position is ending position

    int sx, sy, e2;
    int dx = Math.abs(x2 - x1);
    int dy =
        -Math.abs(
            y2 - y1); // we calculate -dx in the first place to possibly save a clock cycle down
    // there

    sx = x1 < x2 ? 1 : -1;
    sy = y1 < y2 ? 1 : -1;

    int err = dx + dy;

    while (true) {
      if (!op.on(x1, y1)) return;
      e2 = 2 * err;
      if (e2 > dy) { // down here is where I mean
        err += dy;
        x1 += sx;
      }
      if (e2 < dx) {
        err += dx;
        y1 += sy;
      }
    }
  }
Exemplo n.º 4
0
  /**
   * Traces along a line. Source:
   * http://en.wikipedia.org/wiki/Bresenham's_line_algorithm#Simplification
   *
   * @return true if the end of the line is reached, false if it is stopped partway
   */
  public static boolean line(int x1, int y1, int x2, int y2, PointPredicate op) {
    int sx, sy, e2;
    int dx = Math.abs(x2 - x1);
    int dy =
        -Math.abs(
            y2 - y1); // we calculate -dx in the first place to possibly save a clock cycle down
    // there

    sx = x1 < x2 ? 1 : -1;
    sy = y1 < y2 ? 1 : -1;

    int err = dx + dy;

    while (true) {
      if (!op.on(x1, y1)) return false;
      if (x1 == x2 && y1 == y2) return true;
      e2 = 2 * err;
      if (e2 > dy) { // down here is where I mean
        err += dy;
        x1 += sx;
      }
      if (e2 < dx) {
        err += dx;
        y1 += sy;
      }
    }
  }