ichigo/geom/polygon_test.go
2021-09-08 20:22:18 +10:00

63 lines
1.4 KiB
Go

package geom
import (
"image"
"testing"
)
func TestPolygonContains(t *testing.T) {
square5 := []image.Point{
{X: 0, Y: 0},
{X: 0, Y: 5},
{X: 5, Y: 5},
{X: 5, Y: 0},
}
pentagon := []image.Point{
{X: -5, Y: -4},
{X: -7, Y: 2},
{X: 0, Y: 7},
{X: 7, Y: 2},
{X: 5, Y: -4},
}
hexagon := []image.Point{
{X: 8, Y: 0},
{X: 0, Y: 8},
{X: 8, Y: 16},
{X: 23, Y: 16},
{X: 31, Y: 8},
{X: 23, Y: 0},
}
tests := []struct {
polygon []image.Point
point image.Point
want bool
}{
{square5, image.Pt(2, 3), true},
{square5, image.Pt(0, 0), true},
{square5, image.Pt(5, 5), true},
{square5, image.Pt(-5, 0), false},
{square5, image.Pt(0, -5), false},
{square5, image.Pt(6, 6), false},
{pentagon, image.Pt(0, 0), true},
{pentagon, image.Pt(8, 0), false},
{pentagon, image.Pt(1, 1), true},
{pentagon, image.Pt(-1, -1), true},
{pentagon, image.Pt(-10000, 10000), false},
{hexagon, image.Pt(0, 0), false},
{hexagon, image.Pt(16, 8), true},
{hexagon, image.Pt(0, 8), true},
{hexagon, image.Pt(-1, 8), false},
{hexagon, image.Pt(31, 8), true},
{hexagon, image.Pt(32, 8), false},
{hexagon, image.Pt(10000, 10000), false},
}
for _, test := range tests {
if got, want := PolygonContains(test.polygon, test.point), test.want; got != want {
t.Errorf("PolygonContains(%v, %v) = %v, want %v", test.polygon, test.point, got, want)
}
}
}