This repository has been archived by the owner on Oct 29, 2018. It is now read-only.
forked from skelterjohn/geom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly_test.go
128 lines (117 loc) · 2.39 KB
/
poly_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package geom
import (
"testing"
)
func TestInsert(t *testing.T) {
p := &Polygon{}
p.AddVertex(Coord{0, 0})
p.AddVertex(Coord{0, 1})
p.AddVertex(Coord{1, 1})
p.AddVertex(Coord{1, 0})
p.InsertVertexAfter(Coord{5, 0}, 2)
p2 := &Polygon{}
p2.AddVertex(Coord{0, 0})
p2.AddVertex(Coord{0, 1})
p2.AddVertex(Coord{5, 0})
p2.AddVertex(Coord{1, 1})
p2.AddVertex(Coord{1, 0})
if !p.Equals(p2) {
t.Fail()
}
}
func TestPolyTriangularize(t *testing.T) {
poly := new(Polygon)
poly.AddVertex(Coord{0, 0})
poly.AddVertex(Coord{0, 1})
poly.AddVertex(Coord{1, 1})
poly.AddVertex(Coord{1, 0})
tris, ok := poly.Triangles()
if ok {
for _, tri := range tris {
t.Logf("triangle: %v\n", tri)
}
} else {
t.Logf("No triangles for %v\n", poly)
}
poly = new(Polygon)
poly.AddVertex(Coord{0, 0})
poly.AddVertex(Coord{1, 1})
poly.AddVertex(Coord{2, 0})
poly.AddVertex(Coord{2, 3})
poly.AddVertex(Coord{1, 2})
poly.AddVertex(Coord{0, 3})
tris, ok = poly.Triangles()
if ok {
for _, tri := range tris {
t.Logf("triangle: %v\n", tri)
}
} else {
t.Logf("No triangles for %v\n", poly)
}
poly = new(Polygon)
poly.AddVertex(Coord{2, 1})
poly.AddVertex(Coord{2, 2})
poly.AddVertex(Coord{1, 2})
poly.AddVertex(Coord{1, 3})
tris, ok = poly.Triangles()
if ok {
for _, tri := range tris {
t.Logf("triangle: %v\n", tri)
}
} else {
t.Logf("No triangles for %v\n", poly)
}
}
//{44 736} {44 848} {88 848} {88 1044} {44 1044} {44 1244} {68 1244} {68 1068} {112 1068} {112 824} {68 824} {68 736}
func TestPiece(t *testing.T) {
vertices := []Coord{
Coord{1, 1},
Coord{1, 6},
Coord{2, 6},
Coord{2, 3},
Coord{4, 3},
Coord{4, 6},
Coord{5, 6},
Coord{5, 2},
Coord{2, 2},
Coord{2, 1},
}
poly := new(Polygon)
for _, v := range vertices {
poly.AddVertex(v)
}
tris, ok := poly.Triangles()
if ok {
for _, tri := range tris {
t.Logf("triangle: %v\n", tri)
}
} else {
t.Logf("No triangles for %v\n", poly)
}
vertices = []Coord{
Coord{44, 736},
Coord{44, 848},
Coord{88, 848},
Coord{88, 1044},
Coord{44, 1044},
Coord{44, 1244},
Coord{68, 1244},
Coord{68, 1068},
Coord{112, 1068},
Coord{112, 824},
Coord{68, 824},
Coord{68, 736},
}
poly = new(Polygon)
for _, v := range vertices {
poly.AddVertex(v)
}
tris, ok = poly.Triangles()
if ok {
for _, tri := range tris {
t.Logf("triangle: %v\n", tri)
}
} else {
t.Logf("No triangles for %v\n", poly)
}
}