# Bresenham algorithm

Printable View

• May 19th, 2013, 09:11 AM
killbill689
Bresenham algorithm
I use bresenhams line algorithm to draw a line.Sometimes when the slope is close to 0.5 the line isn't drawing correctly.Some parts of the line are thicker than other or the line that is drawn looks like a triangle.The following code is for the first and fourth octant.
Why this is happening?
Code:

``` public void        draw_line(Graphics g,int x1,int y1,int xn,int yn)         {                 int error,dx,dy,y,x;                                         dx=xn-x1;                 dy=yn-y1;                                 y=y1;                 x=x1;                 //the octant of the line                 if(slope==1||slope==4)                 {                         error=-dx/2;                 for( x=x1;x<=xn;x++)                 {                          PutPixel(g,x,y);                         error=error+dy;                 if(error>=0)                 {y++;                 error=error-dx;}}}```