Repository navigation
Expand file tree
/
Copy pathline_test.go
More file actions
70 lines (58 loc) · 1.76 KB
/
Copy pathline_test.go
File metadata and controls
70 lines (58 loc) · 1.76 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
// 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"
"testing"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestLineArclen(t *testing.T) {
l := Line{Pt(0.0, 0.0), Pt(1.0, 1.0)}
want := math.Sqrt(2.0)
epsilon := 1e-9
if d := l.PathLength(epsilon) - want; d > epsilon {
t.Errorf("%g > %g", d, epsilon)
}
ts := l.SolveForPathLength(want/3.0, epsilon)
if d := math.Abs(ts - 1.0/3.0); d > epsilon {
t.Errorf("%g > %g", d, epsilon)
}
}
func TestLineIsInf(t *testing.T) {
if (Line{Pt(0.0, 0.0), Pt(1.0, 1.0)}).IsInf() {
t.Error("line is infinite but shouldn't be")
}
if !(Line{Pt(0.0, 0.0), Pt(math.Inf(1), 1.0)}).IsInf() {
t.Errorf("line is finite but shouldn't be")
}
if !(Line{Pt(0.0, 0.0), Pt(0.0, math.Inf(1))}).IsInf() {
t.Errorf("line is finite but shouldn't be")
}
}
func TestIntersectLine(t *testing.T) {
hLine := Line{Pt(0.0, 0.0), Pt(100.0, 0.0)}
vLine := Line{Pt(10.0, -10.0), Pt(10.0, 10.0)}
xs, n := hLine.IntersectLine(vLine)
want := []LineIntersection{{0.5, 0.1}}
diff(t, xs[:n], want, cmpopts.EquateApprox(0, 1e-7))
vLine = Line{Pt(-10.0, -10.0), Pt(-10.0, 10.0)}
if xs, n := hLine.IntersectLine(vLine); n != 0 {
t.Errorf("expected no intersections, got %v", xs[:n])
}
vLine = Line{Pt(10.0, 10.0), Pt(10.0, 20.0)}
if xs, n := hLine.IntersectLine(vLine); n != 0 {
t.Errorf("expected no intersections, got %v", xs[:n])
}
}
func BenchmarkLineNearest(b *testing.B) {
for b.Loop() {
l := Line{Pt(10, 20), Pt(30, 50)}
// Points chosen to exercise all paths in Nearest.
l.Nearest(Pt(10, 10), 0)
l.Nearest(Pt(30, 30), 0)
l.Nearest(Pt(40, 50), 0)
}
}