- IsPointInPolygon added
This commit is contained in:
33
utils/utils_geo.go
Normal file
33
utils/utils_geo.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package utils
|
||||
|
||||
// http://ju.outofmemory.cn/entry/130555
|
||||
func IsPointInPolygon(x float64, y float64, points [][2]float64) bool {
|
||||
count := 0
|
||||
x1, y1 := points[0][0], points[0][1]
|
||||
x1Part := (y1 > y) || ((x1-x > 0) && (y1 == y))
|
||||
var a = [2]float64{x1, y1}
|
||||
p := append(points, a)
|
||||
for i := range p {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
point := p[i]
|
||||
x2, y2 := point[0], point[1]
|
||||
x2Part := (y2 > y) || ((x2 > x) && (y2 == y))
|
||||
if x2Part == x1Part {
|
||||
x1, y1 = x2, y2
|
||||
continue
|
||||
}
|
||||
mul := (x1-x)*(y2-y) - (x2-x)*(y1-y)
|
||||
if mul > 0 {
|
||||
count++
|
||||
} else {
|
||||
if mul < 0 {
|
||||
count--
|
||||
}
|
||||
}
|
||||
x1, y1 = x2, y2
|
||||
x1Part = x2Part
|
||||
}
|
||||
return count == 2 || count == -2
|
||||
}
|
||||
Reference in New Issue
Block a user