forked from go-spatial/geom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle_test.go
83 lines (74 loc) · 2.16 KB
/
circle_test.go
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
package geom_test
import (
"testing"
"github.com/go-spatial/geom"
cmppkg "github.com/go-spatial/geom/cmp"
)
func TestCircleFromPoints(t *testing.T) {
type tcase struct {
p [3][2]float64
circle geom.Circle
err error
cmp *cmppkg.Compare
}
fn := func(tc tcase) func(*testing.T) {
cmp := cmppkg.DefaultCompare()
if tc.cmp != nil {
cmp = *tc.cmp
}
return func(t *testing.T) {
circle, err := geom.CircleFromPoints(tc.p[0], tc.p[1], tc.p[2])
if (tc.err != nil && err == nil) || (tc.err == nil && err != nil) {
t.Errorf("error, expected %v got %v", tc.err, err)
return
}
if tc.err != nil {
if tc.err != err {
t.Errorf("error, expected %v got %v", tc.err, err)
}
return
}
if !cmp.Float(circle.Radius, tc.circle.Radius) {
t.Errorf("circle radius, expected %v got %v", tc.circle, circle)
return
}
if !cmp.Float(circle.Center[0], tc.circle.Center[0]) {
t.Errorf("circle x, expected %v got %v", tc.circle, circle)
return
}
if !cmp.Float(circle.Center[1], tc.circle.Center[1]) {
t.Errorf("circle y, expected %v got %v", tc.circle, circle)
return
}
}
}
tests := map[string]tcase{
"simple colinear": {
p: [3][2]float64{{1, 0}, {1, 1}, {1, 20}},
err: geom.ErrPointsAreCoLinear,
},
"center outside of triangle": {
p: [3][2]float64{{1, 0}, {10, 20}, {5, 5}},
circle: geom.Circle{Center: [2]float64{-21.642857, 22.214286}, Radius: 31.7202},
},
"center outside of triangle 1": {
p: [3][2]float64{{1, 0}, {5, 5}, {10, 20}},
circle: geom.Circle{Center: [2]float64{-21.642857, 22.214286}, Radius: 31.7202},
},
"center outside of triangle 2": {
p: [3][2]float64{{5, 5}, {1, 0}, {10, 20}},
circle: geom.Circle{Center: [2]float64{-21.642857, 22.214286}, Radius: 31.7202},
},
"center right triangle": {
p: [3][2]float64{{1, 0}, {10, 0}, {10, 7}},
circle: geom.Circle{Center: [2]float64{5.5, 3.5}, Radius: 5.7009},
},
"center right triangle 1": {
p: [3][2]float64{{10, 0}, {1, 0}, {10, 7}},
circle: geom.Circle{Center: [2]float64{5.5, 3.5}, Radius: 5.7009},
},
}
for name, tc := range tests {
t.Run(name, fn(tc))
}
}