-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpath_2d.go
More file actions
321 lines (279 loc) · 8.95 KB
/
Copy pathpath_2d.go
File metadata and controls
321 lines (279 loc) · 8.95 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2016 Michael Fogleman
// Copyright 2026 LumiFloat
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package tinyskia
import (
"math"
"github.com/lumifloat/tinyskia/internal/path"
)
type Path2D struct {
builder *path.PathBuilder
}
// NewPath2D returns a new empty Path2D.
func NewPath2D() *Path2D {
return &Path2D{builder: path.NewPathBuilder()}
}
// NewPath2DWithPath adds all subpaths of path to output and return output.
// (In other words, it returns a copy of the argument.)
func NewPath2DWithPath(p0 *Path2D) *Path2D {
if p0 == nil {
return NewPath2D()
} else {
p := &Path2D{builder: path.NewPathBuilder()}
p.AddPath(p0)
return p
}
}
// AddPath adds to the path the path given by the argument.
func (p *Path2D) AddPath(p0 *Path2D) {
if p0 == nil {
return
}
pp := p0.builder.Finish()
if pp != nil {
p.builder.PushPath(pp)
}
}
// AddPathWithTransform adds to the path the path given by the argument, transformed by the given transform.
func (p *Path2D) AddPathWithTransform(p0 *Path2D, transform *Matrix) {
if p0 == nil {
return
}
if transform == nil {
transform = NewMatrixIdentity()
}
pp := p0.builder.Finish()
if pp != nil {
p.builder.PushPathWithTransform(pp, transform.transform)
}
}
// MoveTo creates a new subpath with the given point.
func (p *Path2D) MoveTo(x, y float64) {
p1 := path.Point{X: float32(x), Y: float32(y)}
p.builder.MoveTo(p1.X, p1.Y)
}
// ClosePath Marks the current subpath as closed,
// and starts a new subpath with a point the same as the start and end of the newly closed subpath.
func (p *Path2D) ClosePath() {
p.builder.Close()
}
// LineTo adds the given point to the current subpath, connected to the previous one by a straight line.
func (p *Path2D) LineTo(x, y float64) {
_, hasCurrent := p.builder.LastPoint()
if !hasCurrent {
p.MoveTo(x, y)
return
}
p1 := path.Point{X: float32(x), Y: float32(y)}
p.builder.LineTo(p1.X, p1.Y)
}
// QuadraticCurveTo adds the given point to the current subpath, connected to the previous one
// by a quadratic Bézier curve with the given control point.
func (p *Path2D) QuadraticCurveTo(x1, y1, x2, y2 float64) {
_, hasCurrent := p.builder.LastPoint()
if !hasCurrent {
p.MoveTo(x1, y1)
}
p1 := path.Point{X: float32(x1), Y: float32(y1)}
p2 := path.Point{X: float32(x2), Y: float32(y2)}
p.builder.QuadTo(p1.X, p1.Y, p2.X, p2.Y)
}
// BezierCurveTo adds the given point to the current subpath, connected to the previous one
// by a cubic Bézier curve with the given control points.
func (p *Path2D) BezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y float64) {
_, hasCurrent := p.builder.LastPoint()
if !hasCurrent {
p.MoveTo(cp1x, cp1y)
}
p1 := path.Point{X: float32(cp1x), Y: float32(cp1y)}
p2 := path.Point{X: float32(cp2x), Y: float32(cp2y)}
p3 := path.Point{X: float32(x), Y: float32(y)}
p.builder.CubicTo(p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y)
}
// ArcTo adds an arc with the given control points and radius to the current subpath,
// connected to the previous point by a straight line.
func (p *Path2D) ArcTo(x1, y1, x2, y2, radius float64) {
p.builder.ArcTo(
float32(x1), float32(y1),
float32(x2), float32(y2),
float32(radius),
)
}
// Rect adds a new closed subpath to the path, representing the given rectangle.
func (p *Path2D) Rect(x, y, w, h float64) {
if math.IsInf(x, 0) || math.IsNaN(x) ||
math.IsInf(y, 0) || math.IsNaN(y) ||
math.IsInf(w, 0) || math.IsNaN(w) ||
math.IsInf(h, 0) || math.IsNaN(h) {
return
}
p.MoveTo(x, y)
p.LineTo(x+w, y)
p.LineTo(x+w, y+h)
p.LineTo(x, y+h)
p.ClosePath()
}
// RoundRect adds a new closed subpath to the path representing the given rounded rectangle.
func (p *Path2D) RoundRect(x, y, w, h float64, radii []float64) {
if math.IsInf(x, 0) || math.IsNaN(x) ||
math.IsInf(y, 0) || math.IsNaN(y) ||
math.IsInf(w, 0) || math.IsNaN(w) ||
math.IsInf(h, 0) || math.IsNaN(h) {
return
}
if radii == nil {
p.Rect(x, y, w, h)
return
}
if (len(radii) == 0) || len(radii) > 4 {
return
}
var rx, ry [4]float64
switch len(radii) {
case 1:
rx = [4]float64{radii[0], radii[0], radii[0], radii[0]}
ry = [4]float64{radii[0], radii[0], radii[0], radii[0]}
case 2:
rx = [4]float64{radii[0], radii[1], radii[0], radii[1]}
ry = [4]float64{radii[0], radii[1], radii[0], radii[1]}
case 3:
rx = [4]float64{radii[0], radii[1], radii[2], radii[1]}
ry = [4]float64{radii[0], radii[1], radii[2], radii[1]}
case 4:
rx = [4]float64{radii[0], radii[1], radii[2], radii[3]}
ry = [4]float64{radii[0], radii[1], radii[2], radii[3]}
default:
// unreachable
}
if w < 0 {
x += w
w = -w
rx[0], rx[1] = rx[1], rx[0]
rx[2], rx[3] = rx[3], rx[2]
ry[0], ry[1] = ry[1], ry[0]
ry[2], ry[3] = ry[3], ry[2]
}
if h < 0 {
y += h
h = -h
rx[0], rx[3] = rx[3], rx[0]
rx[1], rx[2] = rx[2], rx[1]
ry[0], ry[3] = ry[3], ry[0]
ry[1], ry[2] = ry[2], ry[1]
}
top := rx[0] + rx[1]
right := ry[1] + ry[2]
bottom := rx[2] + rx[3]
left := ry[0] + ry[3]
scale := 1.0
if top > 0 {
scale = math.Min(scale, w/top)
}
if right > 0 {
scale = math.Min(scale, h/right)
}
if bottom > 0 {
scale = math.Min(scale, w/bottom)
}
if left > 0 {
scale = math.Min(scale, h/left)
}
if scale < 1 {
for i := 0; i < 4; i++ {
rx[i] *= scale
ry[i] *= scale
}
}
p.MoveTo(x+rx[0], y)
p.LineTo(x+w-rx[1], y)
if rx[1] > 0 || ry[1] > 0 {
radius := math.Max(rx[1], ry[1])
p.Arc(x+w-rx[1], y+ry[1], radius, -math.Pi/2, 0)
}
p.LineTo(x+w, y+h-ry[2])
if rx[2] > 0 || ry[2] > 0 {
radius := math.Max(rx[2], ry[2])
p.Arc(x+w-rx[2], y+h-ry[2], radius, 0, math.Pi/2)
}
p.LineTo(x+rx[3], y+h)
if rx[3] > 0 || ry[3] > 0 {
radius := math.Max(rx[3], ry[3])
p.Arc(x+rx[3], y+h-ry[3], radius, math.Pi/2, math.Pi)
}
p.LineTo(x, y+ry[0])
if rx[0] > 0 || ry[0] > 0 {
radius := math.Max(rx[0], ry[0])
p.Arc(x+rx[0], y+ry[0], radius, math.Pi, 3*math.Pi/2)
}
p.ClosePath()
}
// Arc adds points to the subpath such that the arc described by the circumference of the circle
// described by the arguments, starting at the given start angle and ending at the given end angle,
// is added to the path, connected to the previous point by a straight line.
func (p *Path2D) Arc(x, y, radius, startAngle, endAngle float64) {
p.ArcWithCounterclockwise(x, y, radius, startAngle, endAngle, false)
}
// ArcWithCounterclockwise adds points to the subpath such that the arc described by the circumference of the circle
// described by the arguments, starting at the given start angle and ending at the given end angle,
// going in the given direction (defaulting to clockwise), is added to the path, connected to
// the previous point by a straight line.
func (p *Path2D) ArcWithCounterclockwise(x, y, radius, startAngle, endAngle float64, counterclockwise bool) {
p.EllipseWithCounterclockwise(x, y, radius, radius, 0, startAngle, endAngle, counterclockwise)
}
// Ellipse adds points to the subpath such that the arc described by the circumference of the ellipse
// described by the arguments, starting at the given start angle and ending at the given end angle,
// going in the given direction (defaulting to clockwise), is added to the path, connected to
// the previous point by a straight line.
func (p *Path2D) Ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle float64) {
p.EllipseWithCounterclockwise(x, y, radiusX, radiusY, rotation, startAngle, endAngle, false)
}
// EllipseWithCounterclockwise adds points to the subpath such that the arc described by the circumference of the ellipse
// described by the arguments, starting at the given start angle and ending at the given end angle,
// going in the given direction (defaulting to clockwise), is added to the path, connected to
// the previous point by a straight line.
func (p *Path2D) EllipseWithCounterclockwise(x, y, radiusX, radiusY, rotation, startAngle, endAngle float64, counterclockwise bool) {
sweepAngle := endAngle - startAngle
for sweepAngle > 2*math.Pi {
sweepAngle -= 2 * math.Pi
}
for sweepAngle <= -2*math.Pi {
sweepAngle += 2 * math.Pi
}
if counterclockwise && sweepAngle > 0 && sweepAngle < 2*math.Pi {
sweepAngle = sweepAngle - 2*math.Pi
} else if !counterclockwise && sweepAngle < 0 && sweepAngle > -2*math.Pi {
sweepAngle = sweepAngle + 2*math.Pi
}
oval, ok := path.NewRectFromXYWH(float32(x-radiusX), float32(y-radiusY), float32(radiusX*2), float32(radiusY*2))
if !ok {
return
}
startDeg := float32(startAngle * 180.0 / math.Pi)
sweepDeg := float32(sweepAngle * 180.0 / math.Pi)
if math.Abs(float64(sweepDeg)) >= 360 {
if sweepDeg > 0 {
sweepDeg = 359.9999
} else {
sweepDeg = -359.9999
}
}
if rotation == 0 {
p.builder.ArcToOval(oval, startDeg, sweepDeg)
return
}
pb := path.NewPathBuilder()
pb.ArcToOval(oval, startDeg, sweepDeg)
if pb.IsEmpty() {
return
}
rotationDeg := float32(rotation * 180.0 / math.Pi)
ts := path.NewTransformDefault().
PostRotateAt(rotationDeg, float32(x), float32(y))
arcPath := pb.Finish()
if arcPath == nil {
return
}
p.builder.PushPathWithTransform(arcPath, ts)
}