Advertisements
Home > Cpp Graphics > C++ Graphics – 04 – Circle program by Midpoint Algorithm

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.


Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: