Thursday, June 21, 2018

Implementation of circle drawing algorithm


  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<graphics.h>
  4. #include<dos.h> 
  5. void main()
  6. {
  7. int gm,gd=DETECT; initgraph(&gd,&gm,"c:\\tc\\bgi"); 
  8. int x1,y1;
  9. int x=0,y=0,r=0; float d;
  10. //textbackground(15);
  11. //textcolor(8);
  12. //setbkcolor(15);
  13. //setcolor(8);
  14. cout<<"Enter pixels for centre of a circle (x,y): "; cin>>x1>>y1;
  15. cout<<"\nEnter radius of circle: "; cin>>r;
  16. x=0;
  17. y=r; d=1.25-r;
  18. line(0,getmaxy()/2,getmaxx(),getmaxy()/2); line(getmaxx()/2,0,getmaxx()/2,getmaxy()); 
  19. do
  20. {
  21. putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1+y),8); 
  22. putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1+y),8);
  23. putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1-y),8); 

  24. putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1-y),8); 

  25. putpixel(getmaxx()/2+(x1+y),getmaxy()/2-(y1+x),8); 

  26. putpixel(getmaxx()/2+(x1+y),getmaxy()/2-(y1-x),8); 

  27. putpixel(getmaxx()/2+(x1-y),getmaxy()/2-(y1-x),8); 

  28. putpixel(getmaxx()/2+(x1-y),getmaxy()/2-(y1+x),8); 

  29. if(d<0)
  30. {
  31. x+=1;
  32. y=y; 
  33. d+=2*x+1;}
  34. else
  35. {
  36.              x+=1; 
  37.              y-=1;
  38. d+=2*(x-y)+1;
  39. }
  40. delay(50);
  41. }while(x<y);
  42. getch();
  43. }

No comments:

Post a Comment