Repository navigation
Expand file tree
/
Copy pathannulus.go
More file actions
225 lines (191 loc) · 5.41 KB
/
Copy pathannulus.go
File metadata and controls
225 lines (191 loc) · 5.41 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
// SPDX-FileCopyrightText: 2018 Raph Levien
// SPDX-FileCopyrightText: 2026 Dominik Honnef and contributors
//
// SPDX-License-Identifier: MIT
// SPDX-FileAttributionText: https://github.com/linebender/kurbo
package curve
import (
"math"
)
type Annulus struct {
Center Point
OuterRadius float64
InnerRadius float64
}
var _ ClosedShape = Annulus{}
// Contains implements ClosedShape.
func (an Annulus) Contains(pt Point) bool {
return an.Winding(pt) != 0
}
// Path implements Shape.
func (an Annulus) Path(tolerance float64, out BezPath) BezPath {
out = Circle{
Center: an.Center,
Radius: an.OuterRadius,
}.Path(tolerance, out)
// OPT(dh): could we reverse in-place and avoid allocating for Circle.Path?
out = Circle{
Center: an.Center,
Radius: an.InnerRadius,
}.Path(tolerance, nil).ReverseSubpaths(out)
return out
}
func (an Annulus) IsInf() bool {
return an.Center.IsInf() ||
math.IsInf(an.OuterRadius, 0) ||
math.IsInf(an.InnerRadius, 0)
}
func (an Annulus) IsNaN() bool {
return an.Center.IsNaN() ||
math.IsNaN(an.OuterRadius) ||
math.IsNaN(an.InnerRadius)
}
func (an Annulus) Translate(v Vec2) Annulus {
an.Center = an.Center.Translate(v)
return an
}
func (an Annulus) Area() float64 {
return math.Pi * (an.OuterRadius*an.OuterRadius - an.InnerRadius*an.InnerRadius)
}
func (an Annulus) BoundingBox() Rect {
r := an.OuterRadius
x := an.Center.X
y := an.Center.Y
return Rect{
X0: x - r,
Y0: y - r,
X1: x + r,
Y1: y + r,
}
}
func (an Annulus) PathLength(accuracy float64) float64 {
return 2 * math.Pi * (an.OuterRadius + an.InnerRadius)
}
func (an Annulus) Winding(pt Point) int {
dist2 := pt.Sub(an.Center).Hypot2()
if dist2 < an.OuterRadius*an.OuterRadius && dist2 > an.InnerRadius*an.InnerRadius ||
dist2 < an.InnerRadius*an.InnerRadius && dist2 > an.OuterRadius*an.OuterRadius {
return 1
} else {
return 0
}
}
// Sector returns a sector of the annulus. The start angle gets
// normalized to [0, 2π] and the sweep angle gets clamped to [-2π, 2π].
func (an Annulus) Sector(startAngle, sweepAngle Angle) AnnulusSector {
return AnnulusSector{
Center: an.Center,
OuterRadius: an.OuterRadius,
InnerRadius: an.InnerRadius,
StartAngle: normalizeAngle(startAngle),
SweepAngle: clampAngle(sweepAngle),
}
}
type AnnulusSector struct {
Center Point
OuterRadius float64
InnerRadius float64
StartAngle Angle
SweepAngle Angle
}
var _ ClosedShape = AnnulusSector{}
// Contains implements ClosedShape.
func (as AnnulusSector) Contains(pt Point) bool {
return as.Winding(pt) != 0
}
// OuterArc returns the arc representing the outer radius.
func (as AnnulusSector) OuterArc() Arc {
return Arc{
Center: as.Center,
Radii: Vec2{as.OuterRadius, as.OuterRadius},
StartAngle: normalizeAngle(as.StartAngle),
SweepAngle: clampAngle(as.SweepAngle),
XRotation: 0.0,
}
}
// InnerArc returns the arc representing the inner radius.
//
// This is in the opposite direction of the outer arc, so that it is in the same
// direction as the arc that would be drawn (as the path elements for this
// annulus sector produce a closed path). See [Arc.Reverse] for reversing the
// arc.
func (as AnnulusSector) InnerArc() Arc {
sweepAngle := clampAngle(as.SweepAngle)
return Arc{
Center: as.Center,
Radii: Vec2{as.InnerRadius, as.InnerRadius},
StartAngle: normalizeAngle(as.StartAngle + sweepAngle),
SweepAngle: -sweepAngle,
XRotation: 0.0,
}
}
// Path implements Shape.
func (as AnnulusSector) Path(tolerance float64, out BezPath) BezPath {
if math.Abs(as.SweepAngle) < 2*math.Pi {
out.MoveTo(pointOnCircle(as.Center, as.InnerRadius, as.StartAngle))
// First radius
out.LineTo(pointOnCircle(as.Center, as.OuterRadius, as.StartAngle))
// Outer arc
out = appendPathDropMoveTo(out, tolerance, as.OuterArc())
// Second radius
out.LineTo(pointOnCircle(as.Center, as.InnerRadius, as.StartAngle+as.SweepAngle))
// Inner arc
out = appendPathDropMoveTo(out, tolerance, as.InnerArc())
return out
} else {
out = as.OuterArc().Path(tolerance, out)
out = as.InnerArc().Path(tolerance, out)
return out
}
}
func (as AnnulusSector) IsInf() bool {
return as.Center.IsInf() ||
math.IsInf(as.OuterRadius, 0) ||
math.IsInf(as.InnerRadius, 0) ||
math.IsInf(as.StartAngle, 0) ||
math.IsInf(as.SweepAngle, 0)
}
func (as AnnulusSector) IsNaN() bool {
return as.Center.IsNaN() ||
math.IsNaN(as.OuterRadius) ||
math.IsNaN(as.InnerRadius) ||
math.IsNaN(as.StartAngle) ||
math.IsNaN(as.SweepAngle)
}
func (as AnnulusSector) Translate(v Vec2) AnnulusSector {
as.Center = as.Center.Translate(v)
return as
}
func (as AnnulusSector) Area() float64 {
return 0.5 * math.Abs(as.OuterRadius*as.OuterRadius-as.InnerRadius*as.InnerRadius) * as.SweepAngle
}
func (as AnnulusSector) BoundingBox() Rect {
// todo this is currently not tight
r := as.OuterRadius
x := as.Center.X
y := as.Center.Y
return Rect{
X0: x - r,
Y0: y - r,
X1: x + r,
Y1: y + r,
}
}
func (as AnnulusSector) PathLength(accuracy float64) float64 {
return 2.0*
(as.OuterRadius-as.InnerRadius) +
clampAngle(as.SweepAngle)*(as.InnerRadius+as.OuterRadius)
}
func (as AnnulusSector) Winding(pt Point) int {
angle := pt.Sub(as.Center).Angle()
if angle < normalizeAngle(as.StartAngle) ||
angle > normalizeAngle(as.StartAngle+clampAngle(as.SweepAngle)) {
return 0
}
dist2 := pt.Sub(as.Center).Hypot2()
if dist2 < as.OuterRadius*as.OuterRadius && dist2 >= 0 {
return 1
} else {
return 0
}
}