Home > Back-end >  Bresenham line algorithm
Bresenham line algorithm

Time:09-26

For help, how to put this code improvement for all the slope of the straight line algorithm?

CodePudding user response:

Void lineBres (int x0, int y0, int xEnd, int yEnd)
{
Int dx=fabs (xEnd - x0), dy=fabs (yEnd - y0);
Int p=2 * dy - dx;
Int twoDy=2 * dy, twoDyMinusDx=2 * (dy dx);
int x, y;
If (x0 & gt; XEnd)
{
X=xEnd;
Y=yEnd;
XEnd=x0;
}
The else
{
X=x0;
Y=y0;
}
GlVertex2i (x, y);
While (x & lt; XEnd)
{
x++;
If (p & lt; 0)
P +=twoDy;
The else
{
Y++;
P +=twoDyMinusDx;
}
GlVertex2i (x, y);
}
}
  • Related