Sunteți pe pagina 1din 20

Computer Graphics

Mid-Point Circle Algorithm


A Simple Circle Drawing
Algorithm
• The equation for a circle is:
x2  y2  r 2
• where r is the radius of the circle
• So, we can write a simple circle
drawing algorithm by solving the
equation for y at unit x intervals using:

y r x2 2
A Simple Circle Drawing Algorithm
(cont…)
y0  20 2  0 2  20

y1  20 2  12  20

y2  20 2  2 2  20

y19  20 2  19 2  6

y20  20 2  20 2  0
A Simple Circle Drawing Algorithm

• However, unsurprisingly this is not a brilliant


solution!
• Firstly, the resulting circle has large gaps where
the slope approaches the vertical
• Secondly, the calculations are not very efficient
– The square (multiply) operations
– The square root operation – try really hard to avoid
these!
• We need a more efficient, more accurate solution
Scan Converting Circles
Version 1: really bad (0, 17)

For x = – R to R
y = sqrt(R * R – x * x);
Pixel (round(x), round(y));
(17, 0)
Pixel (round(x), round(-y));
(0, 17)

Version 2: slightly less bad


For x = 0 to 360
(17, 0
Pixel (round (R • cos(x)), round(R • sin(x)));
Polar coordinates
X=r*cosθ+xc
Y=r*sinθ+yc
0º≤θ≤360º
Or
0 ≤ θ ≤6.28(2*π)
Problem:
– Deciding the increment in θ
– Cos, sin calculations
Eight-Way Symmetry
• The first thing we can notice to make our
circle drawing algorithm more efficient is that
circles centred at (0, 0) have eight-way
symmetry
Mid-Point Circle Algorithm
• Assume that we
just plotted point (xk, yk) (xk, yk) (xk+1, yk)
• The next point is a
choice between (xk+1, yk)
and (xk+1, yk-1) (xk+1, yk-1)
• We would like to choose
the point that is nearest to
the actual circle
• So how do we make this choice?
Mid-Point Circle Algorithm
6

1 2 3 4
Mid-Point Circle Algorithm
6

M
5

1 2 3 4
Mid-Point Circle Algorithm
6
M
5

1 2 3 4
Circle drawing.
• Can also use Bresenham to draw circles.

• Use 8-fold symmetry

M
SE

Previous Choices for Choices for


Pixel Current pixel Next pixel

12
Mid-Point Circle Algorithm
• Let’s the equation of the circle slightly to give us:

x, y )  x as
f circ (evaluates
• The equation  yfollows:
2
r
2 2

 0, if ( x, y ) is inside the circle boundary



f circ ( x, y )  0, if ( x, y ) is on the circle boundary
 0, if ( x, y ) is outside the circle boundary

• By evaluating this function at the midpoint
between the candidate pixels we can make our
decision
Mid-Point Circle Algorithm
• Assuming we have just plotted the pixel at
(xp,yp) so we need to choose between
(xp+1,yp) and (xp+1,yp-1)
• Our decision variable can be defined as:
d k  f circ ( x p  1, y p  1 )
2
 ( x p  1) 2  ( y p  1 ) 2  r 2
2
• If dk < 0 the midpoint is inside the circle and
the pixel at yp is closer to the circle
• Otherwise the midpoint is outside and yp-1 is
closer
Mid-Point Circle Algorithm
• To ensure things are as efficient as possible we
can do all of our calculations incrementally
• First consider:


d k 1  f circ x p 1  1, y p 1  1 2
 2
• or: 2
 [( x p  1)  1]  y p 1  1
2
 r2

• where yp+1 is either yp or 2 yp-1 2 depending on the


d k 1  d k  2( x p  1)  ( y p 1  y p )  ( y p 1  y p )  1
sign of dk
Circle drawing.
• Implicit form for a circle is:
d0  5  r
4
If SE is chosen d new  d old  (2 x p  2 y p  5)
If E is chosen d new  d old  (2 x p  3)

• Functions are linear equations in terms of (xp,yp)


–Termed point of evaluation
Mid-Point Circle Algorithm
Example
• To see the mid-point circle algorithm in
action lets use it to draw a circle centred
at (0,0) with radius 10
Mid-Point Circle Algorithm Example
10 k dk (xk+1,yk+1) 2xk+1 2yk+1
9
8 0
7 1
6
5 2
4 3
3
2 4
1 5
0
6
0 1 2 3 4 5 6 7 8 9 10
Mid-Point Circle Algorithm
Exercise
• Use the mid-point circle algorithm to
draw the circle centred at (0,0) with
radius 15
Mid-Point Circle Algorithm Example
16 k dk (xk+1,yk+1) 2xk+1 2yk+1
15
14 0
13
1
12
11 2
10 3
9 4
8 5
7
6 6
5 7
4 8
3 9
2
1 10
0 11

0 1 2 3 4 5 6 7 8 9 10 1112 13141516 12

S-ar putea să vă placă și