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