merge the formfield patch from ooo-build
[ooovba.git] / agg / source / agg_curves.cpp
blob34659e6d2824bb7878d5b59e9738599d28af89e4
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
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 //----------------------------------------------------------------------------
16 // classes curve3 and curve4
18 //----------------------------------------------------------------------------
20 #include <math.h>
21 #include "agg_curves.h"
23 namespace agg
26 //------------------------------------------------------------------------
27 void curve3::init(double x1, double y1,
28 double x2, double y2,
29 double x3, double y3)
31 m_start_x = x1;
32 m_start_y = y1;
33 m_end_x = x3;
34 m_end_y = y3;
36 double dx1 = x2 - x1;
37 double dy1 = y2 - y1;
38 double dx2 = x3 - x2;
39 double dy2 = y3 - y2;
41 double len = sqrt(dx1 * dx1 + dy1 * dy1) + sqrt(dx2 * dx2 + dy2 * dy2);
43 m_num_steps = int(len * 0.25 * m_scale);
45 if(m_num_steps < 2)
47 m_num_steps = 2;
50 double subdivide_step = 1.0 / m_num_steps;
51 double subdivide_step2 = subdivide_step * subdivide_step;
53 double tmpx = (x1 - x2 * 2.0 + x3) * subdivide_step2;
54 double tmpy = (y1 - y2 * 2.0 + y3) * subdivide_step2;
56 m_saved_fx = m_fx = x1;
57 m_saved_fy = m_fy = y1;
59 m_saved_dfx = m_dfx = tmpx + (x2 - x1) * (2.0 * subdivide_step);
60 m_saved_dfy = m_dfy = tmpy + (y2 - y1) * (2.0 * subdivide_step);
62 m_ddfx = tmpx * 2.0;
63 m_ddfy = tmpy * 2.0;
65 m_step = m_num_steps;
71 //------------------------------------------------------------------------
72 void curve3::rewind(unsigned)
74 if(m_num_steps == 0)
76 m_step = -1;
77 return;
79 m_step = m_num_steps;
80 m_fx = m_saved_fx;
81 m_fy = m_saved_fy;
82 m_dfx = m_saved_dfx;
83 m_dfy = m_saved_dfy;
89 //------------------------------------------------------------------------
90 unsigned curve3::vertex(double* x, double* y)
92 if(m_step < 0) return path_cmd_stop;
93 if(m_step == m_num_steps)
95 *x = m_start_x;
96 *y = m_start_y;
97 --m_step;
98 return path_cmd_move_to;
100 if(m_step == 0)
102 *x = m_end_x;
103 *y = m_end_y;
104 --m_step;
105 return path_cmd_line_to;
107 m_fx += m_dfx;
108 m_fy += m_dfy;
109 m_dfx += m_ddfx;
110 m_dfy += m_ddfy;
111 *x = m_fx;
112 *y = m_fy;
113 --m_step;
114 return path_cmd_line_to;
129 //------------------------------------------------------------------------
130 void curve4::init(double x1, double y1,
131 double x2, double y2,
132 double x3, double y3,
133 double x4, double y4)
135 m_start_x = x1;
136 m_start_y = y1;
137 m_end_x = x4;
138 m_end_y = y4;
140 double dx1 = x2 - x1;
141 double dy1 = y2 - y1;
142 double dx2 = x3 - x2;
143 double dy2 = y3 - y2;
144 double dx3 = x4 - x3;
145 double dy3 = y4 - y3;
147 double len = sqrt(dx1 * dx1 + dy1 * dy1) +
148 sqrt(dx2 * dx2 + dy2 * dy2) +
149 sqrt(dx3 * dx3 + dy3 * dy3);
151 m_num_steps = int(len * 0.25 * m_scale);
153 if(m_num_steps < 2)
155 m_num_steps = 2;
158 double subdivide_step = 1.0 / m_num_steps;
159 double subdivide_step2 = subdivide_step * subdivide_step;
160 double subdivide_step3 = subdivide_step * subdivide_step * subdivide_step;
162 double pre1 = 3.0 * subdivide_step;
163 double pre2 = 3.0 * subdivide_step2;
164 double pre4 = 6.0 * subdivide_step2;
165 double pre5 = 6.0 * subdivide_step3;
167 double tmp1x = x1 - x2 * 2.0 + x3;
168 double tmp1y = y1 - y2 * 2.0 + y3;
170 double tmp2x = (x2 - x3) * 3.0 - x1 + x4;
171 double tmp2y = (y2 - y3) * 3.0 - y1 + y4;
173 m_saved_fx = m_fx = x1;
174 m_saved_fy = m_fy = y1;
176 m_saved_dfx = m_dfx = (x2 - x1) * pre1 + tmp1x * pre2 + tmp2x * subdivide_step3;
177 m_saved_dfy = m_dfy = (y2 - y1) * pre1 + tmp1y * pre2 + tmp2y * subdivide_step3;
179 m_saved_ddfx = m_ddfx = tmp1x * pre4 + tmp2x * pre5;
180 m_saved_ddfy = m_ddfy = tmp1y * pre4 + tmp2y * pre5;
182 m_dddfx = tmp2x * pre5;
183 m_dddfy = tmp2y * pre5;
185 m_step = m_num_steps;
191 //------------------------------------------------------------------------
192 void curve4::rewind(unsigned)
194 if(m_num_steps == 0)
196 m_step = -1;
197 return;
199 m_step = m_num_steps;
200 m_fx = m_saved_fx;
201 m_fy = m_saved_fy;
202 m_dfx = m_saved_dfx;
203 m_dfy = m_saved_dfy;
204 m_ddfx = m_saved_ddfx;
205 m_ddfy = m_saved_ddfy;
212 //------------------------------------------------------------------------
213 unsigned curve4::vertex(double* x, double* y)
215 if(m_step < 0) return path_cmd_stop;
216 if(m_step == m_num_steps)
218 *x = m_start_x;
219 *y = m_start_y;
220 --m_step;
221 return path_cmd_move_to;
223 if(m_step == 0)
225 *x = m_end_x;
226 *y = m_end_y;
227 --m_step;
228 return path_cmd_line_to;
230 m_fx += m_dfx;
231 m_fy += m_dfy;
232 m_dfx += m_ddfx;
233 m_dfy += m_ddfy;
234 m_ddfx += m_dddfx;
235 m_ddfy += m_dddfy;
236 *x = m_fx;
237 *y = m_fy;
238 --m_step;
239 return path_cmd_line_to;