c program to draw an ellipse – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sat, 20 Aug 2022 15:11:15 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg c program to draw an ellipse – Programmerbay https://programmerbay.com 32 32 C program to draw ellipse using Midpoint Ellipse Algorithm https://programmerbay.com/c-program-to-draw-ellipse-using-mid-point-ellipse-drawing-algorithm/ https://programmerbay.com/c-program-to-draw-ellipse-using-mid-point-ellipse-drawing-algorithm/#comments Sat, 23 Jul 2022 08:36:08 +0000 https://www.programmerbay.com/?p=1631 Midpoint ellipse algorithms uses symmetry property of an ellipse in order draw it. It plots points . Here is the program to draw an ellipse using midpoint ellipse drawing algorithm.

Program to draw ellipse using Midpoint Ellipse Algorithm in C

Program:

#include<stdio.h>
#include<graphics.h>
void main(){
      long x,y,x_center,y_center;
      long a_sqr,b_sqr, fx,fy, d,a,b,tmp1,tmp2;
      int g_driver=DETECT,g_mode;
      clrscr();

    initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");
    printf("********* MID POINT ELLIPSE ALGORITHM *********");
    printf("\n\n Enter coordinate x and y = ");
    scanf("%ld%ld",&x_center,&y_center);
    printf("\n Now enter constants a and b = ");
    scanf("%ld%ld",&a,&b);
    x=0;
    y=b;
    a_sqr=a*a;
    b_sqr=b*b;
    fx=2*b_sqr*x;
    fy=2*a_sqr*y;
  d=b_sqr-(a_sqr*b)+(a_sqr*0.25);
  do
   {
  putpixel(x_center+x,y_center+y,1);
  putpixel(x_center-x,y_center-y,1);
  putpixel(x_center+x,y_center-y,1);
  putpixel(x_center-x,y_center+y,1);

   if(d<0)
    {
  d=d+fx+b_sqr;
    }
   else
  {
  y=y-1;
  d=d+fx+-fy+b_sqr;
  fy=fy-(2*a_sqr);
  }
  x=x+1;
  fx=fx+(2*b_sqr);
  delay(10);

   }
   while(fx<fy);
   tmp1=(x+0.5)*(x+0.5);
   tmp2=(y-1)*(y-1);
   d=b_sqr*tmp1+a_sqr*tmp2-(a_sqr*b_sqr);
   do
   {
  putpixel(x_center+x,y_center+y,1);
  putpixel(x_center-x,y_center-y,1);
  putpixel(x_center+x,y_center-y,1);
  putpixel(x_center-x,y_center+y,1);

   if(d>=0)
  d=d-fy+a_sqr;
   else

  {
  x=x+1;
  d=d+fx-fy+a_sqr;
  fx=fx+(2*b_sqr);
  }
   y=y-1;
   fy=fy-(2*a_sqr);
   }
   while(y>0);
   getch();
   closegraph();
}

 

Input:

X= 300, Y=300

a = 100, b=150

Output:

mid point ellipse drawing 2

Input:

X=300,Y=300

a=200,b=100

Output:

mid point ellipse drawing

]]>
https://programmerbay.com/c-program-to-draw-ellipse-using-mid-point-ellipse-drawing-algorithm/feed/ 1