Repository navigation
Expand file tree
/
Copy pathcurve_test.go
More file actions
98 lines (91 loc) · 2 KB
/
Copy pathcurve_test.go
File metadata and controls
98 lines (91 loc) · 2 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
// 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"
"slices"
"testing"
)
func TestSolveITP(t *testing.T) {
f := func(x float64) float64 { return x*x*x - x - 2.0 }
x := SolveITP(f, 1.0, 2.0, 1e-12, 0, 0.2, f(1.0), f(2.0))
if n := math.Abs(f(x)); n > 6e-12 {
t.Errorf("%v > 6e-12", n)
}
}
func TestSolveForArclen(t *testing.T) {
c := CubicBez{
Pt(0.0, 0.0),
Pt(100.0/3.0, 0.0),
Pt(200.0/3.0, 100.0/3.0),
Pt(100.0, 100.0),
}
const target = 100.0
SolveITP(
func(t float64) float64 { return c.Subsegment(0.0, t).PathLength(1e-9) - target },
0.0,
1.0,
1e-6,
1,
0.2,
-target,
c.PathLength(1e-9)-target,
)
}
func TestSVGSingle(t *testing.T) {
segments := []PathSegment{
CubicBez{
Pt(10.0, 10.0),
Pt(20.0, 20.0),
Pt(30.0, 30.0),
Pt(40.0, 40.0),
}.Seg(),
}
var path BezPath = slices.Collect(Elements(slices.Values(segments)))
want := "M10,10 C20,20 30,30 40,40"
got := path.SVG(SVGOptions{})
diff(t, got, want)
}
func TestSVGTwoNoMove(t *testing.T) {
segments := []PathSegment{
CubicBez{
Pt(10.0, 10.0),
Pt(20.0, 20.0),
Pt(30.0, 30.0),
Pt(40.0, 40.0),
}.Seg(),
CubicBez{
Pt(40.0, 40.0),
Pt(30.0, 30.0),
Pt(20.0, 20.0),
Pt(10.0, 10.0),
}.Seg(),
}
var path BezPath = slices.Collect(Elements(slices.Values(segments)))
want := "M10,10 C20,20 30,30 40,40 C30,30 20,20 10,10"
got := path.SVG(SVGOptions{})
diff(t, got, want)
}
func TestSVGTwoMove(t *testing.T) {
segments := []PathSegment{
CubicBez{
Pt(10.0, 10.0),
Pt(20.0, 20.0),
Pt(30.0, 30.0),
Pt(40.0, 40.0),
}.Seg(),
CubicBez{
Pt(50.0, 50.0),
Pt(30.0, 30.0),
Pt(20.0, 20.0),
Pt(10.0, 10.0),
}.Seg(),
}
var path BezPath = slices.Collect(Elements(slices.Values(segments)))
want := "M10,10 C20,20 30,30 40,40 M50,50 C30,30 20,20 10,10"
got := path.SVG(SVGOptions{})
diff(t, got, want)
}