-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMid Point Circle Algorithm
70 lines (59 loc) · 1.71 KB
/
Mid Point Circle Algorithm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// C program for implementing
// Mid-Point Circle Drawing Algorithm
#include<stdio.h>
// Implementing Mid-Point Circle Drawing Algorithm
void midPointCircleDraw(int x_centre, int y_centre, int r)
{
int x = r, y = 0;
// Printing the initial point on the axes
// after translation
printf("(%d, %d) ", x + x_centre, y + y_centre);
// When radius is zero only a single
// point will be printed
if (r > 0)
{
printf("(%d, %d) ", x + x_centre, -y + y_centre);
printf("(%d, %d) ", y + x_centre, x + y_centre);
printf("(%d, %d)\n", -y + x_centre, x + y_centre);
}
// Initialising the value of P
int P = 1 - r;
while (x > y)
{
y++;
// Mid-point is inside or on the perimeter
if (P <= 0)
P = P + 2*y + 1;
// Mid-point is outside the perimeter
else
{
x--;
P = P + 2*y - 2*x + 1;
}
// All the perimeter points have already been printed
if (x < y)
break;
// Printing the generated point and its reflection
// in the other octants after translation
printf("(%d, %d) ", x + x_centre, y + y_centre);
printf("(%d, %d) ", -x + x_centre, y + y_centre);
printf("(%d, %d) ", x + x_centre, -y + y_centre);
printf("(%d, %d)\n", -x + x_centre, -y + y_centre);
// If the generated point is on the line x = y then
// the perimeter points have already been printed
if (x != y)
{
printf("(%d, %d) ", y + x_centre, x + y_centre);
printf("(%d, %d) ", -y + x_centre, x + y_centre);
printf("(%d, %d) ", y + x_centre, -x + y_centre);
printf("(%d, %d)\n", -y + x_centre, -x + y_centre);
}
}
}
// Driver code
int main()
{
// To draw a circle of radius 3 centred at (0, 0)
midPointCircleDraw(0, 0, 3);
return 0;
}