Midpoint Circle Algorithm - C++ Program - Graphics & Multimedia Lab


Midpoint Circle Algorithm 

Circle Drawing - C++ program - Graphics & Multimedia Lab


Sourcecode:

#include<graphics.h>

#include<iostream>

using namespace std;



int points[10000];

int i=0;

int count=0;



void circlePoints(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+y,maxy+x,value);

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

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

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

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

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

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

 }



void midPointCircle(int radius,int value)

{

 int x = 0, y = radius;

 double d = 5.0/4.0-radius;

 while(y>x)

 {

 if(d<0)

  d += 2.0*x+3.0;

 else

    {

  d += 2.0*(x-y)+5.0;

  y--;

 }

 x++;

 circlePoints(x,y,value);

  }

}



int main()

{

 int gd=DETECT, gm,np;

 FILE *fp;

 int radius;

 initgraph(&gd, &gm, NULL);

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

 if(fp==NULL)

  {

  printf("Error in reading file!");

  getch();

  exit(0);

  }



 fscanf(fp,"%d",&radius);

 fclose(fp);

 midPointCircle(radius, WHITE);



 count=i;

 np=0;

 fp=fopen("outcircle.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++ lab3.cpp -lgraph
nn@linuxmint ~ $ ./a.out




"incircle.txt"
100


"outcircle.txt"
Plotted points are:-
(320 339) (321 339) (322 339) (323 339) (324 339) 
(325 339) (326 339) (327 339) (328 339) (329 338) 
(330 338) (331 338) (332 338) (333 338) (334 338) 
(335 338) (336 338) (337 337) (338 337) (339 337) 
(340 337) (341 337) (342 336) (343 336) (344 336) 
(345 336) (346 335) (347 335) (348 335) (349 334) 
(350 334) (351 334) (352 333) (353 333) (354 333) 
(355 332) (356 332) (357 331) (358 331) (359 331) 
(360 330) (361 330) (362 329) (363 329) (364 328) 
(365 328) (366 327) (367 327) (368 326) (369 326) 
(370 325) (371 324) (372 324) (373 323) (374 323) 
(375 322) (376 321) (377 320) (378 320) (379 319) 
(380 318) (381 317) (382 317) (383 316) (384 315) 
(385 314) (386 313) (387 312) (388 311) (389 310) 
(390 309) 

3 comments:

Related Posts Plugin for WordPress, Blogger...