Вы находитесь на странице: 1из 36

# Scan Converting Circle

## A circle is defined as the locus of points at a distance r

from a fixed point (x
c
,y
c
). This distance is described In the
Pythagoras theorem as :

where (x
c
,y
c
) is the centre and r is the radius of the circle
The standard equation for the circle at centre (0,0) is:

So, we can write a simple circle drawing algorithm by
solving the equation for y at unit x intervals using:

2 2 2
) yc ( ) xc ( r y x = +
2 2 2
r y x = +
2 2
x r y =
Scan Converting Circle

void circleSimple(int xCenter, int yCenter, int
{
int x, y, r2;

for (x = -radius; x <= radius; x++)
{
y = (int)(sqrt(r2 - x*x) + 0.5);
setPixel(xCenter + x, yCenter + y, c);
setPixel(xCenter + x, yCenter y, c);
}
}
Scan Converting Circle
20 0 20
2 2
0
~ = y
20 1 20
2 2
1
~ = y
20 2 20
2 2
2
~ = y
6 19 20
2 2
19
~ = y
0 20 20
2 2
20
~ = y
Scan Converting Circle
However, unsurprisingly this is not a brilliant solution!
Firstly, it involves all floating point calculations
Secondly, the calculations are not very efficient
The square (multiply) operations
The square root operation try really hard to avoid these!
Thirdly, the resulting circle has large gaps where the slope
approaches the vertical

Scan Converting Circle
We came across this
problem with lines
before
Sometimes the slope of
the line tangent to a point
the circle is greater than 1.
Stepping in x wont work
there.
So we could look for this
case and step in y
Scan Converting Circle
But thats also not a good
solution!
But on both cases, we were
taking advantage of the
circles symmetry when we
draw both positive and
negative values for y (or x).
This is called 2-way
symmetry.
Are there more symmetries
to exploit?
Scan Converting Circle
Sure, lets try 4-way symmetry.

With just a quick hack, we get:
put_circle_pixel(x,y, x
c
,y
c
)
{
Put_pixel(x
c
+x, y
c
+y)
Put_pixel(x
c
-x, y
c
+y)
Put_pixel(x
c
-x, y
c
-y)
Put_pixel(x
c
+x, y
c
-y)
}

Scan Converting Circle
What about 8-way symmetry?
Now were looking for symmetries
across the diagonal lines, i.e. where
x=y.
Now when we step along x, we can
permute the coordinates (swap x and
y) and simultaneously be stepping
along y in another part of the circle.
Thats back to how we fixed lines!
Bonus, its 4 times faster than the
original!
Scan Converting Circle
Eight-Way Symmetry
So we can use learned eight-way symmetry to make our circle
drawing algorithm more efficient
Now circle points are computed
only in one octant,
rest of the circle are
found by symmetry.
Centre can be shifted while
plotting

(x, y)
(y, x)
(y, -x)
(x, -y) (-x, -y)
(-y, -x)
(-y, x)
(-x, y)
2
R
Scan Converting Circle
We define a routine that plots circle pixels with centre
(Xc,Yc) in all the eight octants

put_circle_pixel(x,y, x
c
,y
c
)
{
Put_pixel(x
c
+x, y
c
+y)
Put_pixel(x
c
-x, y
c
+y)
Put_pixel(x
c
-x, y
c
-y)
Put_pixel(x
c
+x, y
c
-y)
Put_pixel(x
c
+y, y
c
+x)
Put_pixel(x
c
-y, y
c
+x)
Put_pixel(x
c
-y, y
c
-x)
Put_pixel(x
c
+y, y
c
-x)
}

Circle Drawing Algorithms

1. Polynomial Method
2. Trigonometric Method (Polar Coordinates)
3. Bresenham Circle Algorithm
4. Midpoint Circle Algorithm

Polynomial Method
STEPS
1. Set the initial variables
r = Circle Radius, x =0, Xend = r/Sqrt(2)
i= step size= 1/r, (x
c
,y
c
)= Centre of Circle

2. Test to determine whether the entire circle has
been scan converted. That is, if x>x
end
then STOP.

3. Compute the value of (x,y)coordinates as

4. Plot the 8-points found by symmetry with respect
to the centre (x
c
,y
c
)at the current(x,y)coordinates
(x
c
+x, y
c
+y) , (x
c
+y, y
c
+x)
(x
c
x , y
c
+y) , (x
c
- y, y
c
+x)
(x
c
- x, y
c
- y) , (x
c
- y, y
c
- x)
(x
c
+x, y
c
- y) , (x
c
+y, y
c
-x)
5. Increment x : x = x + i

6. Goto step 2

2 2
x r y =
Circle Drawing Algorithms

1. Polynomial Method
2. Trignometric Method(Polar Coordinates)
3. Bresenham Circle Algorithm
4. Midpoint Circle Algorithm

Trigonometric Method
We could use polar coordinates r and ,
x = x
c
+ r cos y = y
c
+ r sin
A fixed angular step size can be used to plot equally spaced
points along the circumference
A step size of 1/r can be used to set pixel positions to
approximately 1 unit apart for a continuous boundary.
Since Circle follows eight point symmetry, thus we can
generate all pixel positions around a circle by calculating just
the points within the sector from x=0 to x=y
Trigonometric Method
STEPS
1. Set the initial variables
r = Circle Radius, u =0, , radius= 45
o

i= step size= 1/r, (x
c
,y
c
)= Centre of Circle

2. Test to determine whether the entire circle has
been scan converted. That is, if u>u
end
then STOP.

3. Compute the value of (x,y)coordinates as
x = r cos u , y = r sin u
4. Plot the 8-points found by symmetry with respect
to the centre (x
c
,y
c
)at the current(x,y)coordinates
(x
c
+x, y
c
+y) , (x
c
+y, y
c
+x)
(x
c
x , y
c
+y) , (x
c
- y, y
c
+x)
(x
c
- x, y
c
- y) , (x
c
- y, y
c
- x)
(x
c
+x, y
c
- y) , (x
c
+y, y
c
-x)
5. Increment u : u = u + i

6. Goto step 2

/4 t u = end
Circle Drawing Algorithms

1. Polynomial Method
2. Trigonometric Method (Polar Coordinates)
3. Bresenham Circle Algorithm
4. Midpoint Circle Algorithm

Bresenhams Circle Algorithm
1. Introduction
Like line algorithm it make choice of next pixel to be
plotted from two candidate for the next pixel position
Find the closest integer coordinates to the actual circle
path using only integer arithmetic
For ease of calculations circle is assumed to have centre
as origin.
Computations are done in second octant, where x
increases, y decreases, and the slope is less than 1
No division, Efficient comparison, No floating point
operations
Bresenhams Circle Algorithm
2. Derivation
Let us assume that P(x
k
, y
k
) is
the currently plotted pixel.
Q(x
k+1
, y
k+1
) (x
k+1
, y ) is the
next point along the actual
circle path. We need to decide
next pixel to be plotted from
two candidate positions
Q1(x
k
+1, y
k
) or Q2(x
k
+1, y
k
-1)
Q1
Q2
P
Bresenhams Circle Algorithm
Thus actual value of y at x = x
k+1
is given by
y
2

= r
2
(x
k
+1)

2

We define the distance measure in squares to simplify calculations
Let d
1
= y
k
2

y
2

= y
k
2

[r
2
(x
k
+1)

2
]

d
2
= y
2

(y
k
-1)

2

= [r
2
(x
k
+1)

2
]

(y
k
-1)

2

The difference in d
1
and d
2
determine the next pixel to be plotted
if (d
1
-d
2
)<0 Q1(x
k
+1, y
k
) is plotted
else Q2(x
k
+1, y
k
-1) is plotted.

Bresenhams Circle Algorithm
We can define a decision parameter p
k
for the k
th
step as follows:
P
k
= ( d
1
-d
2
)
= 2.(x
k
+1)

2
+ y
k
2
(y
k
-1)

2
2.r
2

To put p
k
in recurrence relation replace k = k+1

P
k+1
= 2.(x
k+1
+1)

2
+ y
k+1
2
(y
k+1
-1)

2
2.r
2

= 2.(x
k
+2)

2
+ y
k+1
2
(y
k+1
-1)

2
2.r
2

From above two equations

P
k+1
P
k
= [2.(x
k
+2)

2
2.(x
k
+1)

2
] +
[y
k+1
2
y
k
2
] [(y
k+1
-1)

2
(y
k
-1)

2
]

P
k+1
= P
k
+ 4.x
k
+6

+ [y
k+1
2
y
k
2
] [(y
k+1
-1)

2
(y
k
-1)

2
]
Bresenhams Circle Algorithm
If p
k
< 0
Q1(x
k
+1, y
k
) was the next choice
y
k+1
= y
k
[y
k+1
2
y
k
2
] [(y
k+1
-1)

2
(y
k
-1)

2
] = 0
p
k+1
= p
k
+ 4.x
k
+6
else
Q2(x
k
+1, y
k

1) was the next choice
y
k+1
= y
k

1

[y
k+1
2
y
k
2
] [(y
k+1
-1)

2
(y
k
-1)

2
] = 4.y
k
+4
p
k+1
= p
k
+ 4(x
k
y
k
)+ 10

Bresenhams Circle Algorithm
The first parameter p
0
is directly computed from:
P
k
= 2.(x
k
+1)

2
+ y
k
2
(y
k
-1)

2
2.r
2

By putting k = 0, x
k
= 0 and y
k
= r
p
0
= 2.(x
0
+1)

2
+ y
0
2
(y
0
-1)

2
2.r
2

= 2.(0

+1)

2
+ r
2
(r-1)

2
2.r
2

= 3 2.r

Bresenhams Circle Algorithm
Algorithm steps
1. Input radius r and circle center (h,k) and obtain the first
point on the circumference of the circle centered on the
origin as (x
0
,y
0
) = (0,r)
2. Set initial value of the decision parameter as p
0
= 3 2.r
3. At each x
k
starting at k = 0 , perform the following test:
If p
k
< 0 , the next point along the circle centered on (0,0)
is (x
k
+1, y
k
)
p
k+1
= p
k
+ 4x
k
+ 6
else //the next point along the circle centered on (0,0) is
(x
k
+1, y
k
-1)
p
k+1
= p
k
+ 4x
k

4y
k
+ 10;
y
k
= y
k
1

4. Increment x
k
= x
k
+ 1
5. Plot circle pixels using 8-way symmetry centred on (h,k)
6. Repeat steps 3 through 5 until x <= y
Circle Drawing Algorithms

1. Polynomial Method
2. Trigonometric Method
3. Bresenham Circle Algorithm
4. Midpoint Circle Algorithm

Midpoint Circle Algorithm
1. Introduction
Similarly to the case with lines, there is an incremental
algorithm for drawing circles the mid-point circle
algorithm.
In the mid-point circle algorithm we use implicit equation of
the circle.
Integer calculations are used to compute the circle points in
one octant, rest of the seven points are plotted using eight
way symmetry.
The equations derived will be similar to Bresenhams circle
algorithm
Midpoint Circle Algorithm
2. Basic Concept
We will first calculate pixel positions for a circle centered
around the origin (0,0). Centre is shifted later on.
Note that along the circle section from x=0 to x=y in the first
octant, the slope of the curve varies from 0 to -1
Circle function around the origin is given by
f
circle
(x,y) = x
2
+ y
2
r
2
Any point (x,y) on the boundary of the circle satisfies the
equation and circle function is zero
Midpoint Circle Algorithm
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
Thus, we have a discriminator function
f
circle
(x,y) < 0 if (x,y) is inside the circle boundary
f
circle
(x,y) = 0 if (x,y) is on the circle boundary
f
circle
(x,y) > 0 if (x,y) is outside the circle boundary
The algorithm does the above test at mid point f
circle
(x,y-), if
it lies inside outer point is plotted, else inner is considered to be
the better choice
Midpoint Circle Algorithm
6
2 3 4 1
5
4
3
Midpoint Circle Algorithm
M
6
2 3 4 1
5
4
3
Midpoint Circle Algorithm

M
6
2 3 4 1
5
4
3
Midpoint Circle Algorithm
3. Derivation
Let us assume that P(x
k
, y
k
)
is the currently plotted pixel.
Q(x
k+1
, y
k+1
) (x
k+1
, y ) is
the next point along the actual
circle path. We need to decide
next pixel to be plotted from
among candidate positions
Q1(x
k
+1, y
k
) or
Q2(x
k
+1, y
k
-1)
Midpoint Circle Algorithm
Our decision parameter is the circle function evaluated at
the midpoint between these two pixels

p
k
= f
circle
(x
k
+1, y
k
-1/2) = (x
k
+1)
2
+ (y
k
-1/2)
2
r
2

If p
k
< 0 ,
this midpoint is inside the circle and
the pixel on the scan line y
k
is closer to the circle boundary.
Otherwise,
the mid position is outside or on the circle boundary,
and we select the pixel on the scan line y
k
-1

Midpoint Circle Algorithm
Successive decision parameters are obtained using incremental
calculations
p
k
= (x
k
+1)
2
+ (y
k
)
2
r
2

Put k = k+1
p
k+1
= [(x
k+1
)+1]
2
+ (y
k+1
)
2
r
2
= (x
k
+2)
2
+ (y
k+1
)
2
r
2

subtracting p
k
from p
k+1

p
k+1
P
k
= (x
k
+2)
2
+ (y
k+1
)
2
[(x
k
+1)
2
+ (y
k
)
2
]
or
p
k+1
= P
k
+2(x
k
+1) + (y
k+1
)
2
(y
k
)
2
+1
Where y
k+1
is either y
k
or y
k-1
, depending on the sign of p
k
Midpoint Circle Algorithm
If p
k
< 0
Q1(x
k
+1, y
k
) was the next choice
y
k+1
= y
k
(y
k+1
)
2
(y
k
)
2
= 0
p
k+1
= p
k
+ 2.x
k
+3
else
Q2(x
k
+1, y
k
-1) was the next choice
y
k+1
= y
k
+1

(y
k+1
)
2
(y
k
)
2
= 2.y
k
+2
p
k+1
= p
k
+ 2(x
k
y
k
)+ 5

Midpoint circle algorithm
Initial decision parameter is obtained by evaluating the circle
function at the start position (x0,y0) = (0,r)
p
0
= f
circle
(1, r 1/2)
= 1+ (r 1/2)
2
r
2
= 5/4 r
If radius r is specified as an integer, we can round p
0
to
p
0
= 1 r

Midpoint Circle Algorithm
4. Algorithm steps
1. Input radius r and circle center (h,k) and obtain the first
point on the circumference of the circle centered on the origin
as (x
0
,y
0
) = (0,r)
2. Set initial value of the decision parameter as p
0
= 1 r
3. At each x
k
starting at k = 0 , perform the following test:
If p
k
< 0 , //the next point along the circle centered on (0,0)
is (x
k
+1, y
k
)
p
k+1
= p
k
+ 2x
k
+ 3
else //the next point along the circle centered on (0,0)
is (x
k
+1, y
k
-1)
p
k+1
= p
k
+ 2x
k

2y
k
+ 5;
y
k
= y
k
1

4. Increment x
k
= x
k
+ 1
5. Plot circle pixels using 8-way symmetry centred on (h,k)
6. Repeat steps 3 through 5 until x <= y