MID POINT CIRCLE
ALGORITHM
CIRCLE ALGORITHMS
Use 8-fold symmetry and only compute pixel positions for the 45° sector.
45°
(x, y)
(y, x) (-x, y)
(y, -x)
(x, -y) (-x, -y)
(-y, x)
(-y, -x)
MIDPOINT CIRCLE ALGORITHM
y
i
y
i-1
x
ix
i+1 x
i+2
Midpoin t
x
2+ y
2– r
2= 0
Assuming that we have just plotted the pixels at (x
i, y
i).
Which is next? (x
i+1, y
i) OR (x
i+1, y
i– 1).
- The one that is closer to the circle.
MIDPOINT CIRCLE ALGORITHM
The decision parameter is the circle at the midpoint between the pixels y
iand y
i– 1.
If p
i< 0, the midpoint is inside the circle and the pixel y
iis closer to the circle boundary.
If p
i≥ 0, the midpoint is outside the circle and the pixel y
i- 1 is closer to the circle boundary.
1 2
2 1 2 2
2
( 1, )
( 1) ( )
i circle i i
i i
p f x y
x y r
DECISION PARAMETERS
Decision Parameters are obtained using incremental calculations
OR
where y
i+1is either y
ior y
i-1 depending on the sign of p
iNote:
x
i+1= x
i+1
1 1 1 12
2 1 2 2
1 2
( 1, )
( 2) ( )
i circle i i
i i
p f x y
x y r
2 2 2
1
2( 1) (
1) (
1) 1
i i i i i i i
p
p x y
y y
y
THE ALGORITHM
1. Initial values:- point (0 , r) [
x0 = 0 y0 = r] 2. Initial decision parameter
3.
At each x
iposition, starting at i = 0
if p
i< 0, the next point is (x
i+ 1, y
i) and
p
i+1= p
i+ 2x
i+1+ 1
if p
i≥ 0, the next point is (x
i+1, y
i-1) and
p
i+1= p
i+ 2x
i+1+ 1 – 2y
i+1where 2x
i+1= 2x
i+ 2 and 2y
i+1= 2y
i– 2
4. Determine symmetry points in the other octants 5. Repeat 3 – 4 until x becomes greater or equal to y
2 2 5
1 1
0 circle
(1,
2) 1 (
2)
4p f r r r r
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10
i p
iNext 2x
i+12y
i+10 -9 (1, 10) 2 18
1 -6 (2, 10) 4 18
2 -1 (3, 10) 6 18
3 6 (4, 9) 8 18
4 -3 (5, 9) 10 16
5 8 (6, 8) 12 16
6 5 (7, 7)
r = 10
p
0= 1 – r = -9 (if r is integer, then consider p
0= 1 – r)
Initial point (x
0, y
0) = (0, 10)
2x
i+1= 2x
i+ 2 and 2y
i+1= 2y
i– 2
If (p < 0) => (x
i+ 1, y
i)
p
i+1= p
i+ 2x
i+1+ 1 Else if (p>=0) =>
(x
i+1, y
i-1)
p
i+1= p
i+ 2x
i+1+ 1
– 2y
i+1MIDPOINT FUNCTION
void plotpoints(int x, int y)
{ setpixel(xcenter+x, ycenter+y);
setpixel(xcenter-x, ycenter+y);
setpixel(xcenter+x, ycenter-y);
setpixel(xcenter-x, ycenter-y);
setpixel(xcenter+y, ycenter+x);
setpixel(xcenter-y, ycenter+x);
setpixel(xcenter+y, ycenter-x);
setpixel(xcenter-y, ycenter-x);
}
void circle(int r) { int x = 0, y = r;
plotpoints(x,y);
int p = 1 – r;
while (x<y) {
x++;if (p<0) p += 2*x + 2;
else {
y--;p += 2*(x-y) + 1;
}plotpoints(x,y);
} }