0% found this document useful (0 votes)
10 views

334csm Chapter3 Exercise Solution

Uploaded by

saraalshreef609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

334csm Chapter3 Exercise Solution

Uploaded by

saraalshreef609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Chapter 3 (Graphics Output Primitives) Exercise Solutions

LINE AND CIRCLE DRAWING ALGORITHM (DDA, BRESENHAM, MIDPOINT)

EXERCISE 1:
Consider the line from (0,0) to (3,3). Use DDA algorithm to find the co-ordinates of this line.

SOLUTION:
dx = 3-0 = 3
dy = 3-0 = 3
Steps = Max(dx,dy) = Max(3,3) = 3
Xinc = float(dx) / float(steps) = 3/3 = 1
Yinc = float(dy) / float(steps) = 3/3 = 1
Initially X=0 Y=0
For (K=0; K<steps; K++)
K X = X+Xinc Y = Y + Yinc Value Computed
0 0+1= 1 0+1=1 1, 1
1 1+1=2 1+1=2 2, 2
2 2+1=3 2+1=3 3, 3

8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
EXERCISE 2:
Consider the line from (6,9) to (11,12). Use DDA algorithm to find the co-ordinates of this line.
SOLUTION:

17
16
8
15
7
14
13
12
11
10
9
6 7 8 9 10 11 12 13
EXERCISE 3:

Apply Bresenham’s Line Drawing algorithm to find the points of the line between the end
points, (2,2) and (10,10).

1. Plot (2,2)
2. Δx = 10 – 2 = 8 Δy = 10 – 2 = 8
2Δy = 2 * 8 = 16
2Δy-2Δx = 16 – 16 = 0

3. P0 = 2Δy- Δx
= 16- 8 = 8
If Pk < 0 Plot (Xk+1,Yk) and Pk+ 1 = Pk + 2Δy
else Plot (Xk+1,Yk+1) and Pk+ 1 = Pk + 2Δy - 2Δx

Δx-1 Steps  8-1 = 7

K Pk Pk <0 (Xk+1,Yk+1) Pk+1


0 8 8 < 0, No 3,3 Pk+ 1 = Pk + 2Δy - 2Δx 8 + 0 = 8
1 8 8 < 0, No 4,4 Pk+ 1 = Pk + 2Δy - 2Δx  8 + 0 = 8
2 8 8 < 0, No 5,5 Pk+ 1 = Pk + 2Δy - 2Δx  8 + 0 = 8
3 8 8 < 0, No 6,6 Pk+ 1 = Pk + 2Δy - 2Δx  8 + 0 = 8
4 8 8 < 0, No 7,7 Pk+ 1 = Pk + 2Δy - 2Δx  8 + 0 = 8
5 8 8 < 0, No 8,8 Pk+ 1 = Pk + 2Δy - 2Δx  8 + 0 = 8
6 8 8 < 0, No 9,9 Pk+ 1 = Pk + 2Δy - 2Δx  8 + 0 = 8
7 8 8 < 0, No 10,10 Pk+ 1 = Pk + 2Δy - 2Δx  8 + 0 = 8

11
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10
EXERCISE 4:

Digitize a line from (10,12) to (15,15) on a raster screen using Bresenhams straight line
algorithm.
1. Plot (10,12)
2. Δx = 15 – 10 = 5 Δy = 15 – 12 = 3
2Δy = 2 * 3 = 6
2Δy-2Δx = 6 – 10 = -4

3. P0 = 2Δy- Δx
= 6-5 = 1
If Pk < 0 Plot (Xk+1,Yk) and Pk+ 1 = Pk + 2Δy
else Plot (Xk+1,Yk+1) and Pk+ 1 = Pk + 2Δy - 2Δx

Δx-1 Steps  5-1 = 4

K Pk Pk <0 (Xk+1,Yk+1) Pk+1


10,12
0 1 1 < 0, No 11,13 Pk+ 1 = Pk + 2Δy - 2Δx 1 + (-4) = -3
1 -3 -3 < 0, Yes 12,13 Pk+ 1 = Pk + 2Δy  -3 + 6 = 3
2 3 3 < 0, No 13,14 Pk+ 1 = Pk + 2Δy - 2Δx  3 + (-4) = -1
3 -1 -1 < 0, Yes 14,14 Pk+ 1 = Pk + 2Δy  -1 + 6 = 5
4 5 5 < 0, No 15,15 Pk+ 1 = Pk + 2Δy - 2Δx  5 + (-4) = 1

21
20
1
19
18
17
16
15
14
13
12
11
10
10 11 12 13 14 15 16 17 18 19 20 21
EXERCISE 5:
Given the circle radius r=20, Demonstrate the midpoint circle algorithm by determining the
circle points along the circle octant in the first quadrant from x=0 to x=y.
SOLUTION:

Value of the decision parameter is P0 = 1 – r


P0= 1-20 = -19

For the circle centred on the origin, the initial point is (x0, y0) = (0,r)  (0, 20), and the initial
increment term for calculating the decision parameters are

Successive decision parameters values and positions along the circle path are calculated using
midpoint algorithm as follows:

If Pk < 0 Plot (Xk+1,Yk) and Pk+ 1 = Pk + 2Xk+1 +1


