scan converting circle

24
24-02-2011 www.chitkarauniversity.edu.in 1 Scan Converting Circle Compiled By : Neha Aggarwal Lecturer CSE Dept.

Upload: anik

Post on 31-Mar-2015

173 views

Category:

Documents


0 download

DESCRIPTION

Bresenham Circle, Midpoint Algorithm

TRANSCRIPT

Page 1: Scan Converting Circle

24-02-2011www.chitkarauniversity.edu.in1

Scan Converting Circle

Compiled By:

Neha Aggarwal

Lecturer

CSE Dept.

Page 2: Scan Converting Circle

Topics to be Covered

24-02-2011www.chitkarauniversity.edu.in2

Properties of Circle

Bresenham’s Circle Drawing Algorithm

Calculating decision parameter

Midpoint Circle Algorithm

Calculating decision parameter

Examples

Page 3: Scan Converting Circle

Properties of Circle

24-02-2011www.chitkarauniversity.edu.in3

A circle is defined as a set of points that are all at the given distance from the centre position (xc,yc).

This distance relationship is expressed by the pythagorean theorem in Cartesian coordinates as

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

2 = r2

This equation can be used to calculate the points on the circle circumference by stepping along x-axis in unit steps from xc-r to xc+r and calculate the corresponding y values at each position as

y = yc +(- ) (r2 – (xc –x )2)1/2

Problems with this method:

Considerable amount of computation.

Spacing between plotted pixels is not uniform.

Page 4: Scan Converting Circle

Solution to the Problem

24-02-2011www.chitkarauniversity.edu.in4

Adjust the spacing by interchanging x and y whenever the absolute value of slope of circle is greater than 1. But, this will not reduce the computation.

Solution could be to use Polar Coordinates of Circle.

Polar coordinates of any circle are represented as r and θ where

1. r represents the radius of the circle.

2. θ represents the angle of the circle with respect to any axis.

Then, the circle equations in parametric polar form are: x = xc + r cosθ y = yc + r sinθ

Page 5: Scan Converting Circle

Symmetry of Circle

24-02-2011www.chitkarauniversity.edu.in5

Page 6: Scan Converting Circle

Bresenham’s Circle Drawing Algo

24-02-2011www.chitkarauniversity.edu.in6

Assumptions

1. Points are generated from 90 degree to 45 degree.

2. Moves are made only in +ve x- direction and –ve y-

direction.

3. If the points are generated from 90 to 45 degree, then

each new point closest to the true circle can be

found by taking either of the following two actions:

i. Move in the x-direction 1 unit.

ii. Move in the x-direction 1 unit and move in the –ve y-

direction 1 unit.

Page 7: Scan Converting Circle

Calculating the Decision Parameter

24-02-2011www.chitkarauniversity.edu.in7

Assume that (xi, yi) are the coordinates

of the last scan converted pixel upon

entering the step i.

D(T) is the distance from the origin to

the pixel T squared minus the distance

to the true circle squared

i.e. D(T) = (xi+1)2 + yi2 – r2

D(S) is the distance from the origin to

the pixel S squared minus the distance

to the true circle squared

i.e. D(S) = (xi+1)2 + (yi -1) 2 – r2

T(xi+1, yi)

S(xi+1, yi-1)

O xi xi+1

yi-1

yi

Page 8: Scan Converting Circle

Calculating the Decision Parameter (Contd…)

24-02-2011www.chitkarauniversity.edu.in8

D(T) will always be +ve and D(S)will always be –ve.

Decision Parameter

di = D(T) + D(S)

di = ((xi+1)2 + yi2 – r2 ) + ((xi+1)2 + (yi -1) 2 – r2)

di = 2(xi+1)2 + yi2 + (yi -1) 2 –2 r2 ……(1)

When di <0 i.e. |D(T)| < |D(S)|, pixel T is chosen

When di >0 i.e. |D(T)| > |D(S)|, pixel S is chosen

Page 9: Scan Converting Circle

Calculating the Decision Parameter (Contd…)

24-02-2011www.chitkarauniversity.edu.in9

Decision Parameter for next pixel di+1

di+1 = 2(xi+1+1)2 + yi+12 + (yi+1 -1) 2 –2 r2

