1
2 //----------------------------------------------------------------------------
3 // Anti-Grain Geometry - Version 2.3
4 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
5 //
6 // Permission to copy, use, modify, sell and distribute this software
7 // is granted provided this copyright notice appears in all copies.
8 // This software is provided "as is" without express or implied
9 // warranty, and with no claim as to its suitability for any purpose.
10 //
11 //----------------------------------------------------------------------------
12 // Contact: mcseem@antigrain.com
13 // mcseemagg@yahoo.com
14 // http://www.antigrain.com
15 //----------------------------------------------------------------------------
16 //
17 // Stroke math
18 //
19 //----------------------------------------------------------------------------
20 #ifndef AGG_STROKE_MATH_INCLUDED
21 #define AGG_STROKE_MATH_INCLUDED
22 #include "agg_math.h"
23 #include "agg_vertex_sequence.h"
24 namespace agg
25 {
26 enum line_cap_e {
27 butt_cap,
28 square_cap,
29 round_cap
30 };
31 enum line_join_e {
32 miter_join = 0,
33 miter_join_revert = 1,
34 miter_join_round = 4,
35 round_join = 2,
36 bevel_join = 3
37 };
38 enum inner_join_e {
39 inner_bevel,
40 inner_miter,
41 inner_jag,
42 inner_round
43 };
44 const float stroke_theta = 1.0f / 1000.0f;
45 template<class VertexConsumer>
stroke_calc_arc(VertexConsumer & out_vertices,float x,float y,float dx1,float dy1,float dx2,float dy2,float width,float approximation_scale)46 void stroke_calc_arc(VertexConsumer& out_vertices,
47 float x, float y,
48 float dx1, float dy1,
49 float dx2, float dy2,
50 float width,
51 float approximation_scale)
52 {
53 typedef typename VertexConsumer::value_type coord_type;
54 float a1 = atan2(dy1, dx1);
55 float a2 = atan2(dy2, dx2);
56 float da = a1 - a2;
57 bool ccw = da > 0 && da < FX_PI;
58 if(width < 0) {
59 width = -width;
60 }
61 da = acos(width / (width + ((1.0f / 8) / approximation_scale))) * 2;
62 out_vertices.add(coord_type(x + dx1, y + dy1));
63 if (da > 0) {
64 if (!ccw) {
65 if (a1 > a2) {
66 a2 += 2 * FX_PI;
67 }
68 a2 -= da / 4;
69 a1 += da;
70 while (a1 < a2) {
71 out_vertices.add(
72 coord_type(x + (width * cos(a1)), y + (width * sin(a1))));
73 a1 += da;
74 }
75 } else {
76 if (a1 < a2) {
77 a2 -= 2 * FX_PI;
78 }
79 a2 += da / 4;
80 a1 -= da;
81 while (a1 > a2) {
82 out_vertices.add(
83 coord_type(x + (width * cos(a1)), y + (width * sin(a1))));
84 a1 -= da;
85 }
86 }
87 }
88 out_vertices.add(coord_type(x + dx2, y + dy2));
89 }
90 template<class VertexConsumer>
stroke_calc_miter(VertexConsumer & out_vertices,const vertex_dist & v0,const vertex_dist & v1,const vertex_dist & v2,float dx1,float dy1,float dx2,float dy2,float width,line_join_e line_join,float miter_limit,float approximation_scale)91 void stroke_calc_miter(VertexConsumer& out_vertices,
92 const vertex_dist& v0,
93 const vertex_dist& v1,
94 const vertex_dist& v2,
95 float dx1, float dy1,
96 float dx2, float dy2,
97 float width,
98 line_join_e line_join,
99 float miter_limit,
100 float approximation_scale)
101 {
102 typedef typename VertexConsumer::value_type coord_type;
103 float xi = v1.x;
104 float yi = v1.y;
105 bool miter_limit_exceeded = true;
106 if(calc_intersection(v0.x + dx1, v0.y - dy1,
107 v1.x + dx1, v1.y - dy1,
108 v1.x + dx2, v1.y - dy2,
109 v2.x + dx2, v2.y - dy2,
110 &xi, &yi)) {
111 float d1 = calc_distance(v1.x, v1.y, xi, yi);
112 float lim = width * miter_limit;
113 if(d1 <= lim) {
114 out_vertices.add(coord_type(xi, yi));
115 miter_limit_exceeded = false;
116 }
117 } else {
118 float x2 = v1.x + dx1;
119 float y2 = v1.y - dy1;
120 if ((((x2 - v0.x) * dy1) - ((v0.y - y2) * dx1) < 0) !=
121 (((x2 - v2.x) * dy1) - ((v2.y - y2) * dx1) < 0)) {
122 out_vertices.add(coord_type(v1.x + dx1, v1.y - dy1));
123 miter_limit_exceeded = false;
124 }
125 }
126 if(miter_limit_exceeded) {
127 switch(line_join) {
128 case miter_join_revert:
129 out_vertices.add(coord_type(v1.x + dx1, v1.y - dy1));
130 out_vertices.add(coord_type(v1.x + dx2, v1.y - dy2));
131 break;
132 case miter_join_round:
133 stroke_calc_arc(out_vertices,
134 v1.x, v1.y, dx1, -dy1, dx2, -dy2,
135 width, approximation_scale);
136 break;
137 default:
138 out_vertices.add(coord_type(v1.x + dx1 + (dy1 * miter_limit),
139 v1.y - dy1 + (dx1 * miter_limit)));
140 out_vertices.add(coord_type(v1.x + dx2 - (dy2 * miter_limit),
141 v1.y - dy2 - (dx2 * miter_limit)));
142 break;
143 }
144 }
145 }
146 template<class VertexConsumer>
stroke_calc_cap(VertexConsumer & out_vertices,const vertex_dist & v0,const vertex_dist & v1,float len,line_cap_e line_cap,float width,float approximation_scale)147 void stroke_calc_cap(VertexConsumer& out_vertices,
148 const vertex_dist& v0,
149 const vertex_dist& v1,
150 float len,
151 line_cap_e line_cap,
152 float width,
153 float approximation_scale)
154 {
155 typedef typename VertexConsumer::value_type coord_type;
156 out_vertices.remove_all();
157 float dx1 = (v1.y - v0.y) / len;
158 float dy1 = (v1.x - v0.x) / len;
159 float dx2 = 0;
160 float dy2 = 0;
161 dx1 = dx1 * width;
162 dy1 = dy1 * width;
163 if(line_cap != round_cap) {
164 if(line_cap == square_cap) {
165 dx2 = dy1;
166 dy2 = dx1;
167 }
168 out_vertices.add(coord_type(v0.x - dx1 - dx2, v0.y + dy1 - dy2));
169 out_vertices.add(coord_type(v0.x + dx1 - dx2, v0.y - dy1 - dy2));
170 } else {
171 float a1 = atan2(dy1, -dx1);
172 float a2 = a1 + FX_PI;
173 float da = acos(width / (width + ((1.0f / 8) / approximation_scale))) * 2;
174 out_vertices.add(coord_type(v0.x - dx1, v0.y + dy1));
175 a1 += da;
176 a2 -= da / 4;
177 while (a1 < a2) {
178 out_vertices.add(
179 coord_type(v0.x + (width * cos(a1)), v0.y + (width * sin(a1))));
180 a1 += da;
181 }
182 out_vertices.add(coord_type(v0.x + dx1, v0.y - dy1));
183 }
184 }
185 template<class VertexConsumer>
stroke_calc_join(VertexConsumer & out_vertices,const vertex_dist & v0,const vertex_dist & v1,const vertex_dist & v2,float len1,float len2,float width,line_join_e line_join,inner_join_e inner_join,float miter_limit,float inner_miter_limit,float approximation_scale)186 void stroke_calc_join(VertexConsumer& out_vertices,
187 const vertex_dist& v0,
188 const vertex_dist& v1,
189 const vertex_dist& v2,
190 float len1,
191 float len2,
192 float width,
193 line_join_e line_join,
194 inner_join_e inner_join,
195 float miter_limit,
196 float inner_miter_limit,
197 float approximation_scale)
198 {
199 typedef typename VertexConsumer::value_type coord_type;
200 float dx1, dy1, dx2, dy2;
201 dx1 = width * (v1.y - v0.y) / len1;
202 dy1 = width * (v1.x - v0.x) / len1;
203 dx2 = width * (v2.y - v1.y) / len2;
204 dy2 = width * (v2.x - v1.x) / len2;
205 out_vertices.remove_all();
206 if(calc_point_location(v0.x, v0.y, v1.x, v1.y, v2.x, v2.y) > 0) {
207 switch(inner_join) {
208 default:
209 out_vertices.add(coord_type(v1.x + dx1, v1.y - dy1));
210 out_vertices.add(coord_type(v1.x + dx2, v1.y - dy2));
211 break;
212 case inner_miter:
213 stroke_calc_miter(out_vertices,
214 v0, v1, v2, dx1, dy1, dx2, dy2,
215 width,
216 miter_join_revert,
217 inner_miter_limit,
218 1.0f);
219 break;
220 case inner_jag:
221 case inner_round: {
222 float d = (dx1 - dx2) * (dx1 - dx2) + (dy1 - dy2) * (dy1 - dy2);
223 if(d < len1 * len1 && d < len2 * len2) {
224 stroke_calc_miter(out_vertices,
225 v0, v1, v2, dx1, dy1, dx2, dy2,
226 width,
227 miter_join_revert,
228 inner_miter_limit,
229 1.0f);
230 } else {
231 if(inner_join == inner_jag) {
232 out_vertices.add(coord_type(v1.x + dx1, v1.y - dy1));
233 out_vertices.add(coord_type(v1.x, v1.y ));
234 out_vertices.add(coord_type(v1.x + dx2, v1.y - dy2));
235 } else {
236 out_vertices.add(coord_type(v1.x + dx1, v1.y - dy1));
237 out_vertices.add(coord_type(v1.x, v1.y ));
238 stroke_calc_arc(out_vertices,
239 v1.x, v1.y, dx2, -dy2, dx1, -dy1,
240 width, approximation_scale);
241 out_vertices.add(coord_type(v1.x, v1.y ));
242 out_vertices.add(coord_type(v1.x + dx2, v1.y - dy2));
243 }
244 }
245 }
246 break;
247 }
248 } else {
249 switch(line_join) {
250 case miter_join:
251 case miter_join_revert:
252 case miter_join_round:
253 stroke_calc_miter(out_vertices,
254 v0, v1, v2, dx1, dy1, dx2, dy2,
255 width,
256 line_join,
257 miter_limit,
258 approximation_scale);
259 break;
260 case round_join:
261 stroke_calc_arc(out_vertices,
262 v1.x, v1.y, dx1, -dy1, dx2, -dy2,
263 width, approximation_scale);
264 break;
265 default:
266 out_vertices.add(coord_type(v1.x + dx1, v1.y - dy1));
267 out_vertices.add(coord_type(v1.x + dx2, v1.y - dy2));
268 break;
269 }
270 }
271 }
272 }
273 #endif
274