Home > Cpp Graphics > C++ Graphics – 05 – Ellipse program by Midpoint Algorithm

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

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();
}
```

Categories: Cpp Graphics