di+1 - di = {2(xi+1+1)2 + yi+12 + (yi+1 -1) 2 –2 r2} – {2(xi+1)2 + yi

2 + (yi -

1) 2 –2 r2}

For next pixel, xi+1 = xi +1

di+1 = di + 4xi + 2(yi+12 - yi

2)- 2(yi+1 -yi)+ 6

If di < 0, yi+1 = yi

di+1 = di + 4xi + 6

If di >= 0, yi+1 = yi - 1

di+1 = di + 4(xi – yi ) + 10

Since, the circle is drawn from origin, so, (x,y) = (0,r)

Therefore, From Equation (1)

d0 = 2(0+1)2 + r2 + (r -1) 2 –2 r2

d0 = 3-2r

Page 10: Scan Converting Circle

Algo for Bresenham’s Circle Drawing

24-02-2011www.chitkarauniversity.edu.in10

Step 1: Set the initial values of the variables (h,k) =

coordinates of the circle centre i.e. (o,r)

Initial decision parameter is d=3-2r (as calculated)

Step 2: Test to determine whether the entire circle has been

scan converted. If x>y, stop.

Step 3: Plot the 8 points found by symmetry w.r.t. the

center (h,k) at the current (x,y) coordinates. Plot

(x+h,y+k).

Step 4: Compute the location of the next pixel.

If d<0, then d = d + 4x + 6 and x=x+1

If d>=0, then d = d + 4(x-y) +10 and x=x+1, y=y-1

Step 5: Goto step 2

Page 11: Scan Converting Circle

Midpoint Circle AlgorithmAssumptions

Calculate pixel positions for a circle centered around the origin (0,0). Then, each calculated position (x,y) is moved to its proper screen position by adding xc to x and yc to y

Along the circle section from x=0 to x=y in the first octant, the slope of the curve varies from 0 to -1

By midpoint method, Circle function around the origin is given by

fcircle(x,y) = x2 + y2 – r2

Any point (x,y) on the boundary of the circle satisfies the equation and circle function is zero.

For a point in the interior of the circle, the circle function is negative and for a point outside the circle, the function is positive.

24-02-201111 www.chitkarauniversity.edu.in

Page 12: Scan Converting Circle

24-02-201112 www.chitkarauniversity.edu.in

Page 13: Scan Converting Circle

Midpoint Circle Algorithm Thus,

fcircle(x,y) < 0 if (x,y) is inside the circle boundary

fcircle(x,y) = 0 if (x,y) is on the circle boundary

fcircle(x,y) > 0 if (x,y) is outside the circle boundary

yk

yk-1

xk xk+1 xk+2Midpoint

x2+y2-r2=0

Midpoint between candidate pixels at

sampling position xk+1 along a circular path

24-02-201113 www.chitkarauniversity.edu.in

Page 14: Scan Converting Circle

Calculating the Decision Parameter

Assuming we have just plotted the pixel at (xk,yk) , we

next need to determine whether the pixel at position

(xk + 1, yk-1) or (xk + 1, yk) is closer to the circle.

Decision parameter is the circle function evaluated at

the midpoint between these two pixels

pk = fcircle (xk +1, yk-1/2) = (xk +1)2 + (yk -1/2)2 – r2

If pk < 0 , this midpoint is inside the circle and the

pixel on the scan line yk is closer to the circle

boundary.

Otherwise, the mid position is outside or on the circle

boundary, and pixel on the scan line yk-1 is selected.

24-02-201114 www.chitkarauniversity.edu.in

Page 15: Scan Converting Circle

Calculating the Decision Parameter (Contd…)

Successive decision parameters are obtained using

incremental calculations

pk+1 = fcircle(xk+1+1, yk+1-1/2)

= [(xk+1)+1]2 + (yk+1 -1/2)2 –r2

OR

pk+1 = pk+2(xK+1) + (yK+12 – yk

2) – (yk+1- yk)+1

Where yk+1 is either yk or yk-1, depending on the sign of pk

Increments for obtaining Pk+1:

2xk+1+1 if pk is -ve

2xk+1+1-2yk+1 , if pk is +ve

