Draw Ellipse - Midpoint Ellipse Algorithm - C++ program - Graphics & Multimedia Lab

Midpoint Ellipse Algorithm 

Ellipse Drawing C++ program - Graphics & Multimedia Lab

Sourcecode:
#include<graphics.h>

#include<iostream>

using namespace std;



int points[20000];

int i=0;

int count=0;



void ellipsePoints(int x, int y, int value)

 {

 int maxx = getmaxx()/2;

 int maxy = getmaxy()/2;

 putpixel(maxx+x,maxy+y,value);

 points[i]=maxx+x;

 points[i+1]=maxy+y;

 i+=2;



 putpixel(maxx-x,maxy+y,value);

 putpixel(maxx+x,maxy-y,value);

 putpixel(maxx-x,maxy-y,value);

 }



void midPointEllipse(float a,float b,int value)

 {

 double d2;

 int x=0;

 int y=b;

 double dl=b*b-(a*a*b)+(0.25*a*a);

 putpixel(x,y,value);

 while((a*a*(y-0.5))>(b*b*(x+1)))

  {

  if(dl<0)

  dl+=b*b*(2*x+3);

  else

   {

   dl+=b*b*(2*x+3)+a*a*(-2*y+2);

   y--;

   }

  x++;

  ellipsePoints(x,y,value);

  }

 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--;

  ellipsePoints(x,y,value);

  }

 }



int main()

 {

 int gd=DETECT, gm,np;

 FILE *fp;

 int ax1,ax2;

 initgraph(&gd, &gm, NULL);

 fp=fopen("elinput.txt","r");

 if(fp==NULL)

  {

  printf("ERROR !");

  getch();

  exit(0);

  }



 fscanf(fp,"%d %d",&ax1,&ax2);

 fclose(fp);

 midPointEllipse(ax1,ax2,WHITE);



 count=i;np=0;

 fp=fopen("eloutput.txt","w");

 fprintf(fp,"%s","Plotted points are:-\n");

 for(i=0;i<count;i+=2)

 {

  fprintf(fp,"(%d %d) ",points[i],points[i+1]);

  np++;

  if(np==5){fprintf(fp,"\n");np=0;}

 }

 fclose(fp);



 getch();

 closegraph();

 return 0;

 }


Output:
nn@linuxmint ~ $ g++ lab4.cpp -lgraph
nn@linuxmint ~ $ ./a.out




"elinput.txt"
75 50


"eloutput.txt"
Plotted points are:-
(320 289) (321 289) (322 289) (323 289) (324 289) 
(325 289) (326 289) (327 289) (328 289) (329 289) 
(330 288) (331 288) (332 288) (333 288) (334 288) 
(335 288) (336 288) (337 288) (338 287) (339 287) 
(340 287) (341 287) (342 287) (343 286) (344 286) 
(345 286) (346 286) (347 285) (348 285) (349 285) 
(350 285) (351 284) (352 284) (353 284) (354 283) 
(355 283) (356 282) (357 282) (358 282) (359 281) 
(360 281) (361 280) (362 280) (363 279) (364 279) 
(365 278) (366 278) (367 277) (368 277) (369 276) 
(370 276) (371 275) (372 274) (373 274) (374 273) 
(375 272) (376 271) (377 271) (378 270) (379 269) 
(380 268) (381 267) (382 266) (383 265) (384 264) 
(385 263) (386 262) (386 261) (387 260) (388 259) 
(388 258) (389 257) (390 256) (390 255) (391 254) 
(391 253) (391 252) (392 251) (392 250) (392 249) 
(393 248) (393 247) (393 246) (393 245) (394 244) 
(394 243) (394 242) (394 241) (394 240) (394 239) 

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...