Line Drawing Algorithm not working properly
Posted: Sun Nov 04, 2018 10:13 am
I have used Bresenham's line drawing algorithm to draw lines in my OS. I have encountered the problem of the algorithm not drawing vertical lines. The code does not produce vertical lines nor any lines of a similar angle, it stops after about 135 degrees from the top.
Here is the code:
Can anyone help me with this problem?
Here is the code:
Code: Select all
static void drawline(int x0, int y0, int x1, int y1, int colour) {
int dx, dy, p, x, y, end;
dx=abs(x1-x0);
dy=abs(y1-y0);
if (dy<dx) {p=2*dy-dx;} else {p=2*dx-dy;}
if (x0>x1){
x=x1;
y=y1;
end=x0;
} else {
x=x0;
y=y0;
end=x1;
}
putpixel(x,y,colour);
while(x<=end) {
if (p<0) {
x++;
if (dy<dx) {p=p+2*dy;} else {p=p+2*dx;}
} else {
x++;
y++;
if (dy<dx) {p=p+2*(dy-dx);} else {p=p+2*(dx-dy);}
}
putpixel(x,y,colour);
}
}