Bresenham Algorithms

Graphics Primitives Part II:
Midpoint, Bresenhams line and circle
Midpoint Line Algorithm
 Hitung konstanta
a  y0  y1
b  x1  x0
 Hitung d 0  2a  b
 Untuk setiap iterasi sebanyak ∆x
If d >= 0
x  x 1
d  d  2a
Else if d<0 x  x  1, y  y  1
d  d  2a  2b
Example Midpoint Alg
 Drawing a line from (0,0) to (5.2)
a  y0  y1  2, b  x1  x0  5
d0  2a  b  1, incre _ d1  2a  4, incre _ d2  2(a  b)  6,
i
1
2
3
4
xi
0
1
2
3
yi
0
0
1
1
d
1
-3
3
-1
5 4
2
5
3
2
1
0
1
2
3
4
5
void MidpointLine (int x0,int y0,int xend, int yend,int color)
{ int a, b, incre_d1, incre_d2, d, x, y;
a=y0-yend, b=xend-x0, d=2*a+b;
incre_d1=2*a, incre_d2=2* (a+b);
x=x0, y=y0;
drawpixel(x, y, color);
while (x<x1)
{ if (d<0)
{x++, y++, d+=incre_d2; }
else
{x++, d+=incre_d1;}
drawpixel (x, y, color);
} /* while */
} /* mid PointLine */
Bresenham’s Line Algorithm
 An accurate, efficient raster line drawing algorithm
developed by Bresenham, scan converts lines using only
incremental integer calculations that can be adapted to
display circles and other curves.
Bresenham’s Algorithm
 Hitung konstanta
a  y1  y0
b  x1  x0
 Hitung d 0  2a  b
 Untuk setiap iterasi sebanyak ∆x
If d < 0
Else
x  x 1
d  d  2a
x  x  1, y  y  1
d  d  2a  2b
Example Bresenham
 Drawing a line from (0,0) to (5.2)
a  y1  y0  2, b  x1  x0  5
d 0  2a  b  1, incre _ d1  2a  4, incre _ d 2  2(a  b)  6
i
1
2
3
4
xi
0
1
2
3
yi
0
0
1
1
d
-1
3
-3
1
5 4
2
-5
3
2
1
0
1
2
3
4
5
Bresenham’s Line Algorithm
void BresenhamLine (int x0,int y0,int xend, int yend,int color)
{ int dx,dy, incre_p1, incre_p2, p, x, y;
dy=yend-y0, dx=xend-x0;
incre_p1=2*dy, incre_p2=2* (dy-dx);
x=x0, y=y0;
p=2*dy-dx;
drawpixel(x, y, color);
while (x<x1)
{ if (p<0)
{x++, p+=incre_d1; }
else
{x++, y++,p+=incre_d2;}
drawpixel (x, y, color);
} /* while */
} /* Bresenham */
Midpoint Circle Algorithm
 Inisialisasi
x0
yr
 Hitung d 0 1  r
 Untuk setiap iterasi sebanyak ∆x
If d < 0
Else
x  x 1
d  d  2x  3
x  x  1, y  y  1
d  d  2x  2 y  5
MidPointCircle(int r int color)
{ int x,y;
float d;
x=0; y=r; d=1-r;
circlepoints (x,y,color); //draw (x,y) and other symmetric points
while(x<=y)
{
if(d<0)
d+=2*x+3;
else { d+=2*(x-y)+5; y--;}
x++;
circlepoints (x,y,color);
}
}