L 9 Line Drawing Algorithims 1 2
L 9 Line Drawing Algorithims 1 2
Given coordinate of two points A(x1, y1) and B(x2, y2) such that x1 < x2
and y1 < y2. The task to find all the intermediate points required for drawing line AB
on the computer screen of pixels. Note that every pixel has integer coordinates.
for any given/calculated previous pixel P(Xk, Yk), there are two candidates for the
next pixel closest to the line, Right(Xk+1, Yk) and Left(Xk+1, Yk+1)
In Mid-Point algorithm we do following.
Find middle of two possible next points. Middle of R(Xk+1, Yk) and
L(Xk+1, Yk+1) is M(Xk+1, Yk+1/2).
If M is above the line, then choose R as next point.
If M is below the line, then choose L as next point.
1
Let us consider a line y = mx + B.
We can re-write the equation as :
y = (dy/dx)x + B or
(dy)x + B(dx) - y(dx) = 0
Let F(x, y) = (dy)x - y(dx) + B(dx) -----(1)
Let we are given two end points of a line (under above assumptions)
• For all points (x,y) on the line,
the solution to F(x, y) is 0.
• For all points (x,y) above the line,
F(x, y) result in a negative number.
• And for all points (x,y) below the line,
F(x, y) result in a positive number.
3
Case 2: If L is chosen then for next point :
dk+1 = F(Xp+2, Yp+3/2)
= a(Xp+2) + b(Yp+3/2) + c
dk = a(Xp+1) + b(Yp+1/2) + c
Difference (Or delta) of two distances:
Δd = dk+1 – dk
= a(Xp+2)- a(Xp+1)+ b(Yp+3/2)- b(Yp+1/2)+ c-c
=a+b
Therefore, dk+1 = dk + dy - dx. (as a = dy, b= -dx)
Calculation For initial value of decision parameter d0:
d0 = F(X1+1 , Y1+1/2)
= a(X1 + 1) + b(Y1 + 1/2) +c
= aX1+ bY1 + c + a + b/2
= F(X1,Y1) + a + b/2
= a + b/2 (as F(X1, Y1) is on the circle so = 0 )
d0 = dy – dx/2. (as a = dy, b = -dx)
4
PRACTICE PROBLEMS BASED ON MID POINT LINE DRAWING ALGORITHM-
Problem-01: Calculate the points between the starting coordinates (20,
10) and ending coordinates (30, 18).
Solution- Given-
Starting coordinates = (X0, Y0) = (20, 10)
Ending coordinates = (Xn, Yn) = (30, 18)
Step-01:
Calculate ΔX and ΔY
ΔX = Xn – X0 = 30 – 20 = 10
ΔY =Yn – Y0 = 18 – 10 = 8
Step-02:
Calculate dinitial -
do = dy - dx/2 = 8 – 10/2 = 3
Step-03:
As dinitial >= 0, so case-02 is satisfied than dk+1 = dk + dy – dx = 1
Thus,
Xk+1 = Xk + 1 = 20 + 1 = 21
Yk+1 = Yk + 1 = 10 + 1 = 11 5
As dinitial < 0, so case-01 is satisfied than
Dinitia
dk+1 = dk+ dy. Dnew Xk+1 Yk+1
l
Thus,
Xk+1 = Xk + 1 20 10
Yk+1 = Yk 3 1 21 11
7
Problem-02:
Calculate the points between the starting coordinates (5, 9) and ending
coordinates (12, 16)
Step-01:
Calculate ΔX and ΔY.
ΔX = Xn – X0 = 12 – 5 = 7
ΔY =Yn – Y0 = 16 – 9 = 7
Step-02:
Calculate dinitial
do = dy - dx/2 = 7 – 7/2 = 3.5
Step-03:
As dinitial >= 0, so case-02 is satisfied than dk+1 = dk + dy – dx = 3.5
Thus,
Xk+1 = Xk + 1 = 5 + 1 = 6
Yk+1 = Yk + 1 = 9 + 1 = 10
8
Dinitial Dnew Xk+1 Yk+1
5 9
3.5 3.5 6 10
3.5 3.5 7 11
3.5 3.5 8 12
3.5 3.5 9 13
3.5 3.5 10 14
3.5 3.5 11 15
3.5 12 16