### Archive

Archive for December 4, 2009

## C++ Graphics – 04 – Circle program by Midpoint Algorithm

Circle program by Midpoint Algorithm.

This algorithm draws all eight octants simultaneously, starting from each cardinal direction (0°, 90°, 180°, 270°) and extends both ways to reach the nearest multiple of 45° (45°, 135°, 225°, 315°). You can determine where to stop because when y = x, you have reached 45°.

```#include <graphics.h>
#include <conio.h>
#include <dos.h>

void CirclePoints(int x, int y){
putpixel(x+320, y+240, WHITE);
putpixel(y+320, x+240, WHITE);
putpixel(y+320, -x+240, WHITE);
putpixel(x+320, -y+240, WHITE);
putpixel(-x+320, -y+240, WHITE);
putpixel(-y+320, -x+240, WHITE);
putpixel(-y+320, x+240, WHITE);
putpixel(-x+320, y+240, WHITE);
}

void MidPointCircle(int r){
int x = 0;
int y = r;
double d = 5.0/4.0 - r;
CirclePoints(x, y);
while(y > x){
if(d < 0)
d += 2.0 * x + 3.0;
else{
d += 2.0 * (x - y) + 5.0;
y--;
}
x++;
CirclePoints(x, y);
delay(50);
}
}

void main(){
int gd=DETECT, gm;
initgraph(&gd, &gm, "\\tc");
MidPointCircle(200);
getch();
closegraph();
}
```

Bresenham’s Circle algorithm is derived from the Midpoint Circle algorithm.

Categories: Cpp Graphics