aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/paulmach/orb/planar/distance_from_test.go
blob: 0c89f7f85f0ed8189c49e6ff37044b15b32b81af (plain) (blame)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package planar

import (
	"math"
	"testing"

	"github.com/paulmach/orb"
)

var epsilon = 1e-6

func TestDistanceFromSegment(t *testing.T) {
	a := orb.Point{0, 0}
	b := orb.Point{0, 10}

	cases := []struct {
		name   string
		point  orb.Point
		result float64
	}{
		{
			name:   "point in middle",
			point:  orb.Point{1, 5},
			result: 1,
		},
		{
			name:   "on line",
			point:  orb.Point{0, 2},
			result: 0,
		},
		{
			name:   "past start",
			point:  orb.Point{0, -5},
			result: 5,
		},
		{
			name:   "past end",
			point:  orb.Point{0, 13},
			result: 3,
		},
		{
			name:   "triangle",
			point:  orb.Point{3, 4},
			result: 3,
		},
		{
			name:   "triangle off end",
			point:  orb.Point{3, -4},
			result: 5,
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			v := DistanceFromSegment(a, b, tc.point)
			if v != tc.result {
				t.Errorf("incorrect distance: %v != %v", v, tc.result)
			}
		})
	}
}

func TestDistanceFromWithIndex(t *testing.T) {
	for _, g := range orb.AllGeometries {
		DistanceFromWithIndex(g, orb.Point{})
	}
}

func TestDistanceFrom_MultiPoint(t *testing.T) {
	mp := orb.MultiPoint{{0.0}, {1, 1}, {2, 2}}

	fromPoint := orb.Point{3, 2}
	if distance := DistanceFrom(mp, fromPoint); distance != 1 {
		t.Errorf("distance incorrect: %v != %v", distance, 1)
	}
}

func TestDistanceFrom_LineString(t *testing.T) {
	ls := orb.LineString{{0, 0}, {0, 3}, {4, 3}, {4, 0}}

	cases := []struct {
		name   string
		point  orb.Point
		result float64
	}{
		{
			point:  orb.Point{4.5, 1.5},
			result: 0.5,
		},
		{
			point:  orb.Point{0.4, 1.5},
			result: 0.4,
		},
		{
			point:  orb.Point{-0.3, 1.5},
			result: 0.3,
		},
		{
			point:  orb.Point{0.3, 2.8},
			result: 0.2,
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			d := DistanceFrom(ls, tc.point)
			if math.Abs(d-tc.result) > epsilon {
				t.Errorf("incorrect distance: %v != %v", d, tc.result)
			}
		})
	}
}

func TestDistanceFrom_Polygon(t *testing.T) {
	r1 := orb.Ring{{0, 0}, {3, 0}, {3, 3}, {0, 3}, {0, 0}}
	r2 := orb.Ring{{1, 1}, {2, 1}, {2, 2}, {1, 2}, {1, 1}}
	poly := orb.Polygon{r1, r2}

	cases := []struct {
		name   string
		point  orb.Point
		result float64
	}{
		{
			name:   "outside",
			point:  orb.Point{-1, 2},
			result: 1,
		},
		{
			name:   "inside",
			point:  orb.Point{0.4, 2},
			result: 0.4,
		},
		{
			name:   "in hole",
			point:  orb.Point{1.3, 1.4},
			result: 0.3,
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			if d := DistanceFrom(poly, tc.point); math.Abs(d-tc.result) > epsilon {
				t.Errorf("incorrect distance: %v != %v", d, tc.result)
			}
		})
	}
}