Midpoint Ellipse Algorithm C++ Code
Learn here Midpoint Ellipse Algorithm C++ Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include<graphics.h> #include<iostream> using namespace std; void ellipse(double xc,double yc,double a,double b) { double p=b*b-a*a*b+a*a/4; double x=0, y=b; while(2.0*b*b*x <= 2.0*a*a*y) { if(p < 0) { x++; p = p+2*b*b*x+b*b; } else { x++; y--; p = p+2*b*b*x-2*a*a*y-b*b; } putpixel(xc+x,yc+y,WHITE); putpixel(xc+x,yc-y,WHITE); putpixel(xc-x,yc+y,WHITE); putpixel(xc-x,yc-y,WHITE); delay(100); } p=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b; while(y > 0) { if(p <= 0) { x++; y--; p = p+2*b*b*x-2*a*a*y+a*a; } else { y--; p = p-2*a*a*y+a*a; } putpixel(xc+x,yc+y,WHITE); putpixel(xc+x,yc-y,WHITE); putpixel(xc-x,yc+y,WHITE); putpixel(xc-x,yc-y,WHITE); delay(100); } } int main() { int gd = DETECT, gm; double xc,yc,x,y, a,b; initgraph(&gd, &gm, ""); cout<<"Enter coordinates of centre: "; cin>>xc>>yc; cout<<"Enter length of major and minor axix a,b: "; cin>>a>>b; ellipse(xc, yc, a, b); getch(); closegraph(); } |
Thanks for viewing this code.