-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutil.go
42 lines (33 loc) · 987 Bytes
/
util.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
package geometry
import (
"math"
"github.com/EliCDavis/vector/vector2"
)
type Orientation int
const (
Colinear Orientation = iota
Clockwise
Counterclockwise
)
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are colinear
// 1 --> Clockwise
// 2 --> Counterclockwise
func calculateOrientation(p, q, r vector2.Float64) Orientation {
// See https://www.geeksforgeeks.org/orientation-3-ordered-points/
// for details of below formula.
val := ((q.Y() - p.Y()) * (r.X() - q.X())) - ((q.X() - p.X()) * (r.Y() - q.Y()))
if val == 0 {
return Colinear
}
if val > 0 {
return Clockwise
}
return Counterclockwise
}
// Given three colinear points p, q, r, the function checks if
// point q lies on line segment 'pr'
func onSegment(p, q, r vector2.Float64) bool {
return q.X() <= math.Max(p.X(), r.X()) && q.X() >= math.Min(p.X(), r.X()) && q.Y() <= math.Max(p.Y(), r.Y()) && q.Y() >= math.Min(p.Y(), r.Y())
}