Advertisements

Archive

Posts Tagged ‘Midpoint Algorithm’

C++ Graphics – 05 – Ellipse program by Midpoint Algorithm

December 5, 2009 Leave a comment

Ellipse Program by Midpoint Algorithm.

Similarly to the Circle Midpoint algorithm this algorithm also 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°).

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

void ellipsePoint(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 ellipseMidPoint(int a, int b){
	double d2;
	int x=0;
	int y=b;
	double d1 = b*b-(a*a*b)+(0.25*a*a);
	ellipsePoint(x, y);
	while( (a*a*(y-0.5)) > (b*b*(x+1)) ){
		if(d1 < 0)
			d1 += b*b*(2*x+3);
		else{
			d1 += b*b*(2*x+3)+a*a*(-2*y+2);
			y--;
			}
		x++;
		ellipsePoint(x, y);
		}
	d2 = b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;
	while(y > 0){
		if(d2 < 0){
			d2 += b*b*(2*x+2)+a*a*(-2*y+3);
			x++;
			}
		else
			d2 += a*a*(-2*y+3);
		y--;
		ellipsePoint(x, y);
		}
	}

void main(){
	int gd=DETECT, gm;
	initgraph(&gd, &gm, "e:\\tc\\");
	ellipseMidPoint(100, 200);
	getch();
	closegraph();
	}

Advertisements

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

December 4, 2009 Leave a comment

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.