-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharc.go
More file actions
220 lines (187 loc) · 6.14 KB
/
arc.go
File metadata and controls
220 lines (187 loc) · 6.14 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
// SPDX-FileCopyrightText: 2018 Raph Levien
// SPDX-FileCopyrightText: 2024 Dominik Honnef and contributors
//
// SPDX-License-Identifier: MIT
// SPDX-FileAttributionText: https://github.com/linebender/kurbo
package curve
import (
"math"
)
type Arc struct {
Center Point
Radii Vec2
StartAngle Angle
SweepAngle Angle
XRotation Angle
}
var _ Shape = Arc{}
var _ ParametricCurve = Arc{}
func (a Arc) Path(tolerance float64, out BezPath) BezPath {
out.MoveTo(a.Start())
sweepAngle := clampAngle(a.SweepAngle)
scaledError := max(a.Radii.X, a.Radii.Y) / tolerance
// Number of subdivisions per ellipse based on error tolerance.
// Note: this may slightly underestimate the error for quadrants.
nError := max(math.Pow(1.1163*scaledError, 1.0/6.0), 3.999_999)
n := math.Ceil(nError * math.Abs(sweepAngle) * (1.0 / (2.0 * math.Pi)))
angleStep := sweepAngle / n
armLen := math.Copysign((4.0/3.0)*math.Tan(math.Abs(0.25*angleStep)), sweepAngle)
angle0 := normalizeAngle(a.StartAngle)
p0 := sampleEllipse(a.Radii, a.XRotation, angle0)
for range int(n) {
angle1 := angle0 + angleStep
p1 := p0.Add(sampleEllipse(a.Radii, a.XRotation, angle0+math.Pi/2).Mul(armLen))
p3 := sampleEllipse(a.Radii, a.XRotation, angle1)
p2 := p3.Sub(sampleEllipse(a.Radii, a.XRotation, angle1+math.Pi/2).Mul(armLen))
angle0 = angle1
p0 = p3
out.CubicTo(
a.Center.Translate(p1),
a.Center.Translate(p2),
a.Center.Translate(p3),
)
}
return out
}
// Take the ellipse radii, how the radii are rotated, and the sweep angle, and return a
// point on the ellipse.
func sampleEllipse(radii Vec2, xRotation, angle Angle) Vec2 {
sin, cos := math.Sincos(angle)
u := radii.X * cos
v := radii.Y * sin
return rotatePt(Vec2{u, v}, xRotation)
}
// Rotate pt about the origin by angle radians.
func rotatePt(pt Vec2, angle Angle) Vec2 {
sin, cos := math.Sincos(angle)
return Vec2{
X: pt.X*cos - pt.Y*sin,
Y: pt.X*sin + pt.Y*cos,
}
}
func (a Arc) BoundingBox() Rect {
bbox := NewRectFromPoints(a.Start(), a.End())
startAngle := normalizeAngle(a.StartAngle)
sweepAngle := clampAngle(a.SweepAngle)
if math.Abs(sweepAngle) == 2*math.Pi {
return NewEllipse(a.Center, a.Radii, a.XRotation).BoundingBox()
}
containsAngle := func(angle Angle) bool {
if sweepAngle >= 0 {
end := startAngle + sweepAngle
if end <= 2*math.Pi {
return angle >= startAngle && angle <= end
}
return angle >= startAngle || angle <= normalizeAngle(end)
}
end := startAngle + sweepAngle
if end >= 0 {
return angle <= startAngle && angle >= end
}
return angle <= startAngle || angle >= normalizeAngle(end)
}
// These are the angles where the rotated ellipse reaches an extremum
// in x or y. A tight bounding box can only change at the arc endpoints or at
// one of these stationary points, so we test each extremum and keep only those
// that lie on the swept portion of the ellipse.
for _, angle := range [...]Angle{
math.Atan2(-a.Radii.Y*math.Sin(a.XRotation), a.Radii.X*math.Cos(a.XRotation)),
math.Atan2(-a.Radii.Y*math.Sin(a.XRotation), a.Radii.X*math.Cos(a.XRotation)) + math.Pi,
math.Atan2(a.Radii.Y*math.Cos(a.XRotation), a.Radii.X*math.Sin(a.XRotation)),
math.Atan2(a.Radii.Y*math.Cos(a.XRotation), a.Radii.X*math.Sin(a.XRotation)) + math.Pi,
} {
if containsAngle(angle) {
bbox = bbox.UnionPoint(a.Center.Translate(sampleEllipse(a.Radii, a.XRotation, angle)))
}
}
return bbox
}
func (a Arc) PathLength(accuracy float64) float64 {
if a.SweepAngle == 0 {
return 0
}
if a.Radii.IsInf() {
return math.Inf(1)
}
radii := Vec(math.Abs(a.Radii.X), math.Abs(a.Radii.Y))
sweepAngle := math.Abs(clampAngle(a.SweepAngle))
if radii.X == radii.Y {
return radii.X * sweepAngle
}
if sweepAngle == 2*math.Pi {
return NewEllipse(a.Center, radii, a.XRotation).PathLength(accuracy)
}
integrand := func(theta Angle) float64 {
sin, cos := math.Sincos(theta)
return math.Hypot(radii.X*sin, radii.Y*cos)
}
startAngle := normalizeAngle(a.StartAngle)
dir := math.Copysign(1, a.SweepAngle)
var integrate func(start, end Angle, accuracy float64, depth int) float64
integrateInterval := func(sweepStart, sweepEnd Angle, coeffs [][2]float64) float64 {
mid := 0.5 * (sweepStart + sweepEnd)
halfRange := 0.5 * (sweepEnd - sweepStart)
sum := 0.0
for _, coeff := range coeffs {
sum += coeff[0] * integrand(startAngle+dir*(mid+halfRange*coeff[1]))
}
return sum * halfRange
}
integrate = func(sweepStart, sweepEnd Angle, accuracy float64, depth int) float64 {
i8 := integrateInterval(sweepStart, sweepEnd, gaussLegendreCoeffs8[:])
i16 := integrateInterval(sweepStart, sweepEnd, gaussLegendreCoeffs16[:])
if math.Abs(i16-i8) <= accuracy || depth >= 20 {
return i16
}
mid := 0.5 * (sweepStart + sweepEnd)
return integrate(sweepStart, mid, accuracy*0.5, depth+1) + integrate(mid, sweepEnd, accuracy*0.5, depth+1)
}
return integrate(0, sweepAngle, accuracy, 0)
}
func (a Arc) Translate(v Vec2) Arc {
a.Center = a.Center.Translate(v)
return a
}
// Reverse returns a copy of this arc in the opposite direction.
//
// The new arc will sweep towards the original arc's start angle.
func (a Arc) Reverse() Arc {
sweepAngle := clampAngle(a.SweepAngle)
return Arc{
Center: a.Center,
Radii: a.Radii,
StartAngle: normalizeAngle(a.StartAngle + sweepAngle),
SweepAngle: -sweepAngle,
XRotation: a.XRotation,
}
}
func (a Arc) AngleAt(t float64) float64 {
return normalizeAngle(a.StartAngle + clampAngle(a.SweepAngle)*t)
}
func (a Arc) Eval(t float64) Point {
return a.Center.Translate(sampleEllipse(a.Radii, a.XRotation, a.AngleAt(t)))
}
func (a Arc) Subsegment(start, end float64) Arc {
return Arc{
Center: a.Center,
Radii: a.Radii,
StartAngle: a.AngleAt(start),
SweepAngle: clampAngle(clampAngle(a.SweepAngle) * (end - start)),
XRotation: a.XRotation,
}
}
func (a Arc) SubsegmentCurve(start, end float64) ParametricCurve {
return a.Subsegment(start, end)
}
func (a Arc) Subdivide() (Arc, Arc) {
return a.Subsegment(0.0, 0.5), a.Subsegment(0.5, 1.0)
}
func (a Arc) SubdivideCurve() (ParametricCurve, ParametricCurve) {
return a.Subdivide()
}
func (a Arc) Start() Point {
return a.Eval(0)
}
func (a Arc) End() Point {
return a.Eval(1)
}