Esempio n. 1
0
  /**
   * Bresenham's line algorithm. </ Br> Used to calculate the field of view.</br>
   *
   * @param coordA : starting point of the segment.
   * @param coordB : arrival point of the segment.
   * @return a list of <b>Coordinates</b> composing the segment.
   */
  public ArrayList<Coordinate> drawSegment(Coordinate coordA, Coordinate coordB) {
    // details des coordonnées
    int xA = coordA.getCoordX();
    int yA = coordA.getCoordY();
    int xB = coordB.getCoordX();
    int yB = coordB.getCoordY();
    // tockage des coordonnées intermediaires
    ArrayList<Coordinate> segmentAToB = new ArrayList<Coordinate>();

    int dx, dy, i, xinc, yinc, cumul, x, y;

    x = xA;
    y = yA;
    dx = xB - xA;
    dy = yB - yA;
    xinc = (dx > 0) ? 1 : -1;
    yinc = (dy > 0) ? 1 : -1;
    dx = Math.abs(dx);
    dy = Math.abs(dy);

    if (dx > dy) {
      cumul = dx / 2;
      for (i = 1; i <= dx; i++) {
        if (grid.isOpaque(new Coordinate(x, y))) break;
        x = x + xinc;
        cumul += dy;
        if (cumul >= dx) {
          cumul = cumul - dx;
          y = y + yinc;
        }
        segmentAToB.add(new Coordinate(x, y));
      }
    } else {
      cumul = dy / 2;
      for (i = 1; i <= dy; i++) {
        if (grid.isOpaque(new Coordinate(x, y))) break;
        y = y + yinc;
        cumul = cumul + dx;
        if (cumul >= dy) {
          cumul -= dy;
          x = x + xinc;
        }
        segmentAToB.add(new Coordinate(x, y));
      }
    }
    return segmentAToB;
  }