Advertisement
zoro-10

5B. Develop the program for the mid-point ellipse drawing algorithm

Apr 1st, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include<graphics.h>
  2. #include<math.h>
  3. #include<iostream.h>
  4. #include<conio.h>
  5. void main()
  6. {
  7. int gd=DETECT,gm;
  8. int xc,yc,x,y;
  9. float p;
  10. long rx,ry;
  11. initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
  12. cout<<"Enter coordinates of centre : ";
  13. cin>>xc>>yc;
  14. cout<<"Enter x,y radius of ellipse: ";
  15. cin>>rx>>ry;
  16. //Region 1
  17. p=ry*ry-rx*rx*ry+rx*rx/4;
  18. x=0;y=ry;
  19. while(2.0*ry*ry*x <= 2.0*rx*rx*y)
  20. {
  21. if(p < 0)
  22. {
  23. x++;
  24. p=p+2*ry*ry*x+ry*ry;
  25. }
  26. else
  27. {
  28. x++;y--;
  29. p=p+2*ry*ry*x-2*rx*rx*y-ry*ry;
  30. }
  31. putpixel(xc+x,yc+y,RED);
  32. putpixel(xc+x,yc-y,RED);
  33. putpixel(xc-x,yc+y,RED);
  34. putpixel(xc-x,yc-y,RED);
  35. }
  36. //Region 2
  37. p=ry*ry*(x+0.5)*(x+0.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry;
  38. while(y > 0)
  39. {
  40. if(p <= 0)
  41. {
  42. x++;y--;
  43. p=p+2*ry*ry*x-2*rx*rx*y+rx*rx;
  44. }
  45. else
  46. {
  47. y--;
  48. p=p-2*rx*rx*y+rx*rx;
  49. }
  50. putpixel(xc+x,yc+y,RED);
  51. putpixel(xc+x,yc-y,RED);
  52. putpixel(xc-x,yc+y,RED);
  53. putpixel(xc-x,yc-y,RED);
  54. }
  55. getch();
  56. closegraph();
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement