-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_test.go
74 lines (56 loc) · 2.02 KB
/
point_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
package polygon_test
import (
"testing"
"github.com/hamonangann/polygon"
"github.com/stretchr/testify/assert"
)
func TestPoint(t *testing.T) {
t.Run("should return correct manhattan distance", func(t *testing.T) {
a := polygon.Point{0, 0}
b := polygon.Point{3, 4}
assert.Equal(t, 7.0, polygon.ManhattanDistance(a, b))
})
t.Run("should return correct euclidean distance", func(t *testing.T) {
a := polygon.Point{0, 0}
b := polygon.Point{3, 4}
assert.Equal(t, 5.0, polygon.EuclideanDistance(a, b))
})
t.Run("should return 1 if a triplet of points is clockwise", func(t *testing.T) {
a := polygon.Point{0, 0}
b := polygon.Point{3, 3}
c := polygon.Point{3, 0}
assert.Equal(t, 1, polygon.OrientationTriplet(a, b, c))
})
t.Run("should return -1 if a triplet of points is counterclockwise", func(t *testing.T) {
a := polygon.Point{0, 0}
b := polygon.Point{3, 3}
c := polygon.Point{0, 3}
assert.Equal(t, -1, polygon.OrientationTriplet(a, b, c))
})
t.Run("should return true if a triplet of points is collinear", func(t *testing.T) {
a := polygon.Point{1, 3}
b := polygon.Point{0, 0}
c := polygon.Point{-1, -3}
assert.Equal(t, 0, polygon.OrientationTriplet(a, b, c))
})
t.Run("should return true if a point is between two points forming line segment", func(t *testing.T) {
a := polygon.Point{1, 3}
b := polygon.Point{0, 0}
c := polygon.Point{-1, -3}
assert.Equal(t, true, polygon.BetweenSegment(a, b, c))
})
t.Run("should return true if two line segments intersect each other", func(t *testing.T) {
a1 := polygon.Point{0, 0}
a2 := polygon.Point{3, 3}
b1 := polygon.Point{3, 0}
b2 := polygon.Point{0, 3}
assert.Equal(t, true, polygon.LineSegmentIntersect(a1, a2, b1, b2))
})
t.Run("should return true if there are collinearity between two line segments and the segements intersect", func(t *testing.T) {
a1 := polygon.Point{0, 0}
a2 := polygon.Point{2, 2}
b1 := polygon.Point{1, 1}
b2 := polygon.Point{3, 3}
assert.Equal(t, true, polygon.LineSegmentIntersect(a1, a2, b1, b2))
})
}