1//----------------------------------------------------------------------------
2// Anti-Grain Geometry - Version 2.4
3// Copyright (C) 2002-2005 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// Arc vertex generator
17//
18//----------------------------------------------------------------------------
19
20#include <math.h>
21#include "agg_arc.h"
22
23
24namespace agg
25{
26    //------------------------------------------------------------------------
27    arc::arc(double x,  double y,
28             double rx, double ry,
29             double a1, double a2,
30             bool ccw) :
31        m_x(x), m_y(y), m_rx(rx), m_ry(ry), m_scale(1.0)
32    {
33        normalize(a1, a2, ccw);
34    }
35
36    //------------------------------------------------------------------------
37    void arc::init(double x,  double y,
38                   double rx, double ry,
39                   double a1, double a2,
40                   bool ccw)
41    {
42        m_x   = x;  m_y  = y;
43        m_rx  = rx; m_ry = ry;
44        normalize(a1, a2, ccw);
45    }
46
47    //------------------------------------------------------------------------
48    void arc::approximation_scale(double s)
49    {
50        m_scale = s;
51        if(m_initialized)
52        {
53            normalize(m_start, m_end, m_ccw);
54        }
55    }
56
57    //------------------------------------------------------------------------
58    void arc::rewind(unsigned)
59    {
60        m_path_cmd = path_cmd_move_to;
61        m_angle = m_start;
62    }
63
64    //------------------------------------------------------------------------
65    unsigned arc::vertex(double* x, double* y)
66    {
67        if(is_stop(m_path_cmd)) return path_cmd_stop;
68        if((m_angle < m_end - m_da/4) != m_ccw)
69        {
70            *x = m_x + cos(m_end) * m_rx;
71            *y = m_y + sin(m_end) * m_ry;
72            m_path_cmd = path_cmd_stop;
73            return path_cmd_line_to;
74        }
75
76        *x = m_x + cos(m_angle) * m_rx;
77        *y = m_y + sin(m_angle) * m_ry;
78
79        m_angle += m_da;
80
81        unsigned pf = m_path_cmd;
82        m_path_cmd = path_cmd_line_to;
83        return pf;
84    }
85
86    //------------------------------------------------------------------------
87    void arc::normalize(double a1, double a2, bool ccw)
88    {
89        double ra = (fabs(m_rx) + fabs(m_ry)) / 2;
90        m_da = acos(ra / (ra + 0.125 / m_scale)) * 2;
91        if(ccw)
92        {
93            while(a2 < a1) a2 += pi * 2.0;
94        }
95        else
96        {
97            while(a1 < a2) a1 += pi * 2.0;
98            m_da = -m_da;
99        }
100        m_ccw   = ccw;
101        m_start = a1;
102        m_end   = a2;
103        m_initialized = true;
104    }
105
106}
107