1 //---------------------------------------------------------------------------- 2 // Anti-Grain Geometry - Version 2.2 3 // Copyright (C) 2002-2004 Maxim Shemanarev (http://www.antigrain.com) 4 // 5 // Permission to copy, use, modify, sell and distribute this software 6 // is granted provided this copyright notice appears in all copies. 7 // This software is provided "as is" without express or implied 8 // warranty, and with no claim as to its suitability for any purpose. 9 // 10 //---------------------------------------------------------------------------- 11 // Contact: mcseem@antigrain.com 12 // mcseemagg@yahoo.com 13 // http://www.antigrain.com 14 //---------------------------------------------------------------------------- 15 // 16 // class ellipse 17 // 18 //---------------------------------------------------------------------------- 19 20 #ifndef AGG_ELLIPSE_INCLUDED 21 #define AGG_ELLIPSE_INCLUDED 22 23 #include "agg_basics.h" 24 #include <math.h> 25 26 namespace agg 27 { 28 29 //----------------------------------------------------------------ellipse 30 class ellipse 31 { 32 public: 33 ellipse() : m_x(0.0), m_y(0.0), m_rx(1.0), m_ry(1.0), m_num(4), m_step(0) {} 34 ellipse(double x, double y, double rx, double ry, unsigned num_steps) 35 : m_x(x), m_y(y), m_rx(rx), m_ry(ry), m_num(num_steps), m_step(0) {} 36 37 void init(double x, double y, double rx, double ry, unsigned num_steps); 38 void approximation_scale(double scale); 39 void rewind(unsigned id); 40 unsigned vertex(double* x, double* y); 41 42 private: 43 double m_x; 44 double m_y; 45 double m_rx; 46 double m_ry; 47 unsigned m_num; 48 unsigned m_step; 49 }; 50 51 52 //------------------------------------------------------------------------ 53 inline void ellipse::init(double x, double y, double rx, double ry, unsigned num_steps) 54 { 55 m_x = x; 56 m_y = y; 57 m_rx = rx; 58 m_ry = ry; 59 m_num = num_steps; 60 m_step = 0; 61 } 62 63 //------------------------------------------------------------------------ 64 inline void ellipse::approximation_scale(double scale) 65 { 66 m_num = unsigned((fabs(m_rx) + fabs(m_ry) + 6.0) * scale); 67 if(m_num < 6) m_num = 6; 68 } 69 70 //------------------------------------------------------------------------ 71 inline void ellipse::rewind(unsigned) 72 { 73 m_step = 0; 74 } 75 76 //------------------------------------------------------------------------ 77 inline unsigned ellipse::vertex(double* x, double* y) 78 { 79 if(m_step == m_num) 80 { 81 ++m_step; 82 return path_cmd_end_poly | path_flags_close | path_flags_ccw; 83 } 84 if(m_step > m_num) return path_cmd_stop; 85 double angle = double(m_step) / double(m_num) * 2.0 * pi; 86 *x = m_x + cos(angle) * m_rx; 87 *y = m_y + sin(angle) * m_ry; 88 m_step++; 89 return ((m_step == 1) ? path_cmd_move_to : path_cmd_line_to); 90 } 91 92 } 93 94 95 96 #endif 97 98 99