forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bresenham_circle_algo.cpp
59 lines (51 loc) · 1.33 KB
/
bresenham_circle_algo.cpp
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
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<iostream.h>
void main()
{
clrscr();
//radius is the radius of the circle
//x0 and y0 is the centre of the circle
//x and y are an arbitrary point on circle
//p is the decision parameter
int x, y, x0, y0, p, radius;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
cout<<"Enter the radius of circle.";
cin>>radius;
cout<<"Enter co-ordinates of centre of circle.";
cin>>x0>>y0;
x = 0;
y = radius;
//putpixel is used to plot the point
putpixel(x0+x, y0-y, 10);
p = 3 - (2 * radius);
while(x <= y)
{
if(p < 0)
{
p = p + 4 * x + 6;
}
else
{
p = p + 4 * (x - y) + 10;
y--;
}
x++;
//since circle is 8 point symmetric so using this property to plot the circle
//first and second input of putpixel are the coordiantes while the third one
//is the color of the line
putpixel(x0+x, y0+y, 10);
putpixel(x0-x, y0-y, 9);
putpixel(x0+x, y0-y, 8);
putpixel(x0-x, y0+y, 7);
putpixel(x0+y, y0+x, 6);
putpixel(x0-y, y0-x, 5);
putpixel(x0+y, y0-x, 4);
putpixel(x0-y, y0+x, 3);
}
getch();
closegraph();
}
//sample output - https://ibb.co/FzJbVqT