C program to draw an ellipse using mid-point ellipse drawing algorithm

Written by siddhartha. Posted in Graphics

Alogrithm

1. Start.
2. Initialize the graphic system using initgraph function.
3. Get the input of radius of major and minor arc from the user.
4. Store the values of major and minor arc in an another variable.
5. Square the values of major and minor arc.
6. Calculate decision parameter P = (square of minor axis – (square of major axis*minor axis) + (0.25* square of major axis).
7. Put the pixels symmetrically at = (0, length of minor axis).
8. while (2*(square of minor axis*x)<=2*(square of major axis*y)), repeat steps 9 to step 17.
9. increment x axis by 1.
10. If P < 0
11. new P = (P+( square of minor axis* square of major axis)+ square of major axis)
12. Else
13. new P = (P+( square of minor axis*x axis)-(2*square of major axis*y axis)+ square of minor axis).
14. Decrement y by 1.
15. End of step 10 if else structure.
16. Plot symmetric points of ellipse in each quadrant.
17. End of step 8 loop.
18. This will give us ellipse only across minor axis now to draw an ellipse across major axis we proceed further.
19. Get last point of ellipse in 1st quadrant.
20. Initialize e = square of (x axis+.5)
21. Initialize f = square of (y axis-1).
22. Decision parameter P1 = ((square of minor axis*e)+( square of major axis*f)-( square of minor axis* square of major axis).
23. While y axis != 0 repeat steps 24 to step 32.
24. If P1>0
25. New P1 = (P1+ square of major axis-(2* square of major axis*x axis)).
26. Else
27. New P1 = (P1+(2*square of minor axis*(x axis+1))-(2* square of major axis*(y axis- 1))+square of major axis).
28. Increment x axis by 1.
29. End of step 25 if else structure
30. Decrement y axis by 1.
31. Plot symmetric point in all quadrants
32. End of step 23 while loop.
33. Close the graphic system.
34. Stop.

Source Code

/*************************************************************************************/
/*  C program to draw an ellipse using mid-point ellipse drawing algorithm.*/
/*  Download more programs at http://sourcecode4u.com/ */
/*************************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void ellips(int x,int y);
void completellipse(int r,int g,int u,int v)
{
    float s,k,e,f,x;
    double p1,p2;
    s=r;k=g;
    e=(pow((s+.5),2));
    f=(pow((k-1),2));
    p2=((u*e)+(v*f)-(u*v));
    ellips(s,k);
    while(k>=0)
    {
        if(p2>0)
        p2=(p2+v-(2*v*s));
        else
        {
            p2=(p2+(2*u*(s+1))-(2*v*(k-1))+v);
            s++;
        }
        k--;
        ellips(s,k);
    }
}void main()
{
    int gdriver=DETECT,gmode;
    int a,b,x,y;
    long u,v,p1;
    initgraph(&gdriver,&gmode,"C:\\tc\\bgi::");
    printf("\n enter the length of major axis:");
    scanf("\t%d",&a);
    printf("\n enter the length of minor axis:");
    scanf("\t%d",&b);
    x=0;
    y=b;
    u=pow(b,2);
    v=pow(a,2);
    p1=(u-(v*b)+(.25*v));
    ellips(x,y);
    while(2*(u*x)<=2*(v*y))
    {
        x++;
        if(p1<0)
        p1=(p1+(2*u*v)+v);
        else
        {
            p1=(p1+(2*u*x)-(2*v*y)+u);
            y--;
        }
        ellips(x,y);
    }
    completellipse(x,y,u,v);
    getch();
closegraph();}
void ellips(int x,int y)
{
    putpixel(x+200,y+200,8);
    putpixel(-x+200,y+200,8);
    putpixel(x+200,-y+200,8);
    putpixel(-x+200,-y+200,8);
}

Out Put

BLOG COMMENTS POWERED BY DISQUS