CG LAB

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6 PROGRAM 7 PROGRAM 8 PROGRAM 9 PROGRAM 10 PROGRAM 11 PROGRAM 12 . . .

Write a program to implement Ellipse generating algorithm

 
  
 
 
 // C program for implementing 
// Mid-Point Ellipse Drawing Algorithm 

#include  
#include
#include

void midptellipse(int rx, int ry, int xc, int yc) 
{ 

	float dx, dy, d1, d2, x, y; 
	x = 0; 
	y = ry; 

	// Initial decision parameter of region 1 
	d1 = (ry * ry) 
		- (rx * rx * ry) 
		+ (0.25 * rx * rx); 
	dx = 2 * ry * ry * x; 
	dy = 2 * rx * rx * y; 

	// For region 1 
	while (dx < dy) { 

		// Print points based on 4-way symmetry 
	//	printf("(%f, %f)\n", x + xc, y + yc); 
	//	printf("(%f, %f)\n", -x + xc, y + yc); 
	//	printf("(%f, %f)\n", x + xc, -y + yc); 
	//	printf("(%f, %f)\n", -x + xc, -y + yc); 
	putpixel(xc+x,yc-y,WHITE);
	      putpixel(xc-x,yc+y,WHITE);
	      putpixel(xc+x,yc+y,WHITE);
	      putpixel(xc-x,yc-y,WHITE);
		// Checking and updating value of 
		// decision parameter based on algorithm 
		if (d1 < 0) { 
			x++; 
			dx = dx + (2 * ry * ry); 
			d1 = d1 + dx + (ry * ry); 
		} 
		else { 
			x++; 
			y--; 
			dx = dx + (2 * ry * ry); 
			dy = dy - (2 * rx * rx); 
			d1 = d1 + dx - dy + (ry * ry); 
		} 
	} 

	// Decision parameter of region 2 
	d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) 
		+ ((rx * rx) * ((y - 1) * (y - 1))) 
		- (rx * rx * ry * ry); 

	// Plotting points of region 2 
	while (y >= 0) { 

		// printing points based on 4-way symmetry 
		//printf("(%f, %f)\n", x + xc, y + yc); 
		//printf("(%f, %f)\n", -x + xc, y + yc); 
		//printf("(%f, %f)\n", x + xc, -y + yc); 
		//printf("(%f, %f)\n", -x + xc, -y + yc); 
		   putpixel(xc+x,yc-y,WHITE);
	      putpixel(xc-x,yc+y,WHITE);
	      putpixel(xc+x,yc+y,WHITE);
	      putpixel(xc-x,yc-y,WHITE);

		// Checking and updating parameter 
		// value based on algorithm 
		if (d2 > 0) { 
			y--; 
			dy = dy - (2 * rx * rx); 
			d2 = d2 + (rx * rx) - dy; 
		} 
		else { 
			y--; 
			x++; 
			dx = dx + (2 * ry * ry); 
			dy = dy - (2 * rx * rx); 
			d2 = d2 + dx - dy + (rx * rx); 
		} 
	} 
} 

// Driver code 
int main() 
{ 
	int gd=DETECT,gm;
	int x,y,p;
	clrscr();
	initgraph(&gd,&gm,"c:\\turboc3\\bgi");
	// To draw a ellipse of major and 
	// minor radius 15, 10 centred at (50, 50) 
	midptellipse(10, 15, 50, 50); 
getch();
	return 0; 
}