vexyl 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +202 -0
- data/NOTICE +8 -0
- data/README.md +72 -0
- data/exe/vexyl +8 -0
- data/lib/vexyl/adapter.rb +451 -0
- data/lib/vexyl/canonical.rb +440 -0
- data/lib/vexyl/cli.rb +251 -0
- data/lib/vexyl/element.rb +81 -0
- data/lib/vexyl/errors.rb +47 -0
- data/lib/vexyl/flag.rb +66 -0
- data/lib/vexyl/geometry.rb +253 -0
- data/lib/vexyl/json.rb +75 -0
- data/lib/vexyl/kdl.rb +709 -0
- data/lib/vexyl/limits.rb +120 -0
- data/lib/vexyl/locate.rb +133 -0
- data/lib/vexyl/model.rb +568 -0
- data/lib/vexyl/numbers.rb +151 -0
- data/lib/vexyl/parser.rb +1902 -0
- data/lib/vexyl/render.rb +1036 -0
- data/lib/vexyl/render_options.rb +63 -0
- data/lib/vexyl/sanitizer.rb +401 -0
- data/lib/vexyl/shapes.rb +953 -0
- data/lib/vexyl/share.rb +119 -0
- data/lib/vexyl/share_dictionary.rb +124 -0
- data/lib/vexyl/version.rb +7 -0
- data/lib/vexyl/xml.rb +570 -0
- data/lib/vexyl.rb +101 -0
- metadata +71 -0
data/lib/vexyl/shapes.rb
ADDED
|
@@ -0,0 +1,953 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Naomi Persephone Amethyst <naomi@amethyst.name>
|
|
3
|
+
|
|
4
|
+
module Vexyl
|
|
5
|
+
# Works out the polygons and paths that layers and shapes paint.
|
|
6
|
+
module Shapes
|
|
7
|
+
# Stops a render that turns out to be too much, deep inside geometry that otherwise can't fail.
|
|
8
|
+
class Refusal < StandardError; end
|
|
9
|
+
|
|
10
|
+
# A rectangle with its origin at the current coordinate origin. An emblem's frame has no size,
|
|
11
|
+
# and its center is its origin; local is then how far its content could need to reach to cover
|
|
12
|
+
# everything visible.
|
|
13
|
+
class Region
|
|
14
|
+
attr_reader :w, :h, :local
|
|
15
|
+
|
|
16
|
+
def initialize(w, h, local = 0.0)
|
|
17
|
+
@w = w.to_f
|
|
18
|
+
@h = h.to_f
|
|
19
|
+
@local = local.to_f
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# The rectangle a mask has to cover.
|
|
23
|
+
def bounds
|
|
24
|
+
return [-@local, -@local, 2 * @local, 2 * @local] if @local > 0
|
|
25
|
+
[0.0, 0.0, @w, @h]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def center
|
|
29
|
+
Geometry::Vec.new(@w / 2, @h / 2)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def reach
|
|
33
|
+
Math.hypot(@w, @h)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def corners
|
|
37
|
+
[Geometry::Vec.new(0.0, 0.0), Geometry::Vec.new(@w, 0.0), Geometry::Vec.new(@w, @h),
|
|
38
|
+
Geometry::Vec.new(0.0, @h)]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# A shape resolved to coordinates.
|
|
43
|
+
class Geom
|
|
44
|
+
# path: SVG path data, for shapes that polygons can't draw exactly. transform: places a path,
|
|
45
|
+
# whose scale strokes have to allow for.
|
|
46
|
+
attr_accessor :circle, :ring, :c, :r, :inner, :poly, :path, :evenodd, :transform, :scale
|
|
47
|
+
|
|
48
|
+
def initialize
|
|
49
|
+
@circle = false
|
|
50
|
+
@ring = false
|
|
51
|
+
@c = nil
|
|
52
|
+
@r = 0.0
|
|
53
|
+
@inner = 0.0
|
|
54
|
+
@poly = nil
|
|
55
|
+
@path = ""
|
|
56
|
+
@evenodd = false
|
|
57
|
+
@transform = ""
|
|
58
|
+
@scale = 0.0
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def self.poly(poly)
|
|
62
|
+
g = new
|
|
63
|
+
g.poly = poly
|
|
64
|
+
g
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def self.path(d)
|
|
68
|
+
g = new
|
|
69
|
+
g.path = d
|
|
70
|
+
g
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def stroke_scale
|
|
74
|
+
@scale > 0 ? @scale : 1.0
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Which bars are drawn exactly is said by any object answering call(i), such as a lambda,
|
|
79
|
+
# which bar_polys takes as its third argument, or nil.
|
|
80
|
+
|
|
81
|
+
module_function
|
|
82
|
+
|
|
83
|
+
# Formats a coordinate to a fixed precision, so output is stable and free of floating-point noise.
|
|
84
|
+
# It's v * 1e6 rounded half up, as six decimal places, trailing zeros stripped, in plain
|
|
85
|
+
# notation; beyond the range of a long, v rounded half-even to an integer.
|
|
86
|
+
def num(v)
|
|
87
|
+
v = v.to_f
|
|
88
|
+
scaled = v * 1e6
|
|
89
|
+
if scaled.abs < 9e18
|
|
90
|
+
# floor(scaled + 1/2), computed exactly.
|
|
91
|
+
l = scaled.floor
|
|
92
|
+
l += 1 if scaled - l >= 0.5
|
|
93
|
+
return "0" if l == 0
|
|
94
|
+
neg = l < 0
|
|
95
|
+
ip, fp = l.abs.divmod(1_000_000)
|
|
96
|
+
s = neg ? "-#{ip}" : ip.to_s
|
|
97
|
+
return s if fp == 0
|
|
98
|
+
digits = 6
|
|
99
|
+
while fp % 10 == 0
|
|
100
|
+
fp /= 10
|
|
101
|
+
digits -= 1
|
|
102
|
+
end
|
|
103
|
+
return "#{s}.#{fp.to_s.rjust(digits, "0")}"
|
|
104
|
+
end
|
|
105
|
+
v.to_r.round(half: :even).to_s
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Writes a scale, whose size says nothing about how many places it needs: a path drawn in a
|
|
109
|
+
# box tens of thousands wide, on a grid of a few units, is scaled by a very small number.
|
|
110
|
+
# It's the Float's exact value to 10 significant digits, half-even, trailing zeros
|
|
111
|
+
# stripped, in plain notation.
|
|
112
|
+
def factor(v)
|
|
113
|
+
v = v.to_f
|
|
114
|
+
return "0" if v == 0
|
|
115
|
+
r = v.to_r.abs
|
|
116
|
+
e = Math.log10(v.abs).floor
|
|
117
|
+
e -= 1 while r < pow10(e)
|
|
118
|
+
e += 1 while r >= pow10(e + 1)
|
|
119
|
+
scale = 9 - e
|
|
120
|
+
n = (r * pow10(scale)).round(half: :even)
|
|
121
|
+
while n % 10 == 0
|
|
122
|
+
n /= 10
|
|
123
|
+
scale -= 1
|
|
124
|
+
end
|
|
125
|
+
digits = n.to_s
|
|
126
|
+
s = if scale <= 0
|
|
127
|
+
digits + ("0" * -scale)
|
|
128
|
+
elsif digits.length > scale
|
|
129
|
+
"#{digits[0...-scale]}.#{digits[-scale..]}"
|
|
130
|
+
else
|
|
131
|
+
"0.#{"0" * (scale - digits.length)}#{digits}"
|
|
132
|
+
end
|
|
133
|
+
v < 0 ? "-#{s}" : s
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def pow10(k)
|
|
137
|
+
k >= 0 ? 10**k : Rational(1, 10**-k)
|
|
138
|
+
end
|
|
139
|
+
private_class_method :pow10
|
|
140
|
+
|
|
141
|
+
# The remainder of a by b with the sign of a, as C's fmod gives it.
|
|
142
|
+
def fmod(a, b)
|
|
143
|
+
m = a.abs % b.abs
|
|
144
|
+
a < 0 || (a == 0 && 1.0 / a < 0) ? -m : m
|
|
145
|
+
end
|
|
146
|
+
private_class_method :fmod
|
|
147
|
+
|
|
148
|
+
def bounds(poly)
|
|
149
|
+
x0 = y0 = Float::INFINITY
|
|
150
|
+
x1 = y1 = -Float::INFINITY
|
|
151
|
+
poly.each do |p|
|
|
152
|
+
x0 = p.x if p.x < x0
|
|
153
|
+
y0 = p.y if p.y < y0
|
|
154
|
+
x1 = p.x if p.x > x1
|
|
155
|
+
y1 = p.y if p.y > y1
|
|
156
|
+
end
|
|
157
|
+
[x0, y0, x1 - x0, y1 - y0]
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# The direction of an axis, and the normal bars are listed along.
|
|
161
|
+
def axis(r, along, rotation)
|
|
162
|
+
l = r.reach
|
|
163
|
+
case along
|
|
164
|
+
when "hoist"
|
|
165
|
+
dir = Geometry::Vec.new(0.0, 1.0)
|
|
166
|
+
normal = Geometry::Vec.new(1.0, 0.0)
|
|
167
|
+
when "bend"
|
|
168
|
+
dir = Geometry::Vec.new(r.w / l, r.h / l)
|
|
169
|
+
normal = Geometry::Vec.new(r.h / l, -r.w / l)
|
|
170
|
+
when "bend-sinister"
|
|
171
|
+
dir = Geometry::Vec.new(r.w / l, -r.h / l)
|
|
172
|
+
normal = Geometry::Vec.new(r.h / l, r.w / l)
|
|
173
|
+
else
|
|
174
|
+
dir = Geometry::Vec.new(1.0, 0.0)
|
|
175
|
+
normal = Geometry::Vec.new(0.0, 1.0)
|
|
176
|
+
end
|
|
177
|
+
[dir.rotate(rotation), normal.rotate(rotation)]
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Clips a convex polygon to a region (Sutherland-Hodgman).
|
|
181
|
+
def clip_convex(poly, r)
|
|
182
|
+
# Each edge: [on x (true) or y, the line's value, inside is below (<=) or above (>=)].
|
|
183
|
+
[[true, 0.0, false], [true, r.w, true], [false, 0.0, false], [false, r.h, true]].each do |on_x, at, below|
|
|
184
|
+
out = []
|
|
185
|
+
n = poly.size
|
|
186
|
+
n.times do |i|
|
|
187
|
+
cur = poly[i]
|
|
188
|
+
prev = poly[(i + n - 1) % n]
|
|
189
|
+
cv = on_x ? cur.x : cur.y
|
|
190
|
+
pv = on_x ? prev.x : prev.y
|
|
191
|
+
inside = below ? cv <= at : cv >= at
|
|
192
|
+
was = below ? pv <= at : pv >= at
|
|
193
|
+
if inside && was
|
|
194
|
+
out << cur
|
|
195
|
+
elsif inside
|
|
196
|
+
out << clip_cross(on_x, at, prev, cur)
|
|
197
|
+
out << cur
|
|
198
|
+
elsif was
|
|
199
|
+
out << clip_cross(on_x, at, prev, cur)
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
poly = out
|
|
203
|
+
end
|
|
204
|
+
poly
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def clip_cross(on_x, at, a, b)
|
|
208
|
+
if on_x
|
|
209
|
+
Geometry::Vec.new(at, a.y + (b.y - a.y) * (at - a.x) / (b.x - a.x))
|
|
210
|
+
else
|
|
211
|
+
Geometry::Vec.new(a.x + (b.x - a.x) * (at - a.y) / (b.y - a.y), at)
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
private_class_method :clip_cross
|
|
215
|
+
|
|
216
|
+
# The region a layer's axis is measured in and the center it's drawn about.
|
|
217
|
+
def frame(f, r)
|
|
218
|
+
f.nil? ? r : Region.new(f.w, f.h)
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def frame_center(f, r)
|
|
222
|
+
f.nil? ? r.center : Geometry::Vec.new(f.x, f.y)
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# Far enough from a point to cross the whole region, wherever the point is.
|
|
226
|
+
def reach_from(c, r)
|
|
227
|
+
d = c.sub(r.center)
|
|
228
|
+
r.reach + Math.hypot(d.x, d.y)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# The rectangle between two offsets along normal.
|
|
232
|
+
def strip_poly(c, dir, normal, t0, t1, reach)
|
|
233
|
+
[
|
|
234
|
+
c.add(normal.mul(t0)).sub(dir.mul(reach)),
|
|
235
|
+
c.add(normal.mul(t0)).add(dir.mul(reach)),
|
|
236
|
+
c.add(normal.mul(t1)).add(dir.mul(reach)),
|
|
237
|
+
c.add(normal.mul(t1)).sub(dir.mul(reach))
|
|
238
|
+
]
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# One polygon per bar. Each runs from its bar's start to the far side of the region, so the
|
|
242
|
+
# next bar paints over it and antialiasing can't leave a seam showing what's beneath. A gradient
|
|
243
|
+
# spans the shape it fills, so a bar for which exact is true stays its true width.
|
|
244
|
+
# exact: nil, or anything answering call(i) with true or false.
|
|
245
|
+
def bar_polys(b, r, exact)
|
|
246
|
+
frame = frame(b.fixed, r)
|
|
247
|
+
c = frame_center(b.fixed, r)
|
|
248
|
+
dir, normal = axis(frame, b.along, b.rotate)
|
|
249
|
+
lo = Float::INFINITY
|
|
250
|
+
hi = -Float::INFINITY
|
|
251
|
+
r.corners.each do |corner|
|
|
252
|
+
offset = corner.sub(c).dot(normal)
|
|
253
|
+
lo = offset if offset < lo
|
|
254
|
+
hi = offset if offset > hi
|
|
255
|
+
end
|
|
256
|
+
total = 0.0
|
|
257
|
+
b.ratio.each { |part| total += part }
|
|
258
|
+
reach = reach_from(c, r)
|
|
259
|
+
polys = []
|
|
260
|
+
t = lo
|
|
261
|
+
count = b.ratio.length
|
|
262
|
+
count.times do |i|
|
|
263
|
+
nxt = t + (hi - lo) * b.ratio[i] / total
|
|
264
|
+
stop = hi
|
|
265
|
+
is_exact = !exact.nil? && exact.call(i) ? true : false
|
|
266
|
+
stop = nxt if is_exact
|
|
267
|
+
if b.line.nil?
|
|
268
|
+
from = t
|
|
269
|
+
to = stop
|
|
270
|
+
if b.rotate != 0
|
|
271
|
+
# The outermost lines pass through corners, where rounding in the angle could
|
|
272
|
+
# leave a sliver either side, so the outer ends reach well past the region.
|
|
273
|
+
from -= reach if i == 0
|
|
274
|
+
to = hi + reach if stop == hi || i == count - 1
|
|
275
|
+
end
|
|
276
|
+
polys << clip_convex(strip_poly(c, dir, normal, from, to, reach), r)
|
|
277
|
+
t = nxt
|
|
278
|
+
next
|
|
279
|
+
end
|
|
280
|
+
# A shaped edge isn't convex, so clipping is left to the region's own clip.
|
|
281
|
+
margin = b.line.depth + 1
|
|
282
|
+
near = [c.add(normal.mul(lo - margin)).sub(dir.mul(reach)),
|
|
283
|
+
c.add(normal.mul(lo - margin)).add(dir.mul(reach))]
|
|
284
|
+
near = wave_edge(c, dir, normal, t, axis_span(frame, b.along), reach, b.line) if i > 0
|
|
285
|
+
far = [c.add(normal.mul(hi + margin)).add(dir.mul(reach)),
|
|
286
|
+
c.add(normal.mul(hi + margin)).sub(dir.mul(reach))]
|
|
287
|
+
if is_exact && i < count - 1
|
|
288
|
+
far = wave_edge(c, dir, normal, nxt, axis_span(frame, b.along), reach, b.line).reverse
|
|
289
|
+
end
|
|
290
|
+
polys << (near + far)
|
|
291
|
+
t = nxt
|
|
292
|
+
end
|
|
293
|
+
polys
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
# One polygon per color, from the outermost inward, which is the order they paint in.
|
|
297
|
+
def chevron_polys(ch, r)
|
|
298
|
+
raw_chevrons(ch, r)[0].map { |poly| clip_convex(poly, r) }
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
# Each chevron's polygon reaching well past the region, and each chevron alone between its apex
|
|
302
|
+
# and the next, for when gaps have to show. Returns [nested, bands].
|
|
303
|
+
def raw_chevrons(ch, r)
|
|
304
|
+
case ch.from
|
|
305
|
+
when "fly"
|
|
306
|
+
origin = Geometry::Vec.new(r.w, r.h / 2)
|
|
307
|
+
u = Geometry::Vec.new(-1.0, 0.0)
|
|
308
|
+
v = Geometry::Vec.new(0.0, 1.0)
|
|
309
|
+
edge = r.h
|
|
310
|
+
when "top"
|
|
311
|
+
origin = Geometry::Vec.new(r.w / 2, 0.0)
|
|
312
|
+
u = Geometry::Vec.new(0.0, 1.0)
|
|
313
|
+
v = Geometry::Vec.new(1.0, 0.0)
|
|
314
|
+
edge = r.w
|
|
315
|
+
when "bottom"
|
|
316
|
+
origin = Geometry::Vec.new(r.w / 2, r.h)
|
|
317
|
+
u = Geometry::Vec.new(0.0, -1.0)
|
|
318
|
+
v = Geometry::Vec.new(1.0, 0.0)
|
|
319
|
+
edge = r.w
|
|
320
|
+
else
|
|
321
|
+
origin = Geometry::Vec.new(0.0, r.h / 2)
|
|
322
|
+
u = Geometry::Vec.new(1.0, 0.0)
|
|
323
|
+
v = Geometry::Vec.new(0.0, 1.0)
|
|
324
|
+
edge = r.h
|
|
325
|
+
end
|
|
326
|
+
reach = r.reach
|
|
327
|
+
unless ch.apex.nil?
|
|
328
|
+
shift = ch.apex - edge / 2
|
|
329
|
+
origin = origin.add(v.mul(shift))
|
|
330
|
+
reach += shift.abs
|
|
331
|
+
end
|
|
332
|
+
run = ch.depth.to_f
|
|
333
|
+
run = (edge / 2) / Math.tan(ch.angle * Math::PI / 180) if ch.angle != 0
|
|
334
|
+
slope = run / (edge / 2)
|
|
335
|
+
total = 0.0
|
|
336
|
+
ch.ratio.each { |part| total += part }
|
|
337
|
+
apexes = []
|
|
338
|
+
sum = 0.0
|
|
339
|
+
ch.ratio.each do |part|
|
|
340
|
+
sum += part
|
|
341
|
+
apexes << ch.depth * sum / total
|
|
342
|
+
end
|
|
343
|
+
center = r.center
|
|
344
|
+
rotation = ch.rotate
|
|
345
|
+
place = lambda do |pu, pv|
|
|
346
|
+
point = origin.add(u.mul(pu)).add(v.mul(pv))
|
|
347
|
+
center.add(point.sub(center).rotate(rotation))
|
|
348
|
+
end
|
|
349
|
+
nested = []
|
|
350
|
+
bands = []
|
|
351
|
+
(apexes.length - 1).downto(0) do |i|
|
|
352
|
+
a = apexes[i]
|
|
353
|
+
arm_end = a - slope * reach
|
|
354
|
+
back = (arm_end < 0 ? arm_end : 0.0) - reach
|
|
355
|
+
nested << [place.call(a, 0.0), place.call(arm_end, reach), place.call(back, reach),
|
|
356
|
+
place.call(back, -reach), place.call(arm_end, -reach)]
|
|
357
|
+
if i == 0
|
|
358
|
+
bands << nested[-1]
|
|
359
|
+
next
|
|
360
|
+
end
|
|
361
|
+
inner = apexes[i - 1]
|
|
362
|
+
inner_end = inner - slope * reach
|
|
363
|
+
bands << [place.call(a, 0.0), place.call(arm_end, reach), place.call(inner_end, reach),
|
|
364
|
+
place.call(inner, 0.0), place.call(inner_end, -reach), place.call(arm_end, -reach)]
|
|
365
|
+
end
|
|
366
|
+
[nested, bands]
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
# A band's rectangle, clipped to the region unless it will be stroked.
|
|
370
|
+
def band_poly(r, along, rotation, width, offset, stroked)
|
|
371
|
+
placed_band_poly(r, nil, along, rotation, width, offset, stroked)
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
def placed_band_poly(r, fixed, along, rotation, width, offset, stroked)
|
|
375
|
+
frame = frame(fixed, r)
|
|
376
|
+
c = frame_center(fixed, r)
|
|
377
|
+
ax = axis(frame, along, rotation)
|
|
378
|
+
poly = strip_poly(c, ax[0], ax[1], offset - width / 2.0, offset + width / 2.0, reach_from(c, r))
|
|
379
|
+
return poly if stroked
|
|
380
|
+
clip_convex(poly, r)
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
# A cross's vertical and horizontal arms, with their ends extended by a distance.
|
|
384
|
+
def cross_polys(c, r, extend)
|
|
385
|
+
a, w, b = c.fly_parts
|
|
386
|
+
x0 = r.w * a / (a + w + b)
|
|
387
|
+
x1 = r.w * (a + w) / (a + w + b)
|
|
388
|
+
d, h, e = c.hoist_parts
|
|
389
|
+
y0 = r.h * d / (d + h + e)
|
|
390
|
+
y1 = r.h * (d + h) / (d + h + e)
|
|
391
|
+
[Geometry.rect_poly(x0, -extend, x1, r.h + extend), Geometry.rect_poly(-extend, y0, r.w + extend, y1)]
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
def position(s, r)
|
|
395
|
+
return r.center if s.at.nil?
|
|
396
|
+
Geometry::Vec.new(s.at.fly, s.at.hoist)
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
def place_all(points, place)
|
|
400
|
+
points.map { |p| place.call(p) }
|
|
401
|
+
end
|
|
402
|
+
private_class_method :place_all
|
|
403
|
+
|
|
404
|
+
# Resolves a shape at a center, with its rotation added to the extra.
|
|
405
|
+
def shape_geom(s, center, extra)
|
|
406
|
+
rotation = s.rotate + extra
|
|
407
|
+
place = ->(p) { center.add(p.rotate(rotation)) }
|
|
408
|
+
case s.kind
|
|
409
|
+
when "disc"
|
|
410
|
+
g = Geom.new
|
|
411
|
+
g.circle = true
|
|
412
|
+
g.c = center
|
|
413
|
+
g.r = s.radius.to_f
|
|
414
|
+
g
|
|
415
|
+
when "ring"
|
|
416
|
+
return Geom.path(ring_arc_path(s, center, rotation)) if s.has_arc
|
|
417
|
+
g = Geom.new
|
|
418
|
+
g.ring = true
|
|
419
|
+
g.c = center
|
|
420
|
+
g.r = s.radius.to_f
|
|
421
|
+
g.inner = s.inner.to_f
|
|
422
|
+
g
|
|
423
|
+
when "rect"
|
|
424
|
+
x = s.fly / 2.0
|
|
425
|
+
y = s.hoist / 2.0
|
|
426
|
+
Geom.poly(place_all(Geometry.rect_poly(-x, -y, x, y), place))
|
|
427
|
+
when "plus"
|
|
428
|
+
a = s.radius.to_f
|
|
429
|
+
w = s.width / 2.0
|
|
430
|
+
Geom.poly(place_all([
|
|
431
|
+
Geometry::Vec.new(-w, -a), Geometry::Vec.new(w, -a), Geometry::Vec.new(w, -w), Geometry::Vec.new(a, -w), Geometry::Vec.new(a, w), Geometry::Vec.new(w, w),
|
|
432
|
+
Geometry::Vec.new(w, a), Geometry::Vec.new(-w, a), Geometry::Vec.new(-w, w), Geometry::Vec.new(-a, w), Geometry::Vec.new(-a, -w), Geometry::Vec.new(-w, -w)
|
|
433
|
+
], place))
|
|
434
|
+
when "triangle"
|
|
435
|
+
Geom.poly(place_all(star_points(3, s.radius, 0.0), place))
|
|
436
|
+
when "star"
|
|
437
|
+
return Geom.path(star_chords(s.points, s.radius, place)) if s.lines > 0
|
|
438
|
+
Geom.poly(place_all(star_points(s.points, s.radius, s.inner), place))
|
|
439
|
+
when "heart"
|
|
440
|
+
Geom.path(heart_path(s.radius, s.spread, place))
|
|
441
|
+
when "crescent"
|
|
442
|
+
Geom.path(crescent_path(s.radius, s.inner, s.offset, place))
|
|
443
|
+
when "sun"
|
|
444
|
+
Geom.path(sun_path(s, center, place))
|
|
445
|
+
when "lozenge"
|
|
446
|
+
x = s.fly / 2.0
|
|
447
|
+
y = s.hoist / 2.0
|
|
448
|
+
Geom.poly(place_all([Geometry::Vec.new(0.0, -y), Geometry::Vec.new(x, 0.0), Geometry::Vec.new(0.0, y), Geometry::Vec.new(-x, 0.0)], place))
|
|
449
|
+
when "ellipse"
|
|
450
|
+
Geom.path(ellipse_path(center, s.fly / 2.0, s.hoist / 2.0, rotation))
|
|
451
|
+
when "path"
|
|
452
|
+
path_geom(s, center, rotation)
|
|
453
|
+
when "cardioid"
|
|
454
|
+
Geom.poly(place_all(cardioid_points(s.radius), place))
|
|
455
|
+
when "polygon"
|
|
456
|
+
Geom.poly(s.vertices.map { |p| Geometry::Vec.new(p.fly, p.hoist) })
|
|
457
|
+
else
|
|
458
|
+
Geom.new
|
|
459
|
+
end
|
|
460
|
+
end
|
|
461
|
+
|
|
462
|
+
# A heart with its lobes either side of the origin and its tip below, whose straight sides meet
|
|
463
|
+
# at a right angle and touch the lobes. place: a lambda taking and returning a Vec.
|
|
464
|
+
def heart_path(radius, spread, place)
|
|
465
|
+
radius = radius.to_f
|
|
466
|
+
half = spread / 2.0
|
|
467
|
+
edge = radius / Math.sqrt(2)
|
|
468
|
+
tip = place.call(Geometry::Vec.new(0.0, half + radius * Math.sqrt(2)))
|
|
469
|
+
right = place.call(Geometry::Vec.new(half + edge, edge))
|
|
470
|
+
left = place.call(Geometry::Vec.new(-half - edge, edge))
|
|
471
|
+
notch = place.call(Geometry::Vec.new(0.0, -Math.sqrt(radius * radius - half * half)))
|
|
472
|
+
# Each arc runs from 45 degrees below the lobe's side, around to the notch.
|
|
473
|
+
large = 225 - Math.acos(half / radius) * 180 / Math::PI > 180 ? 1 : 0
|
|
474
|
+
r = num(radius)
|
|
475
|
+
"M#{num(tip.x)} #{num(tip.y)}L#{num(right.x)} #{num(right.y)}" \
|
|
476
|
+
"A#{r} #{r} 0 #{large} 0 #{num(notch.x)} #{num(notch.y)}" \
|
|
477
|
+
"A#{r} #{r} 0 #{large} 0 #{num(left.x)} #{num(left.y)}Z"
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
# Lays out n outer points clockwise from up. An inner radius of 0 gives a regular polygon.
|
|
481
|
+
def star_points(n, radius, inner)
|
|
482
|
+
points = []
|
|
483
|
+
step = 360.0 / n
|
|
484
|
+
up = Geometry::Vec.new(0.0, -radius)
|
|
485
|
+
down = Geometry::Vec.new(0.0, -inner)
|
|
486
|
+
n.times do |i|
|
|
487
|
+
a = i * step
|
|
488
|
+
points << up.rotate(a)
|
|
489
|
+
points << down.rotate(a + step / 2) if inner > 0
|
|
490
|
+
end
|
|
491
|
+
points
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
# Samples a cardioid with its cusp at the origin and its lobe below. 180 segments keep the error
|
|
495
|
+
# under the format's tolerance for any radius up to the flag's hoist.
|
|
496
|
+
def cardioid_points(radius)
|
|
497
|
+
segments = 180
|
|
498
|
+
points = []
|
|
499
|
+
segments.times do |i|
|
|
500
|
+
t = 2 * Math::PI * i / segments
|
|
501
|
+
rho = radius * (1 - Math.cos(t)) / 2
|
|
502
|
+
points << Geometry::Vec.new(rho * Math.sin(t), -rho * Math.cos(t))
|
|
503
|
+
end
|
|
504
|
+
points
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
# A disc with another cut from it, offset toward the fly.
|
|
508
|
+
def crescent_path(radius, inner, offset, place)
|
|
509
|
+
radius = radius.to_f
|
|
510
|
+
inner = inner.to_f
|
|
511
|
+
offset = offset.to_f
|
|
512
|
+
x = (radius * radius - inner * inner + offset * offset) / (2 * offset)
|
|
513
|
+
y = Math.sqrt([radius * radius - x * x, 0.0].max)
|
|
514
|
+
top = place.call(Geometry::Vec.new(x, -y))
|
|
515
|
+
bottom = place.call(Geometry::Vec.new(x, y))
|
|
516
|
+
outer_large = x > 0 ? 1 : 0
|
|
517
|
+
inner_large = x > offset ? 1 : 0
|
|
518
|
+
"M#{num(top.x)} #{num(top.y)}" \
|
|
519
|
+
"A#{num(radius)} #{num(radius)} 0 #{outer_large} 0 #{num(bottom.x)} #{num(bottom.y)}" \
|
|
520
|
+
"A#{num(inner)} #{num(inner)} 0 #{inner_large} 1 #{num(top.x)} #{num(top.y)}Z"
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
# A disc and triangular rays, all wound the same way so that where they overlap stays filled.
|
|
524
|
+
def sun_path(s, center, place)
|
|
525
|
+
b = +circle_path(center, s.disc)
|
|
526
|
+
base = s.disc + s.gap
|
|
527
|
+
step = 360.0 / s.points
|
|
528
|
+
half = step / 2 * s.spread
|
|
529
|
+
s.points.times do |i|
|
|
530
|
+
a = i * step
|
|
531
|
+
tip = place.call(Geometry::Vec.new(0.0, -s.radius).rotate(a))
|
|
532
|
+
left = place.call(Geometry::Vec.new(0.0, -base).rotate(a - half))
|
|
533
|
+
right = place.call(Geometry::Vec.new(0.0, -base).rotate(a + half))
|
|
534
|
+
b << " M" << num(right.x) << " " << num(right.y) << "L" << num(tip.x) << " " << num(tip.y) <<
|
|
535
|
+
"L" << num(left.x) << " " << num(left.y) << "Z"
|
|
536
|
+
end
|
|
537
|
+
b
|
|
538
|
+
end
|
|
539
|
+
|
|
540
|
+
# Part of a ring, clockwise from one angle to another.
|
|
541
|
+
def ring_arc_path(s, center, rotation)
|
|
542
|
+
from = s.from + rotation
|
|
543
|
+
# Clockwise from one to the other, by less than a full turn.
|
|
544
|
+
to = from + fmod(fmod(s.to - s.from, 360.0) + 360, 360.0)
|
|
545
|
+
large = to - from > 180 ? 1 : 0
|
|
546
|
+
a = center.add(Geometry::Vec.new(0.0, -s.radius).rotate(from))
|
|
547
|
+
b = center.add(Geometry::Vec.new(0.0, -s.radius).rotate(to))
|
|
548
|
+
c = center.add(Geometry::Vec.new(0.0, -s.inner).rotate(to))
|
|
549
|
+
d = center.add(Geometry::Vec.new(0.0, -s.inner).rotate(from))
|
|
550
|
+
"M#{num(a.x)} #{num(a.y)}A#{num(s.radius)} #{num(s.radius)} 0 #{large} 1 " \
|
|
551
|
+
"#{num(b.x)} #{num(b.y)}L#{num(c.x)} #{num(c.y)}A#{num(s.inner)} #{num(s.inner)}" \
|
|
552
|
+
" 0 #{large} 0 #{num(d.x)} #{num(d.y)}Z"
|
|
553
|
+
end
|
|
554
|
+
|
|
555
|
+
# Joins a star's points by skipping one each time, which is one closed line for an odd number
|
|
556
|
+
# of points and two for six, as on a hexagram.
|
|
557
|
+
def star_chords(n, radius, place)
|
|
558
|
+
seen = Array.new(n, false)
|
|
559
|
+
b = +""
|
|
560
|
+
n.times do |start|
|
|
561
|
+
next if seen[start]
|
|
562
|
+
p = place.call(Geometry::Vec.new(0.0, -radius).rotate(start * 360.0 / n))
|
|
563
|
+
b << "M" << num(p.x) << " " << num(p.y)
|
|
564
|
+
i = (start + 2) % n
|
|
565
|
+
while i != start
|
|
566
|
+
seen[i] = true
|
|
567
|
+
p = place.call(Geometry::Vec.new(0.0, -radius).rotate(i * 360.0 / n))
|
|
568
|
+
b << "L" << num(p.x) << " " << num(p.y)
|
|
569
|
+
i = (i + 2) % n
|
|
570
|
+
end
|
|
571
|
+
seen[start] = true
|
|
572
|
+
b << "Z"
|
|
573
|
+
end
|
|
574
|
+
b
|
|
575
|
+
end
|
|
576
|
+
|
|
577
|
+
# Fits a path's box within a size, keeping its shape, centered on a point.
|
|
578
|
+
def path_geom(s, center, rotation)
|
|
579
|
+
scale = Float::INFINITY
|
|
580
|
+
scale = s.fly / s.box[2] if s.fly > 0
|
|
581
|
+
if s.hoist > 0
|
|
582
|
+
h = s.hoist / s.box[3]
|
|
583
|
+
scale = h if h < scale || h.nan?
|
|
584
|
+
end
|
|
585
|
+
transform = +"translate(#{num(center.x)} #{num(center.y)})"
|
|
586
|
+
transform << " rotate(#{num(rotation)})" if rotation != 0
|
|
587
|
+
transform << " scale(#{factor(scale)}) translate(#{num(-s.box[0] - s.box[2] / 2.0)} " \
|
|
588
|
+
"#{num(-s.box[1] - s.box[3] / 2.0)})"
|
|
589
|
+
g = Geom.path(s.d)
|
|
590
|
+
g.transform = transform
|
|
591
|
+
g.scale = scale.to_f
|
|
592
|
+
g.evenodd = s.even_odd
|
|
593
|
+
g
|
|
594
|
+
end
|
|
595
|
+
|
|
596
|
+
def circle_path(c, radius)
|
|
597
|
+
rs = num(radius)
|
|
598
|
+
"M#{num(c.x - radius)} #{num(c.y)}A#{rs} #{rs} 0 1 0 #{num(c.x + radius)} #{num(c.y)}" \
|
|
599
|
+
"A#{rs} #{rs} 0 1 0 #{num(c.x - radius)} #{num(c.y)}Z"
|
|
600
|
+
end
|
|
601
|
+
|
|
602
|
+
# An ellipse with the given half sizes, turned clockwise by a rotation.
|
|
603
|
+
def ellipse_path(c, x, y, rotation)
|
|
604
|
+
a = c.sub(Geometry::Vec.new(x, 0.0).rotate(rotation))
|
|
605
|
+
b = c.add(Geometry::Vec.new(x, 0.0).rotate(rotation))
|
|
606
|
+
arc = "A#{num(x)} #{num(y)} #{num(rotation)} 1 0"
|
|
607
|
+
"M#{num(a.x)} #{num(a.y)}#{arc} #{num(b.x)} #{num(b.y)}#{arc} #{num(a.x)} #{num(a.y)}Z"
|
|
608
|
+
end
|
|
609
|
+
|
|
610
|
+
# Fills in what a shape takes from the region it's in.
|
|
611
|
+
def for_region(s, r)
|
|
612
|
+
case s.kind
|
|
613
|
+
when "lozenge"
|
|
614
|
+
return s if s.fly > 0 && s.hoist > 0
|
|
615
|
+
out = s.copy
|
|
616
|
+
out.fly = r.w if out.fly == 0
|
|
617
|
+
out.hoist = r.h if out.hoist == 0
|
|
618
|
+
out
|
|
619
|
+
when "pile"
|
|
620
|
+
out = s.copy
|
|
621
|
+
out.kind = "polygon"
|
|
622
|
+
out.vertices = pile_points(s, r)
|
|
623
|
+
out
|
|
624
|
+
when "pall"
|
|
625
|
+
out = s.copy
|
|
626
|
+
out.kind = "polygon"
|
|
627
|
+
out.vertices = pall_points(s, r)
|
|
628
|
+
out
|
|
629
|
+
else
|
|
630
|
+
s
|
|
631
|
+
end
|
|
632
|
+
end
|
|
633
|
+
|
|
634
|
+
# A lambda (in, along) -> Model::Point for an edge.
|
|
635
|
+
def edge_frame(edge, r)
|
|
636
|
+
case edge
|
|
637
|
+
when "fly" then ->(i, along) { Model::Point.new(r.w - i, along) }
|
|
638
|
+
when "top" then ->(i, along) { Model::Point.new(along, i) }
|
|
639
|
+
when "bottom" then ->(i, along) { Model::Point.new(along, r.h - i) }
|
|
640
|
+
else ->(i, along) { Model::Point.new(i, along) }
|
|
641
|
+
end
|
|
642
|
+
end
|
|
643
|
+
private_class_method :edge_frame
|
|
644
|
+
|
|
645
|
+
def edge_length(edge, r)
|
|
646
|
+
edge == "top" || edge == "bottom" ? r.w : r.h
|
|
647
|
+
end
|
|
648
|
+
private_class_method :edge_length
|
|
649
|
+
|
|
650
|
+
# Continues a pile's sides back past its edge, so that fimbriation doesn't run along the flag's edge.
|
|
651
|
+
def pile_points(s, r)
|
|
652
|
+
place = edge_frame(s.edge, r)
|
|
653
|
+
length = edge_length(s.edge, r)
|
|
654
|
+
a = 0.0
|
|
655
|
+
b = length
|
|
656
|
+
unless s.base.nil?
|
|
657
|
+
a = s.base[0].to_f
|
|
658
|
+
b = s.base[1].to_f
|
|
659
|
+
end
|
|
660
|
+
depth = s.depth.to_f
|
|
661
|
+
along = (a + b) / 2
|
|
662
|
+
unless s.tip.nil?
|
|
663
|
+
along = s.tip[0].to_f
|
|
664
|
+
depth = s.tip[1].to_f
|
|
665
|
+
end
|
|
666
|
+
lo = along - s.flat / 2.0
|
|
667
|
+
hi = along + s.flat / 2.0
|
|
668
|
+
back = r.reach
|
|
669
|
+
back_a = a - (lo - a) * back / depth
|
|
670
|
+
back_b = b + (b - hi) * back / depth
|
|
671
|
+
[place.call(-back, back_a), place.call(depth, lo), place.call(depth, hi), place.call(-back, back_b)]
|
|
672
|
+
end
|
|
673
|
+
|
|
674
|
+
# Outlines a Y whose arms run from the corners of its edge to a junction, then on as one stem.
|
|
675
|
+
def pall_points(s, r)
|
|
676
|
+
place = edge_frame(s.edge, r)
|
|
677
|
+
length = edge_length(s.edge, r)
|
|
678
|
+
far = s.edge == "top" || s.edge == "bottom" ? r.h : r.w
|
|
679
|
+
junction = s.junction == 0 ? length / 2 : s.junction.to_f
|
|
680
|
+
half = s.width / 2.0
|
|
681
|
+
mid = length / 2
|
|
682
|
+
reach = r.reach + s.width
|
|
683
|
+
l = Math.hypot(junction, mid)
|
|
684
|
+
dir_in = junction / l
|
|
685
|
+
dir_along = mid / l
|
|
686
|
+
# The upper arm runs along (dir_in, dir_along) from the corner at along=0 to the junction,
|
|
687
|
+
# in coordinates of (in, along from the center line).
|
|
688
|
+
normal_in = dir_along
|
|
689
|
+
normal_along = -dir_in
|
|
690
|
+
line = lambda do |offset, target_along|
|
|
691
|
+
# Where the arm's edge at an offset reaches a distance from the center line.
|
|
692
|
+
start_in = normal_in * offset
|
|
693
|
+
start_along = -mid + normal_along * offset
|
|
694
|
+
start_in + (target_along - start_along) * dir_in / dir_along
|
|
695
|
+
end
|
|
696
|
+
shoulder = line.call(half, -half)
|
|
697
|
+
notch = line.call(-half, 0.0)
|
|
698
|
+
far_outer_in = normal_in * half - dir_in * reach
|
|
699
|
+
far_outer_along = -mid + normal_along * half - dir_along * reach
|
|
700
|
+
far_inner_in = -normal_in * half - dir_in * reach
|
|
701
|
+
far_inner_along = -mid - normal_along * half - dir_along * reach
|
|
702
|
+
at = ->(i, from_center) { place.call(i, mid + from_center) }
|
|
703
|
+
[
|
|
704
|
+
at.call(far_outer_in, far_outer_along),
|
|
705
|
+
at.call(shoulder, -half),
|
|
706
|
+
at.call(far + reach, -half),
|
|
707
|
+
at.call(far + reach, half),
|
|
708
|
+
at.call(shoulder, half),
|
|
709
|
+
at.call(far_outer_in, -far_outer_along),
|
|
710
|
+
at.call(far_inner_in, -far_inner_along),
|
|
711
|
+
at.call(notch, 0.0),
|
|
712
|
+
at.call(far_inner_in, far_inner_along)
|
|
713
|
+
]
|
|
714
|
+
end
|
|
715
|
+
|
|
716
|
+
# Resolves an outline for painting and clipping.
|
|
717
|
+
def outline_geom(o, w, h)
|
|
718
|
+
return Geom.poly(Geometry.outline_points(o, w, h)) if o.kind != "path"
|
|
719
|
+
# A flag's own curved outline is stretched to its box.
|
|
720
|
+
sx = w.to_f / o.box[2]
|
|
721
|
+
sy = h.to_f / o.box[3]
|
|
722
|
+
g = Geom.path(o.d)
|
|
723
|
+
g.scale = 1.0
|
|
724
|
+
g.transform = "scale(#{factor(sx)} #{factor(sy)}) translate(#{num(-o.box[0])} #{num(-o.box[1])})"
|
|
725
|
+
g
|
|
726
|
+
end
|
|
727
|
+
|
|
728
|
+
# Samples a wavy or zigzag line along a span, as distances along it and displacements from it.
|
|
729
|
+
# Returns [along, offset], two Arrays of Floats.
|
|
730
|
+
def line_points(l, span)
|
|
731
|
+
span = span.to_f
|
|
732
|
+
period = l.period.to_f
|
|
733
|
+
period = span / l.count if l.count > 0
|
|
734
|
+
if span / period > Limits::MAX_COPIES
|
|
735
|
+
raise Refusal, "a shaped edge can't have more than #{Limits::MAX_COPIES} waves"
|
|
736
|
+
end
|
|
737
|
+
half = l.depth / 2.0
|
|
738
|
+
along = []
|
|
739
|
+
offset = []
|
|
740
|
+
if l.kind == "zigzag"
|
|
741
|
+
sign = -1.0
|
|
742
|
+
s = 0.0
|
|
743
|
+
while s <= span + period / 4
|
|
744
|
+
along << s
|
|
745
|
+
offset << sign * half
|
|
746
|
+
sign = -sign
|
|
747
|
+
s += period / 2
|
|
748
|
+
end
|
|
749
|
+
else
|
|
750
|
+
per_wave = 24
|
|
751
|
+
s = 0.0
|
|
752
|
+
while s <= span + period / per_wave / 2
|
|
753
|
+
along << s
|
|
754
|
+
offset << half * Math.sin(2 * Math::PI * s / period)
|
|
755
|
+
s += period / per_wave
|
|
756
|
+
end
|
|
757
|
+
end
|
|
758
|
+
[along, offset]
|
|
759
|
+
end
|
|
760
|
+
|
|
761
|
+
# A shaped line across a region, at offset t along normal, continued flat past the region.
|
|
762
|
+
def wave_edge(c, dir, normal, t, span, reach, l)
|
|
763
|
+
along, offset = line_points(l, span)
|
|
764
|
+
points = [c.add(normal.mul(t + offset[0])).sub(dir.mul(reach))]
|
|
765
|
+
along.each_index do |i|
|
|
766
|
+
points << c.add(normal.mul(t + offset[i])).add(dir.mul(along[i] - span / 2.0))
|
|
767
|
+
end
|
|
768
|
+
points << c.add(normal.mul(t + offset[-1])).add(dir.mul(reach))
|
|
769
|
+
points
|
|
770
|
+
end
|
|
771
|
+
|
|
772
|
+
# How far an axis runs across a region.
|
|
773
|
+
def axis_span(r, along)
|
|
774
|
+
case along
|
|
775
|
+
when "fly" then r.w
|
|
776
|
+
when "hoist" then r.h
|
|
777
|
+
else r.reach
|
|
778
|
+
end
|
|
779
|
+
end
|
|
780
|
+
|
|
781
|
+
# One wedge per color, each with an angle under 180 degrees or split so that it clips cleanly.
|
|
782
|
+
def ray_polys(l, r)
|
|
783
|
+
apex, from, sweep, walk = ray_frame(l, r)
|
|
784
|
+
return ray_polys_by_edge(l, r, apex, walk) if l.by_edge
|
|
785
|
+
from += l.rotate
|
|
786
|
+
total = 0.0
|
|
787
|
+
l.ratio.each { |part| total += part }
|
|
788
|
+
reach = r.reach + Math.hypot(apex.x - r.w / 2, apex.y - r.h / 2)
|
|
789
|
+
polys = []
|
|
790
|
+
a = from
|
|
791
|
+
ray = Geometry::Vec.new(0.0, -reach * 2)
|
|
792
|
+
l.ratio.each do |part|
|
|
793
|
+
width = sweep * part / total
|
|
794
|
+
steps = (width / 60).ceil
|
|
795
|
+
poly = [apex]
|
|
796
|
+
(0..steps).each do |i|
|
|
797
|
+
poly << apex.add(ray.rotate(a + width * i / steps))
|
|
798
|
+
end
|
|
799
|
+
polys << clip_convex(poly, r)
|
|
800
|
+
a += width
|
|
801
|
+
end
|
|
802
|
+
polys
|
|
803
|
+
end
|
|
804
|
+
|
|
805
|
+
# Shares the region's far edges out by length, going clockwise.
|
|
806
|
+
def ray_polys_by_edge(l, r, apex, path)
|
|
807
|
+
length = 0.0
|
|
808
|
+
(1...path.size).each do |i|
|
|
809
|
+
length += Math.hypot(path[i].x - path[i - 1].x, path[i].y - path[i - 1].y)
|
|
810
|
+
end
|
|
811
|
+
total = 0.0
|
|
812
|
+
l.ratio.each { |part| total += part }
|
|
813
|
+
# From outside the region, a wedge would cover what's between the point and the region.
|
|
814
|
+
outside = apex.x < 0 || apex.x > r.w || apex.y < 0 || apex.y > r.h
|
|
815
|
+
polys = []
|
|
816
|
+
leg = 0
|
|
817
|
+
into = 0.0
|
|
818
|
+
at = path[0]
|
|
819
|
+
l.ratio.each do |part|
|
|
820
|
+
remaining = length * part / total
|
|
821
|
+
poly = [apex, at]
|
|
822
|
+
while remaining > 1e-9 && leg < path.size - 1
|
|
823
|
+
nxt = path[leg + 1]
|
|
824
|
+
leg_length = Math.hypot(nxt.x - path[leg].x, nxt.y - path[leg].y)
|
|
825
|
+
left = leg_length - into
|
|
826
|
+
if remaining < left
|
|
827
|
+
into += remaining
|
|
828
|
+
at = path[leg].add(nxt.sub(path[leg]).mul(into / leg_length))
|
|
829
|
+
remaining = 0.0
|
|
830
|
+
break
|
|
831
|
+
end
|
|
832
|
+
remaining -= left
|
|
833
|
+
at = nxt
|
|
834
|
+
poly << at
|
|
835
|
+
leg += 1
|
|
836
|
+
into = 0.0
|
|
837
|
+
end
|
|
838
|
+
poly << at if remaining == 0
|
|
839
|
+
poly = clip_convex(poly, r) if outside
|
|
840
|
+
polys << poly
|
|
841
|
+
end
|
|
842
|
+
polys
|
|
843
|
+
end
|
|
844
|
+
|
|
845
|
+
# Where rays start: a named point, or one given by position.
|
|
846
|
+
def ray_point(l, r)
|
|
847
|
+
return Geometry::Vec.new(l.at.fly, l.at.hoist) unless l.at.nil?
|
|
848
|
+
case l.from
|
|
849
|
+
when "upper-hoist" then Geometry::Vec.new(0.0, 0.0)
|
|
850
|
+
when "lower-hoist" then Geometry::Vec.new(0.0, r.h)
|
|
851
|
+
when "upper-fly" then Geometry::Vec.new(r.w, 0.0)
|
|
852
|
+
when "lower-fly" then Geometry::Vec.new(r.w, r.h)
|
|
853
|
+
when "hoist" then Geometry::Vec.new(0.0, r.h / 2)
|
|
854
|
+
when "fly" then Geometry::Vec.new(r.w, r.h / 2)
|
|
855
|
+
when "top" then Geometry::Vec.new(r.w / 2, 0.0)
|
|
856
|
+
when "bottom" then Geometry::Vec.new(r.w / 2, r.h)
|
|
857
|
+
else r.center
|
|
858
|
+
end
|
|
859
|
+
end
|
|
860
|
+
|
|
861
|
+
# Where rays start, the angle they cover going clockwise from up, and the region's far edges,
|
|
862
|
+
# which are the ones the point is inside of, walked clockwise. Returns [apex, from, sweep, walk].
|
|
863
|
+
def ray_frame(l, r)
|
|
864
|
+
apex = ray_point(l, r)
|
|
865
|
+
corners = r.corners
|
|
866
|
+
# Edge i runs clockwise from corner i: the top, the fly, the bottom and the hoist.
|
|
867
|
+
inward = [Geometry::Vec.new(0.0, 1.0), Geometry::Vec.new(-1.0, 0.0), Geometry::Vec.new(0.0, -1.0),
|
|
868
|
+
Geometry::Vec.new(1.0, 0.0)]
|
|
869
|
+
far = Array.new(4) { |i| apex.sub(corners[i]).dot(inward[i]) > 0 }
|
|
870
|
+
first = -1
|
|
871
|
+
4.times do |i|
|
|
872
|
+
first = i if far[i] && !far[(i + 3) % 4]
|
|
873
|
+
end
|
|
874
|
+
if first < 0
|
|
875
|
+
# Every edge is far, so the point is inside, and the rays go all the way round from up.
|
|
876
|
+
top = Geometry::Vec.new(apex.x, 0.0)
|
|
877
|
+
return [apex, 0.0, 360.0, [top, corners[1], corners[2], corners[3], corners[0], top]]
|
|
878
|
+
end
|
|
879
|
+
walk = [corners[first]]
|
|
880
|
+
i = first
|
|
881
|
+
while far[i % 4]
|
|
882
|
+
walk << corners[(i + 1) % 4]
|
|
883
|
+
i += 1
|
|
884
|
+
end
|
|
885
|
+
from = bearing(walk[0].sub(apex))
|
|
886
|
+
sweep = (bearing(walk[-1].sub(apex)) - from + 360) % 360
|
|
887
|
+
[apex, from, sweep, walk]
|
|
888
|
+
end
|
|
889
|
+
|
|
890
|
+
# A direction's angle clockwise from up, exactly for the four straight ones.
|
|
891
|
+
def bearing(v)
|
|
892
|
+
return 0.0 if v.x == 0 && v.y < 0
|
|
893
|
+
return 90.0 if v.y == 0 && v.x > 0
|
|
894
|
+
return 180.0 if v.x == 0 && v.y > 0
|
|
895
|
+
return 270.0 if v.y == 0 && v.x < 0
|
|
896
|
+
# Both operands are positive here, where Ruby's % is fmod.
|
|
897
|
+
(Math.atan2(v.x, -v.y) * 180 / Math::PI + 360) % 360
|
|
898
|
+
end
|
|
899
|
+
|
|
900
|
+
# A panel's place in its parent, in the parent's coordinates. Returns [x, y, w, h].
|
|
901
|
+
def panel_rect(p, r)
|
|
902
|
+
extra = p.line.nil? ? 0.0 : p.line.depth / 2.0
|
|
903
|
+
case p.side
|
|
904
|
+
when "hoist" then return [0.0, 0.0, p.width + extra, r.h]
|
|
905
|
+
when "fly" then return [r.w - p.width - extra, 0.0, p.width + extra, r.h]
|
|
906
|
+
when "top" then return [0.0, 0.0, r.w, p.width + extra]
|
|
907
|
+
when "bottom" then return [0.0, r.h - p.width - extra, r.w, p.width + extra]
|
|
908
|
+
end
|
|
909
|
+
case p.corner
|
|
910
|
+
when "upper-hoist" then return [0.0, 0.0, p.fly.to_f, p.hoist.to_f]
|
|
911
|
+
when "upper-fly" then return [r.w - p.fly, 0.0, p.fly.to_f, p.hoist.to_f]
|
|
912
|
+
when "lower-hoist" then return [0.0, r.h - p.hoist, p.fly.to_f, p.hoist.to_f]
|
|
913
|
+
when "lower-fly" then return [r.w - p.fly, r.h - p.hoist, p.fly.to_f, p.hoist.to_f]
|
|
914
|
+
end
|
|
915
|
+
at = p.at.nil? ? r.center : Geometry::Vec.new(p.at.fly, p.at.hoist)
|
|
916
|
+
[at.x - p.fly / 2.0, at.y - p.hoist / 2.0, p.fly.to_f, p.hoist.to_f]
|
|
917
|
+
end
|
|
918
|
+
|
|
919
|
+
# A panel's outline when it has a shaped edge, in its own coordinates. A line whose last point
|
|
920
|
+
# falls short of the side's end runs on flat to it, as one between bars does.
|
|
921
|
+
def panel_edge(p, w, h)
|
|
922
|
+
w = w.to_f
|
|
923
|
+
h = h.to_f
|
|
924
|
+
span = p.side == "hoist" || p.side == "fly" ? h : w
|
|
925
|
+
along, offset = line_points(p.line, span)
|
|
926
|
+
if span - along[-1] > 1e-9 * span
|
|
927
|
+
along = along + [span]
|
|
928
|
+
offset = offset + [offset[-1]]
|
|
929
|
+
end
|
|
930
|
+
half = p.line.depth / 2.0
|
|
931
|
+
points = []
|
|
932
|
+
case p.side
|
|
933
|
+
when "hoist"
|
|
934
|
+
points << Geometry::Vec.new(0.0, 0.0)
|
|
935
|
+
along.each_index { |i| points << Geometry::Vec.new(w - half + offset[i], along[i]) }
|
|
936
|
+
points << Geometry::Vec.new(0.0, h)
|
|
937
|
+
when "fly"
|
|
938
|
+
points << Geometry::Vec.new(w, 0.0)
|
|
939
|
+
along.each_index { |i| points << Geometry::Vec.new(half - offset[i], along[i]) }
|
|
940
|
+
points << Geometry::Vec.new(w, h)
|
|
941
|
+
when "top"
|
|
942
|
+
points << Geometry::Vec.new(0.0, 0.0)
|
|
943
|
+
along.each_index { |i| points << Geometry::Vec.new(along[i], h - half + offset[i]) }
|
|
944
|
+
points << Geometry::Vec.new(w, 0.0)
|
|
945
|
+
else
|
|
946
|
+
points << Geometry::Vec.new(0.0, h)
|
|
947
|
+
along.each_index { |i| points << Geometry::Vec.new(along[i], half - offset[i]) }
|
|
948
|
+
points << Geometry::Vec.new(w, h)
|
|
949
|
+
end
|
|
950
|
+
points
|
|
951
|
+
end
|
|
952
|
+
end
|
|
953
|
+
end
|