else Plot (Xk+1,Yk-1) and Pk+ 1 = Pk + 2Xk+1 +1- 2Yk+1

K Pk Pk <0 (Xk-1,Yk+1) 2Xk+1 2Yk+1 Pk+1


(0,20)
0 -19 -19 <0, Yes (1,20) 2 40 Pk + 2Xk+1 +1  (-19) + 2 + 1 = -16
1 -16 -16 <0, Yes (2,20) 4 40 Pk + 2Xk+1 +1  (-16) + 4 + 1 = -11
2 -11 -11 <0, Yes (3,20) 6 40 Pk + 2Xk+1 +1 (-11) + 6 +1 = -4
3 -4 -4 <0, Yes (4,20) 8 40 Pk + 2Xk+1 +1 (-4) + 8 + 1 = 5
4 5 5 <0, No (5,19) 10 38 Pk + 2Xk+1 +1- 2Yk+15 + 10 +1 -38 = -22
5 -22 -22 <0, Yes (6,19) 12 38 Pk + 2Xk+1 +1 (-22) + 12 + 1 = -9
6 -9 -9 <0, Yes (7,19) 14 38 Pk + 2Xk+1 +1 (-9) + 19 + 1 = 6
7 6 6 <0, No (8,18) 16 36 Pk + 2Xk+1 +1- 2Yk+1 6 + 16 +1 -36 = -13
8 -14 -14 <0, Yes (9,18) 18 36 Pk + 2Xk+1 +1 (-13) + 18 + 1 = 6
9 5 5 <0, No (10,17) 20 34 Pk + 2Xk+1 +1- 2Yk+16 + 20 + 1 -34 = -7
10 -8 -8 <0, Yes (11,17) 22 34 Pk + 2Xk+1 +1 (-7) + 22 + 1 = 16
11 15 15 <0, No (12,16) 24 32 Pk + 2Xk+1 +1- 2Yk+1 16 + 24 + 1 -32 = 9
12 8 8 <0, No (13,15) 26 30 Pk + 2Xk+1 +1- 2Yk+19 + 26 =1 -30 = 6
13 5 5 <0, No (14,14) 28 28

20
19
18
17
16
15
14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
EXERCISE 6: Given the circle radius r=7, Demonstrate the midpoint circle algorithm by
determining the circle points along all the octant(Complete circle).

SOLUTION:
Value of the decision parameter is P0 = 1 – r
P0= 1-7 = -6

For the circle centered on the origin, the initial point is (x0, y0) = (0,r)  (0, 7), and the initial
increment term for calculating the decision parameters are

Successive decision parameters values and positions along the circle path are calculated using
midpoint algorithm as follows:

K Pk Pk <0 ? (Xk+1,Yk+1) 2Xk+1 2Yk+1 Pk+1


(0,7)
0 -6 Yes (1,7) 2 14 Pk + 2Xk+1 +1  (-6) + 2 + 1 = -3
2 -3 Yes (2,7) 4 14 Pk + 2Xk+1 +1 (-3) + 4 +1 = 2
3 2 No (3,6) 6 12 Pk + 2Xk+1 +1- 2Yk+12 + 6 +1 -12 = -3
4 -3 Yes (4,6) 8 12 Pk + 2Xk+1 +1 (-3) + 8 +1 = 6
5 6 No (5,5)
(0,7) (1,7) (2,7) (3,6) (4,6) (5,5)
X,Y (0,7) (1,7) (2,7) (3,6) (4,6) (5,5)
Y,X (7,0) (7,1) (7,2) (6,3) (6,4) (5,5)
-Y,X (-7,0) (-7,1) (-7,2) (-6,3) (-6,4) (-5,5)
-X,Y (0, 7) (-1,7) (-2,7) (-3,6) (-4,6) (-5,5)
-X,-Y (0,-7) (-1,-7) (-2,-7) (-3,-6) (-4,-6) (-5,-5)
-Y,-X (-7,0) (-7,-1) (-7,-2) (-6,-3) (-6,-4) (-5,-5)
Y,-X (7,0) (7,-1) (7,-2) (6,-3) (6,-4) (5,-5)
X,-Y (0,-7) (1,-7) (2,-7) (3,-6) (4,-6) (5,-5)

20
19
18
17
16
15
14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
EXERCISE 7:
If (x,y)=(3,6) is a co-ordinate of a circle in an octant of first quadrant, find the other circle points
in remaining seven octants using the Symmetric property of circle.

SOLUTION:

(x,y)=(3,6)
Other points are (6,3), (3,-6) , (6,-3), (-6, -3), (-3,-6), (-3,6), (-6,3)
EXERCISE 8:
In Midpoint Circle Generation algorithm If Decision parameter Po at the start position is (0,r).
Prove Po = 1-r.

Po = (0,r)  x = 0, y= r

Po = fcirc 0+1, r - 1
2

= fcirc 1, r - 1
2

=12+r–1 2
– r2
2

= 1 + r 2 + 1 – 2 r * 1 – r2
4 2

= 1 + r2 + 1 – 2r – r2
4 2

=1+1–r
4
= 5–r
4

=1-r

You might also like