Midpoint ellipse algorithm is a method for drawing ellipses in computer graphics. This method is modified from Bresenham’s algorithm so it is sometimes known as Bresenham's circle algorithm.
The advantage of this modified method is that only addition operations are required in the program loops. This leads to simple and fast implementation in all processors. The algorithm uses symmetry of ellipse and mid point algorithm to implement one quadrant only. We divide the quadrant into two regions and the boundary of two regions is the point at which the curve has a slope of -1. We proceed by taking unit steps in the x direction to the point P(where curve has a slope of -1), then taking unit steps in the y direction and applying midpoint algorithm at every step.
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Title: Midpoint Ellipse Algorithm | |
Description: C Program to draw an Ellipse using Midpoint Ellipse Algorithm | |
Author: Saideep Dicholkar | |
*/ | |
#include<stdio.h> | |
#include<graphics.h> | |
void main() | |
{ | |
int gd=DETECT,gm; | |
float p,x,y,xc,yc,a,b; | |
initgraph(&gd,&gm,"C:\\tc\\bgi"); | |
cleardevice(); | |
printf("Enter xc, yc:\n"); | |
scanf("%f%f",&xc,&yc); | |
printf("Enter a, b:\n"); | |
scanf("%f%f",&a,&b); | |
x=0; | |
y=b; | |
//Region 1 | |
p=(b*b)-(a*a*b)+(0.25*a*a); | |
do | |
{ | |
putpixel(xc+x,yc+y,WHITE); | |
putpixel(xc+x,yc-y,WHITE); | |
putpixel(xc-x,yc+y,WHITE); | |
putpixel(xc-x,yc-y,WHITE); | |
if(p<0) | |
{ | |
x=x+1; | |
p=p+2*b*b*x+b*b; | |
} | |
else | |
{ | |
x=x+1; | |
y=y-1; | |
p=p+2*b*b*x-2*a*a*y+b*b; | |
} | |
}while(2*b*b*x<2*a*a*y); | |
//Region 2 | |
p=(b*b*(x+0.5)*(x+0.5))+((y-1)*(y-1)*a*a-a*a*b*b); | |
do | |
{ | |
putpixel(xc+x,yc+y,WHITE); | |
putpixel(xc+x,yc-y,WHITE); | |
putpixel(xc-x,yc+y,WHITE); | |
putpixel(xc-x,yc-y,WHITE); | |
if(p>0) | |
{ | |
y=y-1; | |
p=p-2*a*a*y+a*a; | |
} | |
else | |
{ | |
x=x+1; | |
y=y-1; | |
p=p-2*a*a*y+2*b*b*x+a*a; | |
} | |
}while(y!=0); | |
getch(); | |
closegraph(); | |
restorecrtmode(); | |
} |
If you can't see the code, then Enable JavaScript in you browser
Output:
Note: Just copy the files "CGA.bgi" &" EGAVGA.bgi" from bgi folder to bin. So, if you have copied these 2 files then you can skip writing "C:\\tc\\bgi" in initgraph with just ""(2 double quotes).
Comments
Post a Comment