-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathraycast_test.go
82 lines (71 loc) · 1.94 KB
/
raycast_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
// public domain
package raycast_test
import (
"fmt"
"math/rand"
"testing"
"github.com/soniakeys/raycast"
)
var square raycast.Poly
func ExamplePoly() {
square = raycast.Poly{{1, 1}, {1, 3}, {3, 3}, {3, 1}}
}
// WP notes a ray passing through a "side" vertex is an interesting test case.
// The test case selected for ExampleXY shows the function working properly
// in this case.
func ExampleXY_In() {
triangle := raycast.Poly{{0, 0}, {0, 2}, {2, 1}}
pt := raycast.XY{1, 1}
fmt.Println(pt.In(triangle))
// Output:
// true
}
func ExampleXY_In_hourglass() {
// hg is an hourglass-shape, constructed with crossing segments.
// Both regions are on the perimiter so In returns true for points
// in both regions.
hg := raycast.Poly{{0, 0}, {2, 4}, {0, 4}, {2, 0}}
top := raycast.XY{1, 3}
bottom := raycast.XY{1, 1}
fmt.Println(top.In(hg))
fmt.Println(bottom.In(hg))
// Output:
// true
// true
}
func ExampleXY_In_star() {
// star is a five-pointed star constructed of five crossing segments.
// Regions of the points of the star are on the perimeter; In returns
// true for these regions. The center region is not on the perimeter
// but borders the perimeter regions. By the two-coloring then, In
// returns false for the center region.
star := raycast.Poly{{0, 3}, {4, 3}, {1, 0}, {2, 5}, {3, 0}}
top := raycast.XY{2, 4}
center := raycast.XY{2, 2}
fmt.Println(top.In(star))
fmt.Println(center.In(star))
// Output:
// true
// false
}
// added for coverage
func TestDegenerate(t *testing.T) {
pg := make(raycast.Poly, 2)
pt := raycast.XY{}
// In is coded to return false for all points, even if a point is on the
// polygon
if pt.In(pg) {
t.Fatal()
}
}
// show that points on vertices may return either in or out
func TestV(t *testing.T) {
pg := make(raycast.Poly, 8)
for i := range pg {
pg[i] = raycast.XY{rand.Float64(), rand.Float64()}
}
// log results on all vertices
for _, pt := range pg {
t.Log(pt.In(pg), pt)
}
}