Java/PHP/GitLab Fan, Passionate Developer and Loves Teaching

0%

Midpoint Circle Algorithm

Summary

This is an algorithm for Computer Graphics using C Language to draw a circle using the Midpoint Circle Algorithm

Code

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
#include "stdio.h"
#include "conio.h"
#include "graphics.h"

void plotpts(int x1,int y1,int a1,int b1,int c1)
{
putpixel(x1+a1,y1-b1,c1);
putpixel(x1-a1,y1-b1,c1);
putpixel(x1+a1,y1+b1,c1);
putpixel(x1-a1,y1+b1,c1);
putpixel(x1+b1,y1-a1,c1);
putpixel(x1-b1,y1-a1,c1);
putpixel(x1+b1,y1+a1,c1);
putpixel(x1-b1,y1+a1,c1);
}

void ci(int a,int b,int c,int co)
{
double p = 1-c;
int xc = 0,yc = c;
putpixel(a+xc,b-yc,co);
putpixel(a-xc,b-yc,co);
putpixel(a+xc,b+yc,co);
putpixel(a-xc,b+yc,co);
while(yc > xc)
{
xc++;
if(p<0)
{
p = p + 2 * (xc + 1) + 1;
}
else
{
p = p + 2 * (xc + 1) + 1 - 2 * (yc - 1);
yc--;
}
plotpts(a,b,xc,yc,co);
}
}

void main()
{
int maxx,maxy,gd,gm;
detectgraph(&gd,&gm);
gd = DETECT;
initgraph(&gd,&gm,"C:\TC\BGI");
maxx = getmaxx()/2;
maxy = getmaxy()/2;
ci(maxx,maxy,100,1);
getch();
closegraph();
}