Computer Graphic - Lines, Circles and Ellipse

Post on 11-Apr-2017

58 views 0 download

Transcript of Computer Graphic - Lines, Circles and Ellipse

COMPUTER GRAPHICSCH02 – LINES, CIRCLES AND ELLIPSE

BRESENHAM’S LINE DRAWING ALGORITHM For |m|<11. Input 2 line end points and store the left end

point in (x0,y0).2. Load (x0,y0) into the frame buffer to plot the first

end point.3. Calculate constants ∆x, ∆y, and 2∆y - 2∆x and

2∆y and obtain the storing value for the decision parameter as : P0 = 2∆y - ∆x.

4. At each xk along the line, starting at k = 0, do the following test: if pk < 0, the next point to plot is (xk+1, yk) and pk+1 = pk + 2∆y else, the next point to plot is (xk+1, yk+1) and pk+1 = pk + 2∆y - 2∆x

5. Repeat step 4 ∆x times.

EXAMPLE 1 PART 1 Suppose we have the line with the 2 following end points (20,10) and (30,18) use line drawing algorithm to draw this line.m=∆y / ∆x = 8/10 < 1.Initial point : (20,10)∆x = 10∆y = 82∆y - 2∆x = -42∆y = 16Initial decision parameter P0 = 2∆y - ∆x =16 – 10 =

6

(30,18)

(20,10)

EXAMPLE 1 PART 2k pk (xk+1,yk+1)0 6 (21,11)1 6-4 = 2 (22,12)2 18-20 = -2 (23,12)3 -2+16 = 14 (24,13)4 14-4 = 10 (25,14)5 6 (26,15)6 2 (27,16)7 -2 (28,16)8 14 (29,17)9 10 (30,18)

