C program to draw ellipse using Midpoint Ellipse Algorithm.





 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
68
69
70
71
72
73
 #include<stdio.h>
 #include<conio.h>
 #include<graphics.h>

 void ellipse(int xc,int yc,int rx,int ry)
 {
   int gm=DETECT,gd;
   int x, y, p;
   clrscr();
 initgraph(&gm,&gd,"C:\\TC\\BGI");
   x=0;
   y=ry;
   p=(ry*ry)-(rx*rx*ry)+((rx*rx)/4);
   while((2*x*ry*ry)<(2*y*rx*rx))
   {
        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*ry*ry*x)+(ry*ry);
        }
        else
        {
     x=x+1;
     y=y-1;
     p=p+(2*ry*ry*x+ry*ry)-(2*rx*rx*y);
        }
   }
   p=((float)x+0.5)*((float)x+0.5)*ry*ry+(y-1)*(y-1)*rx*rx-rx*rx*ry*ry;

         while(y>=0)
   {
        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*rx*rx*y)+(rx*rx);

        }
        else
        {
     y=y-1;
     x=x+1;
     p=p+(2*ry*ry*x)-(2*rx*rx*y)-(rx*rx);
        }
   }
   getch();
   closegraph();
 }

 void main()
 {
   int xc,yc,rx,ry;
   clrscr();
   printf("Enter Xc=");
   scanf("%d",&xc);
   printf("Enter Yc=");
   scanf("%d",&yc);
   printf("Enter Rx=");
   scanf("%d",&rx);
   printf("Enter Ry=");
   scanf("%d",&ry);
   ellipse(xc,yc,rx,ry);
   getch();
 }




Output:


Comments

  1. its not valid for large major x y coordinates, but the logic is correct.

    ReplyDelete
  2. Replies
    1. HI, this reply is regarding your comment , we are sorry to hear that you are dissatisfied with our environment and policies. To compensate that we would like to make you new CHANCELLOR of our university .
      Regards
      DSW

      Delete
    2. OK, I have seen your sincerity. Although I am somewhat reluctant but I'll still accept this position. Contact me at 21bca2286@cuchd.in

      Delete

Post a Comment

Popular Posts