Repository navigation
Expand file tree
/
Copy pathellipse_test.go
More file actions
78 lines (65 loc) · 2.09 KB
/
Copy pathellipse_test.go
File metadata and controls
78 lines (65 loc) · 2.09 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
// 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 (
"fmt"
"math"
"testing"
)
func TestEllipseAreaSign(t *testing.T) {
approxEqual := func(x, y float64) bool {
return math.Abs(x-y) < 1e-7
}
center := Pt(5.0, 5.0)
e := NewEllipse(center, Vec(5.0, 5.0), 1.0)
if a := e.Area(); !approxEqual(a, 25.0*math.Pi) {
t.Errorf("got area %v, expected %v", a, 25.0*math.Pi)
}
e = NewEllipse(center, Vec(5.0, 10.0), 1.0)
if a := e.Area(); !approxEqual(a, 50.0*math.Pi) {
t.Errorf("got area %v, expected %v", a, 50.0*math.Pi)
}
if w := e.Winding(center); w != 1 {
t.Errorf("got winding number %d, expected 1", w)
}
p := e.Path(1e-9, nil)
if ea, pa := e.Area(), p.Area(); !approxEqual(ea, pa) {
t.Errorf("got areas %v and %v, expected them to be the same", ea, pa)
}
if ew, pw := e.Winding(center), p.Winding(center); ew != pw {
t.Errorf("got winding numbers %d and %d, expected them to be the same", ew, pw)
}
eNegRadius := NewEllipse(center, Vec(-5.0, 10.0), 1.0)
if a := eNegRadius.Area(); !approxEqual(a, 50.0*math.Pi) {
t.Errorf("got area %v, expected %v", a, 50.0*math.Pi)
}
if w := eNegRadius.Winding(center); w != 1 {
t.Errorf("got winding number %d, expected 1", w)
}
pNegRadius := eNegRadius.Path(1e-9, nil)
if ea, pa := eNegRadius.Area(), pNegRadius.Area(); !approxEqual(ea, pa) {
t.Errorf("got areas %v and %v, expected them to be the same", ea, pa)
}
if ew, pw := eNegRadius.Winding(center), pNegRadius.Winding(center); ew != pw {
t.Errorf("got winding numbers %d and %d, expected them to be the same", ew, pw)
}
}
func BenchmarkEllipseArea(b *testing.B) {
e := NewEllipse(Pt(0, 0), Vec(5, 2), 2)
for b.Loop() {
e.Area()
}
}
func BenchmarkEllipsePerimeter(b *testing.B) {
e := NewEllipse(Pt(0, 0), Vec(5, 2), 2)
for _, accuracy := range []float64{1e-5, 1e-8, 1e-20} {
b.Run(fmt.Sprintf("accuracy=%e", accuracy), func(b *testing.B) {
for b.Loop() {
e.PathLength(accuracy)
}
})
}
}