BRESENHAM’S LINE DRAWING ALGORITHM For |m|>=1 Interchange the values of x and y (y is always increasing in the table and the decision parameter become :

P0 = 2∆x - ∆y and pk+1 = pk + 2∆x - 2∆y or pk + 2∆x .

EXAMPLE 2 PART 1 Suppose we have the line with the 2 following end points (1,3) and (7,12) use line drawing algorithm to draw this line.m=∆y / ∆x = 9/6 > 1.Initial point : (1,3)∆x = 6∆y = 92∆x - 2∆y = -62∆x = 12Initial decision parameter P0 = 2∆x - ∆y = 3

(7,2)

(1,3)

EXAMPLE 2 PART 2k pk (xk+1,yk+1)0 3 (2,4)1 (3+12-18) = -3 (2,5)2 9 (3,6)3 3 (4,7)4 -3 (4,8)5 9 (5,9)6 3 (6,10)7 -3 (6,11)8 9 (7,12)

EXAMPLE 3 PART 1 Suppose we have the line with the 2 following end points (5,7) and (9,19) use line drawing algorithm to draw this line.m=∆y / ∆x = 12/4 > 1.Initial point : (5,7)∆x = 4∆y = 122∆x - 2∆y = -162∆x = 8Initial decision parameter P0 = 2∆x - ∆y = -4

(9,19)

(5,7)

EXAMPLE 3 PART 2k pk (xk+1,yk+1)0 -4 (5,8)1 4 (6,9)2 -12 (6,10)3 -4 (6,11)4 4 (7,12)5 -12 (7,13)6 -4 (7,14)7 4 (8,15)8 -12 (8,16)9 -4 (8,17)10 4 (9,18)11 -12 (9,19)

EXAMPLE 4 PART 1 Suppose we have the line with the 2 following end points (2,6) and (8,7) use line drawing algorithm to draw this line.m=∆y / ∆x = 1/6 < 1.Initial point : (2,6)∆x = 6∆y = 12∆y - 2∆x = -102∆y = 2Initial decision parameter P0 = 2∆y - ∆x = -4

(8,7)

(2,6)

EXAMPLE 4 PART 2k pk (xk+1,yk+1)0 -4 (3,6)1 -2 (4,6)2 0 (5,7)3 -10 (6,7)4 -8 (7,7)5 -6 (8,7)

MIDPOINT CIRCLE ALGORITHM1. Input radius r and circle center (xi,yi) and

obtain the first point on the circumference of the circle centered on the origin as (0,r).

2. Calculate the initial value of decision parameter as p0 = 5/4 – r (if its integer 1-r).

3. At each xk position starting at k=0, perform the following test: If pk < 0 then the next point on circle centered on (0,0) is (xk+1,yk) and pk+1 = pk+2xk+1+1 else the next point along the circle centered on (0,0) is (xk+1,yk-1) and pk+1 = pk+2xk+1+1-2yk+1

4. Repeat step 3 until x>=y.

EXAMPLE 1 Given r=10 use midpoint circle algorithm to draw circle.Initial point : (0,10)Initial decision parameter P0 = 1-r = -9

k pk (xk+1,yk+1) 2xk+1 2yk+1

0 -9 (1,10) 2 201 -6 (2,10) 4 202 -1 (3,10) 6 203 6 (4,9) 8 184 -3 (5,9) 10 185 8 (6,8) 12 166 5 (7,7) Stop 14 14

EXAMPLE 2 Given r=14 use midpoint circle algorithm to draw circle.Initial point : (0,14) P0 = 1-r = -13k pk (xk+1,yk+1) 2xk+1 2yk+1

0 -13 (1,14) 2 281 -10 (2,14) 4 282 -5 (3,14) 6 283 2 (4,13) 8 264 -15 (5,13) 10 265 -4 (6,13) 12 266 9 (7,12) 14 247 0 (8,11) 16 228 -5 (9,11) 18 229 14 (10,10)

MIDPOINT ELLIPSE DRAWING1. Input rx, ry and ellipse center (xc,yc) and obtain

the first point on an ellipse centered (0,0) on the origins as (x0,y0) = (0,ry).

2. Calculate the initial value of decision parameter in region 1 as p10 = ry

2 - rx

2 ry + ¼ rx

2.3. At each xk position in region 1 starting at k=0,

perform the following test: If p1k < 0 then the next point along the ellipse centered on (0,0) is (xk+1,yk) and p1k+1 = p1k+2ry

2xk+1+ry2 else the

next point along the ellipse centered on (0,0) is (xk+1,yk-1) and p1k+1 = p1k+2ry

2xk+1+ry2-2rx

2yk+1.

4. Repeat the steps for region 1 until 2ry2

xk+1>= 2rx

2 yk+1

MIDPOINT ELLIPSE DRAWING4. Calculate the initial value of decision

parameter in region 2 using the last point (x0,y0) calculated in region 1 as p20 = ry

2 (x0+½)2+rx

2 (y0-1)2-rx2ry

2

5. At each yk position in region 2 starting at k=0, perform the following test: If p2k >= 0 then the next point along the ellipse centered on (0,0) is (xk,yk-1) and p2k+1 = p2k-2rx

2yk+1+rx2

else the next point along the ellipse centered on (0,0) is (xk+1,yk+1) and p2k+1 = p2k+2ry

2xk+1-2rx

2yk+1+rx2. (Note: Use the same increment

value for x and y in region 1.)6. Repeat the steps for region 2 until yk+1=0.

EXAMPLE 1 PART 1 Given ellipse parameter with rx=8 and ry=6 use midpoint ellipse drawing to draw an ellipse.Initial point for region 1 (0,ry)=(0,6).2ry

2x= 0 (with increment value 2ry2 = 2(6)2=72)

2rx2y= 2rx

2ry= 2(8)2(6) (with increment value –rx

2=-2(8)2=-128) The initial decision parameter for region 1

p10 = ry2 - rx

2 ry + ¼ rx

2 =(6)2 - (8)2(6) + ¼(8)2 =-332

EXAMPLE 1 PART 2k pk (xk+1,yk+1) 2ry

2 xk+1 2rx

2 yk+1

0 -332 (1,6) 72 7681 -224 (2,6) 144 7682 -44 (3,6) 216 7683 208 (4,5) 288 6404 -108 (5,5) 360 6405 288 (6,4) 432 5126 244 (7,3) 504 384

Stop since 2ry2 xk+1 > 2rx

2 yk+1

EXAMPLE 1 PART 3We now move to region 2Initial point for region 2 (x0,y0)=(7,3).The initial decision parameter for region 2

p20 = ry2 (x0+½)2+rx

2 (y0-1)2-rx2ry

2 =36(7.5)2+64(2)2-64*36= -151

k pk (xk+1,yk+1) 2ry2

xk+1 2rx2

yk+1

0 -151 (8,2) 576 2561 233 (8,1) 576 1282 745 (8,0) 576 0

EXAMPLE 2 PART 1 Given ellipse parameter with rx=8 and ry=10 use midpoint ellipse drawing to draw an ellipse.Initial point for region 1 (0,ry)=(0,10).2ry

2x= 0 (with increment value 2ry2 = 2(10)2=200)

2rx2y= 2rx

2ry= 2(8)2(10) (with increment value –rx

2=-2(8)2=-128) The initial decision parameter for region 1

p10 = ry2 - rx

2 ry + ¼ rx

2

=(10)2 - (8)2(10) + ¼(8)2 =-524

EXAMPLE 2 PART 2k pk (xk+1,yk+1) 2ry

2 xk+1 2rx

2 yk+1

0 -524 (1,10) 200 12801 -224 (2,10) 400 12802 276 (3,9) 600 11523 -176 (4,9) 800 11524 724 (5,8) 1000 10245 800 (6,7) 1200 896

EXAMPLE 2 PART 3We now move to region 2Initial point for region 2 (x0,y0)=(6,7).The initial decision parameter for region 2

p20 = ry2 (x0+½)2+rx

2 (y0-1)2-rx2ry

2 =100(6.5)2+64(6)2-64*100= 129

k pk (xk+1,yk+1) 2ry2

xk+1 2rx2

yk+1

0 129 (6,6) 1200 7681 -703 (7,5) 1400 640

( ,0) Stop