drawEllipse.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*****************************************************************
  2. Fichier : DrawEllipse.cpp
  3. Date : 07/01/11
  4. Version : 1.000
  5. Description : Classe de dessin des ellipses
  6. |*****************************************************************/
  7. #include <cstdlib>
  8. #include "drawEllipse.h"
  9. CDrawEllipse::CDrawEllipse()
  10. {
  11. top = 0;
  12. bottom = 0;
  13. left = 0;
  14. right = 0;
  15. }
  16. CDrawEllipse::~CDrawEllipse()
  17. {
  18. }
  19. void CDrawEllipse::PutPixEllipse(void (*ptFonction) (int, int), float xc,float yc,float x,float y)
  20. {
  21. ptFonction( (int) (xc + x), (int) (yc + y));
  22. ptFonction( (int) (xc - x), (int) (yc - y));
  23. ptFonction( (int) (xc + x), (int) (yc - y));
  24. ptFonction( (int) (xc - x), (int) (yc + y));
  25. }
  26. // Algorithme MidPoint de dessin d'une Ellipse
  27. void CDrawEllipse::myDrawEllipse(void (*ptFonction) (int, int))
  28. {
  29. float x = 0, y = 0;
  30. double p1, p2, t1, t2;
  31. int k;
  32. float rx, ry, xc, yc;
  33. rx = (float) (right - left) / 2;
  34. ry = (float) (bottom - top) / 2;
  35. xc = left + rx;
  36. yc = top + ry;
  37. y = ry;
  38. p1 = ry * ry - rx * rx * ry + 0.25 * rx * rx;
  39. PutPixEllipse(ptFonction, xc, yc, x, y);
  40. for (k = 0; (2 * ry * ry * x) <= (2 * rx * rx * y); k++)
  41. {
  42. t1 = 2 * ry * ry * x + 2 * ry * ry;
  43. t2 = 2 * rx * rx * y - 2 * rx * rx;
  44. if (p1 < 0)
  45. {
  46. p1 = p1 + t1 + ry * ry;
  47. }
  48. else
  49. {
  50. p1 = p1 + t1 - t2 + ry * ry;
  51. y--;
  52. }
  53. x++;
  54. PutPixEllipse(ptFonction, xc, yc, x, y);
  55. }
  56. p2 = ry * ry * ( x + 0.5 ) * ( x + 0.5 ) + rx * rx * (y - 1) * (y - 1) - rx * rx * ry * ry;
  57. PutPixEllipse(ptFonction, xc, yc, x, y);
  58. for (k = 0 ; y >= 0 ; k++)
  59. {
  60. t1 = 2 * ry * ry * x + 2 * ry * ry;
  61. t2 = 2 * rx * rx * y - 2 * rx * rx;
  62. if (p2 > 0)
  63. {
  64. p2 = p2 - t2 + rx * rx;
  65. }
  66. else
  67. {
  68. p2 = p2 + t1 - t2 + rx * rx;
  69. x++;
  70. }
  71. y--;
  72. PutPixEllipse(ptFonction, xc, yc, x, y);
  73. }
  74. }