20
Drawing Algorithms Computer Graphics Spring 2017-2018 CS4815

Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

  • Upload
    trantu

  • View
    219

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms

Computer Graphics

Spring 2017-2018

CS4815

Page 2: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms

Outline

1 Drawing AlgorithmsCircle Drawing Algorithms; §6-4

CS4815

Page 3: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Outline

1 Drawing AlgorithmsCircle Drawing Algorithms; §6-4

CS4815

Page 4: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Equation of a circle

With respect to their centre thepoints of a circle must satisfy thePythagorean theorem:A2 + B2 = C2

For a circle of radius r centred at(xc , yc) this means

(x − xc)2 + (y − yc)

2 = r2

If we’re sampling in terms of xthen

y = yc ±√

r2 − (x − xc)2

This leads to “uneven” samplingand is expensive

r

x,y

yc

cx

CS4815

Page 5: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Parametric equation of a circle

We want the perimeter of the circle to look as even aspossible so it would be better to sample along theperimeter, rather than using x coordinates for samplingFor circle of radius r centred at (xc , yc) circle comprises

x = xc + r cos θy = yc + r sin θ

By sampling at increments of θ we can get a smootherlooking curveWhat is an appropriate value of θ to use?The curvature of a circle is given by 1

r and this is anappropriate value for θ

CS4815

Page 6: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Using symmetry

Computing trigonometrical functions inthe θ approach previously is stillexpensiveDo we have to compute pixels overentire circle (360◦)?From (x , y) we can get (±x ,±y) for freeBut also (y , x) and ...So we only need to compute points inone octantTo keep slope |m| < 1 and forincreasing x , the octant we choose issecond

CS4815

Page 7: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Using symmetry

Computing trigonometrical functions inthe θ approach previously is stillexpensiveDo we have to compute pixels overentire circle (360◦)?From (x , y) we can get (±x ,±y) for freeBut also (y , x) and ...So we only need to compute points inone octantTo keep slope |m| < 1 and forincreasing x , the octant we choose issecond

CS4815

Page 8: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Using symmetry

Computing trigonometrical functions inthe θ approach previously is stillexpensiveDo we have to compute pixels overentire circle (360◦)?From (x , y) we can get (±x ,±y) for freeBut also (y , x) and ...So we only need to compute points inone octantTo keep slope |m| < 1 and forincreasing x , the octant we choose issecond

(x , y)

CS4815

Page 9: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Using symmetry

Computing trigonometrical functions inthe θ approach previously is stillexpensiveDo we have to compute pixels overentire circle (360◦)?From (x , y) we can get (±x ,±y) for freeBut also (y , x) and ...So we only need to compute points inone octantTo keep slope |m| < 1 and forincreasing x , the octant we choose issecond

(x , y)

CS4815

Page 10: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Using symmetry

Computing trigonometrical functions inthe θ approach previously is stillexpensiveDo we have to compute pixels overentire circle (360◦)?From (x , y) we can get (±x ,±y) for freeBut also (y , x) and ...So we only need to compute points inone octantTo keep slope |m| < 1 and forincreasing x , the octant we choose issecond

(x , y)

(x ,−y)

CS4815

Page 11: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Using symmetry

Computing trigonometrical functions inthe θ approach previously is stillexpensiveDo we have to compute pixels overentire circle (360◦)?From (x , y) we can get (±x ,±y) for freeBut also (y , x) and ...So we only need to compute points inone octantTo keep slope |m| < 1 and forincreasing x , the octant we choose issecond

(x , y)

(y , x)

CS4815

Page 12: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Using symmetry

Computing trigonometrical functions inthe θ approach previously is stillexpensiveDo we have to compute pixels overentire circle (360◦)?From (x , y) we can get (±x ,±y) for freeBut also (y , x) and ...So we only need to compute points inone octantTo keep slope |m| < 1 and forincreasing x , the octant we choose issecond

(x , y)

(y , x)

CS4815

Page 13: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Using symmetry

Computing trigonometrical functions inthe θ approach previously is stillexpensiveDo we have to compute pixels overentire circle (360◦)?From (x , y) we can get (±x ,±y) for freeBut also (y , x) and ...So we only need to compute points inone octantTo keep slope |m| < 1 and forincreasing x , the octant we choose issecond

CS4815

Page 14: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Midpoint Circle Algorithm

