1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
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.
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 // mcseemagg@yahoo.com
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
17 #include "agg_vertex_sequence.h"
18 #include "agg_trans_single_path.h"
23 //------------------------------------------------------------------------
24 trans_single_path::trans_single_path() :
28 m_preserve_x_scale(true)
32 //------------------------------------------------------------------------
33 void trans_single_path::reset()
35 m_src_vertices
.remove_all();
40 //------------------------------------------------------------------------
41 void trans_single_path::move_to(double x
, double y
)
43 if(m_status
== initial
)
45 m_src_vertices
.modify_last(vertex_dist(x
, y
));
46 m_status
= making_path
;
54 //------------------------------------------------------------------------
55 void trans_single_path::line_to(double x
, double y
)
57 if(m_status
== making_path
)
59 m_src_vertices
.add(vertex_dist(x
, y
));
64 //------------------------------------------------------------------------
65 void trans_single_path::finalize_path()
67 if(m_status
== making_path
&& m_src_vertices
.size() > 1)
73 if(m_src_vertices
.size() > 2)
75 if(m_src_vertices
[m_src_vertices
.size() - 2].dist
* 10.0 <
76 m_src_vertices
[m_src_vertices
.size() - 3].dist
)
78 d
= m_src_vertices
[m_src_vertices
.size() - 3].dist
+
79 m_src_vertices
[m_src_vertices
.size() - 2].dist
;
81 m_src_vertices
[m_src_vertices
.size() - 2] =
82 m_src_vertices
[m_src_vertices
.size() - 1];
84 m_src_vertices
.remove_last();
85 m_src_vertices
[m_src_vertices
.size() - 2].dist
= d
;
90 m_src_vertices
.close(false);
91 for(i
= 0; i
< m_src_vertices
.size(); i
++)
93 vertex_dist
& v
= m_src_vertices
[i
];
98 m_kindex
= (m_src_vertices
.size() - 1) / dist
;
105 //------------------------------------------------------------------------
106 double trans_single_path::total_length() const
108 if(m_base_length
>= 1e-10) return m_base_length
;
109 return (m_status
== ready
) ?
110 m_src_vertices
[m_src_vertices
.size() - 1].dist
:
115 //------------------------------------------------------------------------
116 void trans_single_path::transform(double *x
, double *y
) const
118 if(m_status
== ready
)
120 if(m_base_length
> 1e-10)
122 *x
*= m_src_vertices
[m_src_vertices
.size() - 1].dist
/
134 // Extrapolation on the left
135 //--------------------------
136 x1
= m_src_vertices
[0].x
;
137 y1
= m_src_vertices
[0].y
;
138 dx
= m_src_vertices
[1].x
- x1
;
139 dy
= m_src_vertices
[1].y
- y1
;
140 dd
= m_src_vertices
[1].dist
- m_src_vertices
[0].dist
;
144 if(*x
> m_src_vertices
[m_src_vertices
.size() - 1].dist
)
146 // Extrapolation on the right
147 //--------------------------
148 unsigned i
= m_src_vertices
.size() - 2;
149 unsigned j
= m_src_vertices
.size() - 1;
150 x1
= m_src_vertices
[j
].x
;
151 y1
= m_src_vertices
[j
].y
;
152 dx
= x1
- m_src_vertices
[i
].x
;
153 dy
= y1
- m_src_vertices
[i
].y
;
154 dd
= m_src_vertices
[j
].dist
- m_src_vertices
[i
].dist
;
155 d
= *x
- m_src_vertices
[j
].dist
;
160 //--------------------------
162 unsigned j
= m_src_vertices
.size() - 1;
163 if(m_preserve_x_scale
)
166 for(i
= 0; (j
- i
) > 1; )
168 if(*x
< m_src_vertices
[k
= (i
+ j
) >> 1].dist
)
177 d
= m_src_vertices
[i
].dist
;
178 dd
= m_src_vertices
[j
].dist
- d
;
183 i
= (unsigned)floor(*x
* m_kindex
);
185 dd
= m_src_vertices
[j
].dist
- m_src_vertices
[i
].dist
;
186 d
= ((*x
* m_kindex
) - i
) * dd
;
188 x1
= m_src_vertices
[i
].x
;
189 y1
= m_src_vertices
[i
].y
;
190 dx
= m_src_vertices
[j
].x
- x1
;
191 dy
= m_src_vertices
[j
].y
- y1
;
193 double x2
= x1
+ dx
* d
/ dd
;
194 double y2
= y1
+ dy
* d
/ dd
;
195 *x
= x2
- *y
* dy
/ dd
;
196 *y
= y2
+ *y
* dx
/ dd
;