Computer Garphics
Computer Garphics
Unit 2
Q.> Digitize a Line with end point A(3,2) and B(8,4) , using DDA
Q.>Digitize a Line with end point A(2,6) and B(4,2) , using DDA
+1 +1
+1 +1
+1 +1
Here,
Pk+1- Pk = 2 Δy Xk+1+2 Δy + 2 Δx c - 2 Δx yk+1 – Δx – {2 Δy Xk+2
Δy + 2 Δx c - 2 Δx yk – Δx }
Pk+1 = Pk + 2 Δy
Case 2
If Pk ≥ 0 (i.e. d1-d2 is Positive )
then,
Xk+1 = Xk+1
Yk+1 = Yk+1
Pk+1 = Pk + 2 Δy -2Δx
Asistant Professor Loknath Regmi (CG)
Initial Decision Parameter (P0)
y = m(xk+1) + c
Let, X0 = 0 , Y0 = 0 then C = 0
P0 = 2 Δy - Δx
( ,Yk) ( ,Yk)
+1 +1
+1 +1
+1 +1
Pk+1 = Pk + 2 Δx
Case 2
If Pk ≥ 0 (i.e. d1-d2 is Positive )
then,
Xk+1 = Xk+1
Yk+1 = Yk+1
P = P + 2 Δx -2Δy
Asistant Professor Loknath Regmi (CG)
Initial Decision Parameter (P0)
We Know,
P0 = 2 Δx - Δy
Asistant Professor Loknath Regmi (CG)
Example-4: Digitize the line with end points (1, 0) and (3,
3) using BLA.
Solution :
Here,
Starting point of line = (x1, y1) = (1, 0) and
Ending point of line = (x2, y2) = (3, 3)
Thus,
slope of line, m = Δy / Δx = y2-y1 / x2-x1
= (3-0) / (3-1)
= 3/2
As the given points, it is clear that the line is moving
left to right with the positive slope,
|m| = 1.5 >1 Asistant Professor Loknath Regmi (CG)
Thus, the initial decision parameter
Δx = x2-x1 = 3-1 = 2
(P0) = 2Δx - Δy = 2*2 – 3 = 1 Δy = y2-y1 = 3-0 = 3
we have
If Pk < 0 If Pk ≥ 0
then, then,
Xk+1 = Xk Xk+1 = Xk+1
Yk+1 = Yk+1 Yk+1 = Yk+1
Pk = Pk + 2 Δx Pk = Pk + 2 Δx -2Δy
d2
d2
Y
Y
d1
d1
Pk+1 = Pk + 2 Δy
Case 2
If Pk ≥ 0 (i.e. d1-d2 is Positive )
then,
Xk+1 = Xk - 1
Yk+1 = Yk +1
P = P + 2 Δy -2Δx
Asistant Professor Loknath Regmi (CG)
Initial Decision Parameter (P0)
We Know,
Pk = -2 Δy Xk + 2 Δy + 2 c Δx – 2 Δx Yk - Δx
Let, X0 = 0 , Y0 = 0 then C = 0
Then,
P0 = -2 Δy X0 + 2 Δy + 2 c Δx – 2 Δx Y0 - Δx
P0 = 2 Δy - Δx
Asistant Professor Loknath Regmi (CG)
Slope –Ve and |M| > 1
(Xk-1,Yk+1) (Xk,Yk+1)
(Xk,Yk+1) (Xk-1,Yk+1)
d1 d2 d1
d2
(Xk,Yk) (Xk-1,Yk)
(Xk-1,Yk) X X
(Xk,Yk)
Pk = Pk + 2 Δx
Case 2
If Pk ≥ 0 (i.e. d1-d2 is Positive )
then,
Xk+1 = Xk - 1
Yk+1 = Yk +1
Pk = 2 Δy Xk + 2 Δx Yk + 2 Δx - 2 Δx C - Δy
Let, X0 = 0 , Y0 = 0 then C = 0
Then,
P0 = 2 Δy X0 + 2 Δx Y0 + 2 Δx - 2 Δx C - Δy
P0 = 2 Δx - Δy
Asistant Professor Loknath Regmi (CG)
Example: Digitize the given line endpoints (10, 7) and (5, 10) using Bresenham’s
line drawing algorithm.(TU 2014)
Solution:
Here, (X1,Y1)= (10,7) & Δx = |5-10| = 5
Δy = |10-7| = 3
(X2,Y2)=(5,10)
M= (10-7)/(5-10) = -3/5 –ve slope and |M| < 1 here,
Initial (P0) = 2 Δy – Δx
If Pk < If Pk ≥ 0
Xk+1 = Xk - 1 = 2x3-5
Xk+1 = Xk - 1
Yk+1 = Yk +1 =1
Yk+1 = Yk
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx
(10, 7)
K Pk Xk+1 Yk+1 (Xk+1, Yk+1)
0 1 9 8 (9,8)
1 = 1 + 2x3 - 2x5 =-3 8 8 (8,8)
2 = -3 + 2x3 = 3 7 9 (7,9)
3 = 3 + 2x3 – 2x5 = -1 6 9 (6,9)
4 = -1 + 2x3 = 5 5 10 (5,10)
(6, 2)
K PK Xk+1 Yk+1 (Xk+1 ,Yk+1)
0 -2 6 3 (6,3)
1 =-2+2x3 = 4 5 4 (5,4)
2 =4+6-16=-6 5 5 (5,5)
3 =-6+6=0 4 7 (4,7)
4 =0+6-16=-10 4 8 (4,8)
5 =-10+6= -4 4 9 (4,9)
6 =-4 +6 =2 3 Professor Loknath
Asistant
(3,10)
10 Regmi (CG)
Example: Digitize the given line endpoints (15, 15) and (10, 18) using
Bresenham’s line drawing algorithm.
Solution:
Here, (X1,Y1)= (15,15) & Δx = |10-15| = 5
Δy = |18-15| = 3
(X2,Y2)= (10,18)
M= (18-15)/(10-15) = 3/-5 –ve slope and |M| < 1 here,
Initial (P0) = 2 Δy – Δx
If Pk < If Pk ≥ 0
Xk+1 = Xk - 1 = 2x3-5
Xk+1 = Xk - 1
Yk+1 = Yk +1 =1
Yk+1 = Yk
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx
(15, 15)
K Pk Xk+1 Yk+1 (Xk+1, Yk+1)
0 1 14 16 (14,16)
1 = 1 + 2x3 - 2x5 =-3 13 16 (13,16)
2 = -3 + 2x3 = 3 12 17 (12,17)
3 = 3 + 2x3 – 2x5 = -1 11 17 (11,17)
4 = -1 + 2x3 = 5 10 18 (10,18)
(6, 3)
K PK Xk+1 Yk+1 (Xk+1 ,Yk+1)
0 -1 6 4 (6,4)
1 =-1+2x1 = 1 5 5 (5,5)
2 =1+2x1-2x3=-3 5 6 (5,6)
if Pk < 0 If Pk ≥ 0 If Pk < 0 If Pk ≥ 0
Xk+1 = Xk+1 Xk+1 = Xk+1 Xk+1 = Xk Xk+1 = Xk+1
Yk+1 = Yk Yk+1 = Yk+1 Yk+1 = Yk+1 Yk+1 = Yk+1
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx Pk = Pk + 2 Δx Pk = Pk + 2 Δx -2Δy
P0 = 2 Δy - Δx P0 = 2 Δx - Δy
If Pk < 0 If Pk ≥ 0 If Pk < 0 If Pk ≥ 0
Xk+1 = Xk - 1 Xk+1 = Xk - 1 Xk+1 = Xk Xk+1 = Xk - 1
Yk+1 = Yk Yk+1 = Yk +1 Yk+1 = Yk +1 Yk+1 = Yk +1
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx Pk = Pk + 2 Δx Pk = Pk+ 2Δx - 2 Δy
P0 = 2 Δy - Δx P0 = 2 Δx - Δy
Asistant Professor Loknath Regmi (CG)
Circle Algorithm
What is Circle ?
• Similarly to the case with lines, there is an incremental
algorithm for drawing circles – the mid-point circle algorithm
• In the mid-point circle algorithm we use eight-way symmetry
so only ever calculate the points for the top right eighth of a
circle, and then use symmetry to get the rest of the points
Pk<0
Pk≥0 (Xk+1,Yk-1/2)
(Xk+1,Yk-1)
Pk< 0 Pk ≥ 0
XK+1 = Xk+1 XK+1 = Xk+1
YK+1 = Yk YK+1 = Yk-1
Asistant Professor Loknath Regmi (CG)
Mid Point Circle Algorithm
Case 2: Pk ≥ 0 (Xk+1,Yk-1)
Xk+1 = Xk +1
Yk+1 = Yk - 1
Pk+1 = Pk + 2(xk+1) + [(yk – 1)2 - yk2)] - (yk - 1 - yk) +1
= Pk+2(xk+1) + [(yk 2 - 2yk +1 – yk 2)] - (yk-1- yk) + 1
= Pk+2(xk+1) - 2yk +1 +1 + 1
= Pk+2(xk+1) – 2(yk +1) +1
Asistant Professor Loknath Regmi (CG)
P = P +2X – 2Y +1
Initial decision parameter
(1,r)
(0,r)
i.e.,
(x0,y0) = (0,r)
(1,r-1/2)
Now, (1,r-1)
P0 = 12+(r-1/2 )2 – r2
= 1 + r2 - r + ¼ - r 2
= 5/4 – r
P0 ≈ 1-r
P0 ≈ 1-r
Asistant Professor Loknath Regmi (CG)
Example : Digitize a circle with radius 10 at center (0,0)
Solution
Here, the initial decision parameter (P0) =1 – r = 1-
10 = - 9
Since, for the Midpoint Circle Algorithm of initial
point(0, r) & center at origin (0, 0) rotating at
clockwise direction, we have
Initial point (x0,y0) = (0,10)
P0=1-r = 1-10 = -9
0 -9 0+1 = 1 10 (1,10)
1 = -9 + 2*1 +1= -6 1+2 =2 10 (2, 10)
Pk<0
(Xk-1,Yk-1/2) Pk ≥ 0
(Xk-1,Yk-1)
Pk< 0 Pk ≥ 0
XK+1 = Xk-1 XK+1 = Xk-1
YK+1 = Yk YK+1 = Yk-1
Asistant Professor Loknath Regmi (CG)
Here,
Decision parameter(Pk) = Fcircle (Xk -1,Yk-1/2)
= (Xk -1)2 + (Yk-1/2)2 - r2
Then, K+1th term is,
Pk+1= (Xk+1 -1)2 + (Yk+1 - 1/2)2 - r2
Now,
Pk+1 – Pk = (Xk+1 -1)2 + (Yk+1 - 1/2)2 - r2 – {(Xk -1)2 + (Yk-1/2)2 - r2}
= -2(xk-1)+(Y2k+1-Y2k) – (Yk+1 – Yk) + 1
[ ؞Xk+1 =Xk-1 in both condition ]
Pk+1= Pk-2xk+1+(Y2k+1-Y2k) – (Yk+1 – Yk) + 1
Case 1 : Pk< 0 Case 2: Pk ≥ 0
XK+1 = Xk-1 XK+1 = Xk-1
YK+1 = Yk YK+1 = Yk-1
Pk+1= Pk-2Xk+1 +1 Pk+1= Pk-2Xk+1 – 2Yk+1 +1
Asistant Professor Loknath Regmi (CG)
Initial decision parameter
(-1,r) (0,r)
i.e.,
(x0,y0) = (0,r)
(-1,r-1/2)
Now, (-1,r-1)
P0 = (-1)2+(r-1/2 )2 – r2
= 1 + r2 - r + ¼ - r 2
= 5/4 – r
P0 ≈ 1-r
P0 ≈ 1-r
Asistant Professor Loknath Regmi (CG)
Ellipse
Our approach here is similar to that used in displaying a raster circle but the
ellipse has 4-way symmetry. The midpoint ellipse method is applied
throughout the first quadrant in two parts or region as shown in figure. The
region-1 just behaves as the circular property and the region-2 is slightly
straight curve.
• This occurs because the algorithm checks next pixels both for
boundary color and for fill color.
• Encountering a pixel with the fill color can cause a recursive branch to
terminate, leaving other interior pixels unfilled.