Repository navigation
Expand file tree
/
Copy pathcircle.go
More file actions
282 lines (240 loc) · 6.38 KB
/
Copy pathcircle.go
File metadata and controls
282 lines (240 loc) · 6.38 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
// 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 Circle struct {
Center Point
Radius float64
}
var _ ClosedShape = Circle{}
// Contains implements ClosedShape.
func (c Circle) Contains(pt Point) bool {
return c.Winding(pt) != 0
}
func (c Circle) Path(tolerance float64, out BezPath) BezPath {
scaledError := math.Abs(c.Radius) / tolerance
var n int
var armLength float64
if scaledError < 1.0/1.9608e-4 {
// Solution from http://spencermortensen.com/articles/bezier-circle/
n = 4
armLength = 0.551915024494
} else {
// This is empirically determined to fall within error tolerance.
n = int(math.Ceil(math.Pow(1.1163*scaledError, 1.0/6.0)))
// Note: this isn't minimum error, but it is simple and we can easily
// estimate the error.
armLength = (4.0 / 3.0) * math.Tan(math.Pi/2/(float64(n)))
}
x, y := c.Center.Splat()
r := c.Radius
out.MoveTo(Pt(x+r, y))
deltaTh := 2.0 * math.Pi / float64(n)
for ix := 1; ix <= n; ix++ {
a := armLength
th1 := deltaTh * float64(ix)
th0 := th1 - deltaTh
s0, c0 := math.Sincos(th0)
var s1, c1 float64
if ix == n {
s1 = 0.0
c1 = 1.0
} else {
s1, c1 = math.Sincos(th1)
}
out.CubicTo(
Pt(x+r*(c0-a*s0), y+r*(s0+a*c0)),
Pt(x+r*(c1+a*s1), y+r*(s1-a*c1)),
Pt(x+r*c1, y+r*s1),
)
}
out.ClosePath()
return out
}
// Sector returns a sector of the circle. The start angle gets
// normalized to [0, 2π] and the sweep angle gets clamped to [-2π, 2π].
func (c Circle) Sector(startAngle, sweepAngle Angle) CircleSector {
return CircleSector{
Center: c.Center,
Radius: c.Radius,
StartAngle: normalizeAngle(startAngle),
SweepAngle: clampAngle(sweepAngle),
}
}
func (c Circle) IsInf() bool {
return c.Center.IsInf() || math.IsInf(c.Radius, 0)
}
func (c Circle) IsNaN() bool {
return c.Center.IsNaN() || math.IsNaN(c.Radius)
}
func (c Circle) Translate(v Vec2) Circle {
return Circle{
Center: c.Center.Translate(v),
Radius: c.Radius,
}
}
func (c Circle) Area() float64 {
return math.Pi * c.Radius * c.Radius
}
func (c Circle) BoundingBox() Rect {
r := math.Abs(c.Radius)
x := c.Center.X
y := c.Center.Y
return Rect{
X0: x - r,
Y0: y - r,
X1: x + r,
Y1: y + r,
}
}
func (c Circle) PathLength(accuracy float64) float64 {
return math.Abs(2 * math.Pi * c.Radius)
}
func (c Circle) Winding(pt Point) int {
if pt.Sub(c.Center).Hypot2() < c.Radius*c.Radius {
return 1
} else {
return 0
}
}
func (c Circle) Transform(aff Affine) Ellipse {
return NewEllipseFromCircle(c).Transform(aff)
}
type CircleSector struct {
Center Point
Radius float64
StartAngle Angle
SweepAngle Angle
}
var _ ClosedShape = CircleSector{}
var _ ParametricCurve = CircleSector{}
// Contains implements ClosedShape.
func (cs CircleSector) Contains(pt Point) bool {
return cs.Winding(pt) != 0
}
func (cs CircleSector) Arc() Arc {
return Arc{
Center: cs.Center,
Radii: Vec2{cs.Radius, cs.Radius},
StartAngle: normalizeAngle(cs.StartAngle),
SweepAngle: clampAngle(cs.SweepAngle),
XRotation: 0.0,
}
}
// Path implements Shape.
func (cs CircleSector) Path(tolerance float64, out BezPath) BezPath {
if math.Abs(cs.SweepAngle) < 2*math.Pi {
out.MoveTo(cs.Center)
out = appendPathReplaceMoveTo(
out,
LineTo(pointOnCircle(cs.Center, cs.Radius, cs.StartAngle)),
tolerance,
cs.Arc(),
)
out.LineTo(cs.Center)
return out
} else {
return cs.Arc().Path(tolerance, out)
}
}
func pointOnCircle(center Point, radius float64, angle Angle) Point {
sin, cos := math.Sincos(angle)
return center.Translate(
Vec2{
X: cos * radius,
Y: sin * radius,
})
}
func (cs CircleSector) IsInf() bool {
return cs.Center.IsInf() ||
math.IsInf(cs.Radius, 0) ||
math.IsInf(cs.StartAngle, 0) ||
math.IsInf(cs.SweepAngle, 0)
}
func (cs CircleSector) IsNaN() bool {
return cs.Center.IsNaN() ||
math.IsNaN(cs.Radius) ||
math.IsNaN(cs.StartAngle) ||
math.IsNaN(cs.SweepAngle)
}
func (cs CircleSector) Translate(v Vec2) CircleSector {
cs.Center = cs.Center.Translate(v)
return cs
}
func (cs CircleSector) Area() float64 {
return 0.5 * cs.Radius * cs.Radius * clampAngle(cs.SweepAngle)
}
func (cs CircleSector) BoundingBox() Rect {
sweepAngle := clampAngle(cs.SweepAngle)
containsAngle := func(a Angle) bool {
if sweepAngle >= 0 {
return normalizeAngle(a-cs.StartAngle) <= sweepAngle
}
return normalizeAngle(cs.StartAngle-a) <= -sweepAngle
}
bbox := Rect{1, 1, 0, 0}.
UnionPoint(cs.Center).
UnionPoint(cs.Eval(0)).
UnionPoint(cs.Eval(1))
for _, a := range []Angle{0, math.Pi / 2, math.Pi, 3 * math.Pi / 2} {
if containsAngle(a) {
bbox = bbox.UnionPoint(pointOnCircle(cs.Center, cs.Radius, a))
}
}
return bbox
}
func (cs CircleSector) PathLength(accuracy float64) float64 {
return 2.0*cs.Radius + clampAngle(cs.SweepAngle)*cs.Radius
}
func (cs CircleSector) Winding(pt Point) int {
angle := pt.Sub(cs.Center).Angle()
if angle < normalizeAngle(cs.StartAngle) || angle > normalizeAngle(cs.StartAngle+clampAngle(cs.SweepAngle)) {
return 0
}
dist2 := pt.Sub(cs.Center).Hypot2()
if dist2 < cs.Radius*cs.Radius && dist2 >= 0 {
return 1
} else {
return 0
}
}
// Start implements [ParametricCurve].
func (cs CircleSector) Start() Point {
return cs.Eval(0)
}
// End implements [ParametricCurve].
func (cs CircleSector) End() Point {
return cs.Eval(1)
}
func (cs CircleSector) AngleAt(t float64) Angle {
return normalizeAngle(cs.StartAngle + clampAngle(cs.SweepAngle)*t)
}
// Eval implements [ParametricCurve].
func (cs CircleSector) Eval(t float64) Point {
a := cs.AngleAt(t)
return pointOnCircle(cs.Center, cs.Radius, a)
}
func (cs CircleSector) Subdivide() (CircleSector, CircleSector) {
return cs.Subsegment(0.0, 0.5), cs.Subsegment(0.5, 1.0)
}
// SubdivideCurve implements [ParametricCurve].
func (cs CircleSector) SubdivideCurve() (ParametricCurve, ParametricCurve) {
return cs.Subdivide()
}
func (cs CircleSector) Subsegment(start float64, end float64) CircleSector {
return CircleSector{
Center: cs.Center,
Radius: cs.Radius,
StartAngle: cs.AngleAt(start),
SweepAngle: clampAngle(cs.SweepAngle) * (end - start),
}
}
// SubsegmentCurve implements [ParametricCurve].
func (cs CircleSector) SubsegmentCurve(start float64, end float64) ParametricCurve {
return cs.Subsegment(start, end)
}