37 lines
712 B
Go
37 lines
712 B
Go
package utils
|
|
|
|
// http://ju.outofmemory.cn/entry/130555
|
|
func IsPointInPolygon(x float64, y float64, points [][2]float64) bool {
|
|
if len(points) == 0 {
|
|
return false
|
|
}
|
|
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
|
|
}
|