-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathshape_test.go
95 lines (75 loc) · 2.16 KB
/
shape_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
84
85
86
87
88
89
90
91
92
93
94
95
package geometry_test
import (
"testing"
"github.com/EliCDavis/polyform/math/geometry"
"github.com/EliCDavis/vector/vector2"
)
func TestPointInShape(t *testing.T) {
shape := geometry.Shape([]vector2.Float64{
vector2.New(0., 0.),
vector2.New(0., 1.),
vector2.New(1., 1.),
vector2.New(1., 0.),
})
point := vector2.New(0.5, 0.5)
if shape.IsInside(point) == false {
t.Error("Point should be inside shape")
}
}
func TestGetPointtInShape(t *testing.T) {
shape := geometry.Shape([]vector2.Float64{
vector2.New(0., 0.),
vector2.New(0., 1.),
vector2.New(1., 1.),
vector2.New(1., 0.),
})
point := shape.RandomPointInShape()
if shape.IsInside(point) == false {
t.Error("Point should be inside shape")
}
}
func TestSplit(t *testing.T) {
shape := geometry.Shape([]vector2.Float64{
vector2.New(0., 0.),
vector2.New(0., 1.),
vector2.New(1., 1.),
vector2.New(1., 0.),
})
left, right := shape.Split(.5)
if len(left) != 1 {
t.Errorf("Split should have returned 1 left shape! Instead returned %d", len(left))
}
if len(right) != 1 {
t.Errorf("Split should have returned 1 left shape! Instead returned %d", len(right))
}
left, right = shape.Split(1.1)
if len(left) != 1 {
t.Errorf("Split should have returned the same shape! Instead returned %d new shapes", len(left))
}
if len(right) != 0 {
t.Errorf("Split should have returned nothing! Instead returned %d new shapes", len(right))
}
left, right = shape.Split(-1)
if len(right) != 1 {
t.Errorf("Split should have returned the same shape! Instead returned %d new shapes", len(left))
}
if len(left) != 0 {
t.Errorf("Split should have returned nothing! Instead returned %d new shapes", len(right))
}
// l 2 points, r 1 point
shape = geometry.Shape([]vector2.Float64{
vector2.New(0., 0.), // l
vector2.New(.7, .5), // r
vector2.New(0., 1.), // l
vector2.New(.3, 1.), // l
vector2.New(.9, .5), // r
vector2.New(.3, 0.), // l
})
left, right = shape.Split(.5)
if len(left) != 2 {
t.Errorf("Split should have returned 2 left shapes! Instead returned %d", len(left))
}
if len(right) != 1 {
t.Errorf("Split should have returned 1 right shape! Instead returned %d", len(right))
}
}