We have reduced computations to one octant, but stillrequires trig. functionsBresenham’s circle algorithm avoids this by findingclosest pixel to circumference of circleIt avoids the square-root calculation by comparing squaresof distances: if dl > du then d2

l > d2u

We can avoid even working with squares of distances andthis is what the Midpoint Circle Algorithm doesAs before, sample at unit intervals and find closest pixel tocircle pathFor a circle of radius r , centered at (xc , yc) we “shift” it toorigin and add back xc and yc to all computed values laterWe need a function that tests where a given point isrelative to our circle

fcirc(x , y) = x2 + y2 − r2

CS4815

Page 15: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Midpoint Circle Algorithm (contd.)

Given a point (x , y)

fcirc(x , y)

< 0, if (x , y) is inside the circle= 0, if (x , y) is on the circle> 0, if (x , y) is outside the circle

If we have coloured (xk , yk ), for xk+1 = xk + 1 what shouldyk+1 be?Should we stay with (and colour) yk+1 = yk or drop one toyk+1 = yk − 1?We test the point (xk + 1, yk − 1

2) and make this be ourdecision parameter

pk = fcirc(xk + 1, yk −12)

= (xk + 1)2 + (yk −12)2 − r2

CS4815

Page 16: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Midpoint Circle Algorithm (contd.)

Given a point (x , y)

fcirc(x , y)

< 0, if (x , y) is inside the circle= 0, if (x , y) is on the circle> 0, if (x , y) is outside the circle

If we have coloured (xk , yk ), for xk+1 = xk + 1 what shouldyk+1 be?Should we stay with (and colour) yk+1 = yk or drop one toyk+1 = yk − 1?We test the point (xk + 1, yk − 1

2) and make this be ourdecision parameter

pk = fcirc(xk + 1, yk −12)

= (xk + 1)2 + (yk −12)2 − r2

CS4815

Page 17: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Midpoint Circle Algorithm (contd.)

Given a point (x , y)

fcirc(x , y)

< 0, if (x , y) is inside the circle= 0, if (x , y) is on the circle> 0, if (x , y) is outside the circle

If we have coloured (xk , yk ), for xk+1 = xk + 1 what shouldyk+1 be?Should we stay with (and colour) yk+1 = yk or drop one toyk+1 = yk − 1?We test the point (xk + 1, yk − 1

2) and make this be ourdecision parameter

pk = fcirc(xk + 1, yk −12)

= (xk + 1)2 + (yk −12)2 − r2

CS4815

Page 18: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Midpoint Circle Algorithm (contd.)

Given a point (x , y)

fcirc(x , y)

< 0, if (x , y) is inside the circle= 0, if (x , y) is on the circle> 0, if (x , y) is outside the circle

If we have coloured (xk , yk ), for xk+1 = xk + 1 what shouldyk+1 be?Should we stay with (and colour) yk+1 = yk or drop one toyk+1 = yk − 1?We test the point (xk + 1, yk − 1

2) and make this be ourdecision parameter

pk = fcirc(xk + 1, yk −12)

= (xk + 1)2 + (yk −12)2 − r2

CS4815

Page 19: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Midpoint Circle Algorithm (contd.)

y

y

x x

k

k−1

k k+1

If pk < 0 the “half-way” y value isalready inside so we should stay withyk+1 = yko/w the halfway point is outside so weshould definitely compensate bydropping down to a lower y

CS4815

Page 20: Computer Graphicsgarryowen.csisdmz.ul.ie/~cs4815/resources/lect08.pdfDrawing Algorithms Circle Drawing Algorithms; 6-4 Midpoint Circle Algorithm We have reduced computations to one

Drawing Algorithms Circle Drawing Algorithms; §6-4

Midpoint Circle Algorithm (contd.)

From pk we can express pk+1 in terms of it, and aftersimplifying

pk = (xk + 1)2 + (yk −12)2 − r2

pk+1 = (xk + 1 + 1)2 + (yk+1 −12)2 − r2

= pk + 2(xk + 1) + (y2k+1 − y2

k )− (yk+1 − yk ) + 1

Since yk+1 can be either yk (pk < 0) or yk − 1 (pk ≥ 0)

pk+1 = pk + 2xk+1 +

{1, pk < 01− 2yk+1, otherwise

Initial condition: (x0, y0) = (0, r) and

p0 = fcirc(1, r −12) =

54− r

CS4815