This repository was archived by the owner on Apr 2, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathrect_test.go
More file actions
151 lines (137 loc) · 4.63 KB
/
Copy pathrect_test.go
File metadata and controls
151 lines (137 loc) · 4.63 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
package curve
import (
"math"
"slices"
"testing"
)
func TestRectAreaSign(t *testing.T) {
approxEqual := func(x, y float64) bool {
return math.Abs(x-y) < 1e-8
}
r := Rect{0.0, 0.0, 10.0, 10.0}
center := r.Center()
if a := r.Area(); !approxEqual(a, 100) {
t.Errorf("got area %v, want %v", a, 100.0)
}
if w := r.Winding(center); w != 1 {
t.Errorf("got winding %v, want %v", w, 1)
}
var p BezPath = slices.Collect(r.PathElements(1e-9))
if ra, pa := r.Area(), p.SignedArea(); !approxEqual(ra, pa) {
t.Errorf("expected r's and p's areas to be approximately equal, got %v and %v", ra, pa)
}
if rw, pw := r.Winding(center), p.Winding(center); rw != pw {
t.Errorf("expected r's and p's winding numbers to be equal, got %v and %v", rw, pw)
}
rFlip := Rect{0.0, 10.0, 10.0, 0.0}
if a := rFlip.Area(); !approxEqual(a, -100) {
t.Errorf("got area %v, want %v", a, -100.0)
}
if w := rFlip.Winding(Pt(5, 5)); w != -1 {
t.Errorf("got winding %v, want %v", w, -1)
}
var pFlip BezPath = slices.Collect(rFlip.PathElements(1e09))
if ra, pa := rFlip.Area(), pFlip.SignedArea(); !approxEqual(ra, pa) {
t.Errorf("expected r's and p's areas to be approximately equal, got %v and %v", ra, pa)
}
if rw, pw := rFlip.Winding(center), pFlip.Winding(center); rw != pw {
t.Errorf("expected r's and p's winding numbers to be equal, got %v and %v", rw, pw)
}
}
func TestContainedRectWithAspectRatio(t *testing.T) {
f := func(outer Rect, aspectRatio float64, want Rect) {
t.Helper()
if got := outer.ContainedRectWithAspectRatio(aspectRatio); got != want {
t.Errorf("got %v, want %v", got, want)
}
}
// squares (different point orderings)
f(Rect{0.0, 0.0, 10.0, 20.0}, 1.0, Rect{0.0, 5.0, 10.0, 15.0})
f(Rect{0.0, 20.0, 10.0, 0.0}, 1.0, Rect{0.0, 5.0, 10.0, 15.0})
f(Rect{10.0, 0.0, 0.0, 20.0}, 1.0, Rect{10.0, 15.0, 0.0, 5.0})
f(Rect{10.0, 20.0, 0.0, 0.0}, 1.0, Rect{10.0, 15.0, 0.0, 5.0})
// non-square
f(Rect{0.0, 0.0, 10.0, 20.0}, 1.0/0.5, Rect{0.0, 7.5, 10.0, 12.5})
// same aspect ratio
f(Rect{0.0, 0.0, 10.0, 20.0}, 1.0/2.0, Rect{0.0, 0.0, 10.0, 20.0})
// negative aspect ratio
f(Rect{0.0, 0.0, 10.0, 20.0}, -1.0, Rect{0.0, 15.0, 10.0, 5.0})
// infinite aspect ratio
f(Rect{0.0, 0.0, 10.0, 20.0}, math.Inf(1), Rect{0.0, 10.0, 10.0, 10.0})
// zero aspect ratio
f(Rect{0.0, 0.0, 10.0, 20.0}, 0, Rect{5.0, 0.0, 5.0, 20.0})
// zero width rect
f(Rect{0.0, 0.0, 0.0, 20.0}, 1.0, Rect{0.0, 10.0, 0.0, 10.0})
// many zeros
f(Rect{0.0, 0.0, 0.0, 20.0}, 0.0, Rect{0.0, 0.0, 0.0, 20.0})
// everything zero
f(Rect{0.0, 0.0, 0.0, 0.0}, 0.0, Rect{0.0, 0.0, 0.0, 0.0})
}
func TestAspectRatio(t *testing.T) {
r := Rect{0, 0, 1, 1}
if ratio := r.AspectRatio(); math.Abs(ratio-1) > 1e-6 {
t.Errorf("got ratio %v, want 1.0", ratio)
}
}
func TestRoundedRectArea(t *testing.T) {
const epsilon = 1e-9
// Extremum: 0.0 radius corner -> rectangle
rect := Rect{0.0, 0.0, 100.0, 100.0}
roundedRect := RoundedRect{Rect: rect}
if ra, rra := rect.Area(), roundedRect.Area(); math.Abs(ra-rra) > epsilon {
t.Errorf("got areas %v and %v, expected them to be equal", ra, rra)
}
// Extremum: half-size radius corner -> circle
circle := Circle{Pt(0.0, 0.0), 50.0}
roundedRect = NewRoundedRect(0, 0, 100, 100, 50)
if ca, rra := circle.Area(), roundedRect.Area(); math.Abs(ca-rra) > epsilon {
t.Errorf("got areas %v and %v, expected them to be equal", ca, rra)
}
}
func TestRoundedRectWinding(t *testing.T) {
type point struct {
point Point
winding int
}
tests := []struct {
rect RoundedRect
points []point
}{
{
RoundedRect{Rect{-5.0, -5.0, 10.0, 20.0}, RoundedRectRadii{5, 5, 5, 0}},
[]point{
{Pt(0.0, 0.0), 1},
{Pt(-5.0, 0.0), 1}, // left edge
{Pt(0.0, 20.0), 1}, // bottom edge
{Pt(10.0, 20.0), 0}, // bottom-right corner
{Pt(-5.0, 20.0), 1}, // bottom-left corner (has a radius of 0)
{Pt(-10.0, 0.0), 0},
},
},
{
NewRoundedRect(-10.0, -20.0, 10.0, 20.0, 0.0), // rectangle
[]point{
{Pt(10, 20), 1}, // bottom-right corner
},
},
}
for i, tt := range tests {
for j, p := range tt.points {
if w := tt.rect.Winding(p.point); w != p.winding {
t.Errorf("test %d, point %d (zero-indexed): got winding %d, expected %d", i, j, w, p.winding)
}
}
}
}
func TestRoundedRectBeziers(t *testing.T) {
rect := NewRoundedRect(-5, -5, 10, 20, 5)
p := BezPath(slices.Collect(rect.PathElements(1e-9)))
// Note: could be more systematic about tolerance tightness.
const epsilon = 1e-7
if ra, pa := rect.Area(), p.SignedArea(); math.Abs(ra-pa) > epsilon {
t.Errorf("got areas %v and %v, expected them to be the same", ra, pa)
}
if w := p.Winding(Point{}); w != 1 {
t.Errorf("got winding %d, expected 1", w)
}
}