Line rasterisation: Cover all pixels, regardless of line gradient?

前端 未结 6 2089
南笙
南笙 2020-12-25 13:30

Basically, I want to use a line algo to determine which cells to check for collisions for my raycaster.

Bresenham isn\'t great for this as it uses a unified-thicknes

6条回答
  •  Happy的楠姐
    2020-12-25 14:12

    Without loss of generality, assume x2 >= x1, then

    int x = floor(x1);
    int y = floor(y1);
    double slope = (x2 - x1) / (y2 - y1);
    if (y2 >= y1) {
      while (y < y2) {
        int r = floor(slope * (y - y1) + x1);
        do {
          usepixel(x, y);
          ++x;
        } while (x < r);
        usepixel(x, y);
        ++y;
      }
    }
    else {
      while (y > y2) {
        int r = floor(slope * (y - y1) + x1);
        do {
          usepixel(x, y);
          ++x;
        } while (x < r);
        usepixel(x, y);
        --y;
      }
    }
    

    The floor calls can probably be written just as a cast-to-integer.

提交回复
热议问题