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

前端 未结 6 2100
南笙
南笙 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条回答
  •  悲哀的现实
    2020-12-25 14:30

    This thread old, but I thought it'd be worth putting this on the Internet:

    // This prints the pixels from (x, y), increasing by dx and dy.
    // Based on the DDA algorithm (uses floating point calculations).
    void pixelsAfter(int x, int y, int dx, int dy)
    {
        // Do not count pixels |dx|==|dy| diagonals twice:
        int steps = Math.abs(dx) == Math.abs(dy)
                ? Math.abs(dx) : Math.abs(dx) + Math.abs(dy);
        double xPos = x;
        double yPos = y;
        double incX = (dx + 0.0d) / steps;
        double incY = (dy + 0.0d) / steps;
        System.out.println(String.format("The pixels after (%d,%d) are:", x, y));
        for(int k = 0; k < steps; k++)
        {
            xPos += incX;
            yPos += incY;
            System.out.println(String.format("A pixel (%d) after is (%d, %d)",
                k + 1, (int)Math.floor(xPos), (int)Math.floor(yPos)));
        }
    }
    

提交回复
热议问题