PRESTIGE INSTITUTE OF
MANAGEMENT , GWALIOR
Presentation on line drawing
algorithm
SUBMITTED TO
PROF .SATISH BANSAL
Presented by: S.Mamta
BCA 3rd sem
30
RASTERIZATION
A fundamental computer graphics function
Determine the pixels colors, illuminations, textures, etc.
Implemented by graphics hardware
Rasterization algorithms
Lines
Circles
Triangles
Polygons
LINE DRAWING ALGORITHM
Programmer specifies (x,y) values of end pixels
Need algorithm to figure out which intermediate pixels are on line path
Pixel (x,y) values constrained to integer values
Actual computed intermediate line values may be floats
Rounding may be required. E.g. computed point
(10.48, 20.51) rounded to (10, 21)
Rounded pixel value is off actual line path (jaggy!!)
Sloped lines end up having jaggies
Vertical, horizontal lines, no jaggies
LINE DRAWING ALGORITHM
8
7
6
5
4
3
2
1
Line: (3,2) -> (9,6)
0 1 2 3 4 5 6 7 8 9 10 11 12
Which intermediate
pixels to turn on?
LINE DRAWING ALGORITHM
Slope-intercept line equation
dy y1 y0
dx x1 x0
b y 0 m * x0
y = mx + b
Given two end points (x0,y0), (x1, y1), how to compute m and b?
(x1,y1)
dy
(x0,y0)
dx
LINE DRAWING ALGORITHM
Numerical example of finding slope m:
(Ax, Ay) = (23, 41), (Bx, By) = (125, 96)
By Ay 96 41 55
m
0.5392
Bx Ax 125 23 102
MID POINT ALGORITHM
Which candidate should b selected is based upon mid
point
(Xk+1,Yk+1)
The coordinate of mid point are (Xk+1,Yk+1/2)
Now let h= Bx-Ax ,w=By-Ay
In other word we can says that It satisfy f(x,y)=0
Therefore f(X,Y)=h(X-Ax)-w(Y-By)
Candid
ate
pixel
(Xk+1,
Yk)
Current pixel
Now let f(K) be a decision parameter whose value
will be f(K)=f(Xk+1,Yk+1/2)
If fk<0 ,the mid-point is below the line, so next
pixel(Xk+1,Yk)
If fk>0 , the mid-point is above the line, so next
pixel will be (Xk+1,Yk+1)
Now by updating the equation f(k)= f(Xk+1,Yk+1/2)
h(Xk+1-Ax)-w(Yk+1/2-Ay1)
______(1)
for the next pixel
f(k+1)= f(Xk+2,Yk+1/2)
h(Xk+2-Ax)-w(Yk+1/2-Ay)
If f(k+1)-f(k) =h w(Yk+1-Yk)
f(k+1)=f(k)+h-w(Yk+1-Yk)
therefore, fk <0 , Yk+1=YK ,hence F(k+1)=f(k)+h
fk>=0 , Yk+1=Yk +1 , hence f(k+1)=f(K)+h-w
now f(0)=h-w/2
put k=0 in equation (1) , we get
f(0)= h(x0 +1-Ax)- w(Y0+1/2-Ay)
therefore X0 and Ax both are intial point are same
f0=h-w/2
MID POINT LINE DRAWING ALGORITHM
int h=By-Ay, w=Bx-Ax;
Float f=h-w/2;
Int x=Ax,y=Ay;
For(x=Ax ; x<=Bx ; x++)
{
setpixel(x,y);
if(F<0)
F=F+h;
else
{F=F+h-w;
y++;
}
LINE DRAWING ALGORITHM DRAWBACKS
DDA is the simplest line drawing algorithm
Not very efficient
Round operation is expensive
Bresenham algorithm
Incremental algorithm: current value uses previous value
Integers only: avoid floating point arithmetic
Several versions of algorithm: well describe midpoint version of algorithm
MERITS OF MIDPOINT ALGORITHM
It provides high speed and high accuracy