8
Midpoint Circle Algorithm Mid point algorithm is very similar to Bresenham’s approach. It is based on the following function for testing the spatial relationship between an arbitrary point(x,y) and a circle of radius centered at the origin: < 0 (x,y) inside circle f(x,y) = x2 + y2 – r2 = 0 (x,y) on the circle > 0 (x,y) outside circle`

Midpoint circle algo

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Midpoint circle algo

Midpoint Circle AlgorithmMid point algorithm is very similar toBresenham’s approach. It is based on thefollowing function for testing the spatialrelationship between an arbitrary point(x,y)and a circle of radius centered at the origin: < 0 (x,y) inside circlef(x,y) = x2 + y2 – r2 = 0 (x,y) on the circle > 0 (x,y) outside

circle`

Page 2: Midpoint circle algo

Now consider the coordinates of the point

halfway between pixel T & S (xi + 1, yi – ½)

This is called the midpoint and we use it to

define a decision parameter:

pi = f( xi +1, yi – ½)

= (xi + 1)2 + (yi – ½)2 – r2

If pi is –ve , the midpoint is inside the circle,

then we choose pixel T. If pi is +ve, the

midpoint is outside the circle, & we choose S.

Page 3: Midpoint circle algo

Similarly, the decision parameter for the next

step is:

pi+1 = (xi+1 + 1)2 + (yi+1 – ½)2 – r2

Since x i+1 = xi + 1

pi+1 - pi = [(xi + 1)+ 1]2 – (xi + 1)2

+ (y i+1 – ½)2 – (yi – ½)2

Hence

pi+1 = pi + 2(xi + 1) + 1 + (y i+12 – yi2)

– (y i+1 – yi)

Page 4: Midpoint circle algo

If pixel T is chosen (meaning pi < 0), we have yi+1 = yi.

If pixel S is chosen (meaning pi > 0), we have yi+1 = yi – 1. Thus,

pi+1= pi + 2(xi + 1) +1 if pi<0 pi + 2(xi + 1) +1 – 2(yi – 1) if pi>0In terms of (xi, yi), we have

pi+1= pi + 2xi + 3 if pi<0 pi + 2(xi - yi ) + 5 if pi>0

Page 5: Midpoint circle algo

Finally, we compute the initial value for the

decision parameter using the original

definition of pi and (0,r):

pi = (0 + 1)2 + (r – ½)2 – r2 = 5/4 – r

One can see that this is not really integer

computation. However, when r is an integer

we can simply set p1= 1 – r. The error of

being ¼ less than the precise value does not

prevent p1 from getting the appropriate sign.

Page 6: Midpoint circle algo

It does not affect the rest of the scan

conversion process, because the decision

variable is only updated with integer increment

in subsequent steps.

Page 7: Midpoint circle algo

Algorithm:

The following is a description of this midpoint

circle algorithm that generates the pixel

coordinates in the 90˚ to 45 ˚ octant:

int x = 0, y = r, p = 1-r

while (x <=y)

{ setpixel(x,y);

if (p < 0)

p = p + 2x + 3

Page 8: Midpoint circle algo

else

{

p = p + 2(x – y) + 5

y- -

}

x++

}