where xk+1 +1 = xk and yk+1 = yk -1

24-02-201115 www.chitkarauniversity.edu.in

Page 16: Scan Converting Circle

Calculating the Decision Parameter (Contd…)

24-02-2011www.chitkarauniversity.edu.in16

Initial decision parameter is obtained from center (0,r) of

the circle i.e.

pk (0,r) = 5/4 – r

If radius is integer, then pk = 1 – r

Page 17: Scan Converting Circle

Midpoint Circle Algorithm

24-02-2011www.chitkarauniversity.edu.in17

1. Input initial values i.e. radius r and circle center (xc,yc)

and obtain the first point on the circumference of the

circle centered on the origin as (x0,y0) = (0,r).

2. Calculate the initial value of the decision parameter as

p0 = 5/4 – r

3. At each xk , starting at k=0, perform the following test:

If pk < 0, the next position on the circle centered on

(0,0) is (xk+1 , yk) and pk+1 = pk+ 2(xK+1) + 1

Otherwise, the next point along the circle is (xk +1,yk-1)

and pk+1 = pk+ 2xk+1 + 1 – 2yk+1 where 2xk+1 = 2xk +2

and 2yk+1 = 2yk-2

Contd….

Page 18: Scan Converting Circle

Midpoint Circle Algorithm

24-02-2011www.chitkarauniversity.edu.in18

4. Determine symmetry points in other seven octants.

5. Move each calculated position (x, y) onto the circular

path centered on (xc, yc) and plot the coordinate

values as x = x + xc and y = y + yc

6. Repeat steps 3 through 5 until x>=y.

Page 19: Scan Converting Circle

Mid-point circle algorithm (Example)

Given a circle radius r = 10, demonstrate the midpoint circle algorithm by determining positions along the circle octant in the first quadrant from x = 0 to x = y.

Solution:

p0 =1 – r = – 9

Plot the initial point (x0, y0 ) = (0, 10),

2x0 = 0 and 2y0 =20.

Successive decision parameter values and positions along the circle path are calculated using the midpoint method as appear in the next table:

24-02-201119 www.chitkarauniversity.edu.in

Page 20: Scan Converting Circle

24-02-2011www.chitkarauniversity.edu.in

Mid-point circle algorithm (Example)

K Pk (xk+1, yk+1) 2 xk+1 2 yk+1

0 – 9 (1, 10) 2 20

1 – 6 (2, 10) 4 20

2 – 1 (3, 10) 6 20

3 6 (4, 9) 8 18

4 – 3 (5, 9) 10 18

5 8 (6,8) 12 16

6 5 (7,7) 14 14

20

Page 21: Scan Converting Circle

24-02-2011www.chitkarauniversity.edu.in

Mid-point circle algorithm (Example)

21

Page 22: Scan Converting Circle

24-02-2011

Mid-point Circle Algorithm – Example (2)

Given a circle radius r = 15, demonstrate the midpoint circle algorithm by determining positions along the circle octant in the first quadrant from x = 0 to x = y.

Solution:

p0 = 1 – r = – 14

plot the initial point (x0 , y0) = (0, 15),

2x0 = 0 and 2y0 = 30.

Successive decision parameter values and positions along the circle path are calculated using the midpoint method as:

22 www.chitkarauniversity.edu.in

Page 23: Scan Converting Circle

24-02-2011

K Pk (xk+1, yk+1) 2 xk+1 2 yk+1

0 – 14 (1, 15) 2 30

1 – 11 (2, 15) 4 30

2 – 6 (3, 15) 6 30

3 1 (4, 14) 8 28

4 – 18 (5, 14) 10 28

Mid-point Circle Algorithm – Example (2)

23 www.chitkarauniversity.edu.in

Page 24: Scan Converting Circle

24-02-2011www.chitkarauniversity.edu.in

Mid-point Circle Algorithm – Example (2)

K Pk (xk+1, yk+1) 2 xk+1 2 yk+1

5 – 7 (6,14) 12 28

6 6 (7,13) 14 26

7 – 5 (8,13) 16 26

8 12 (9,12) 18 24

9 7 (10,11 ) 20 22

10 6 (11,10) 22 20

24