在您的示例中,它是一个矩形,可以按如下方式计算:
- 找到区域的最小点.
MinimumPoint.X = Min(p1.X, p2.X)
和MinimumPoint.Y = Min(p1.Y, p2.Y)
- 找到面积的最大点.
MaximumPoint.X = Max(p1.X, p2.X)
和MaximumPoint.Y = Max(p1.Y, p2.Y)
- 判断这一点在它们之间:
CheckPoint.X > MinimumPoint.X
和CheckPoint.X < MaximumPoint.X
以及CheckPoint.Y > MinimumPoint.Y
和CheckPoint.Y < MaximumPoint.Y
或者您可以使用下面的contains函数:https://pkg.go.dev/github.com/paulmach/orb
添加到项目:go get github.com/paulmach/orb
以下是我为您的问题编写的示例代码:
package main
import (
"fmt"
"github.com/paulmach/orb"
)
func main() {
p1 := orb.Point{43.672162, -79.43585}
p2 := orb.Point{43.629845, -79.314585}
bound := orb.MultiPoint{p1, p2}.Bound()
fmt.Printf("bound: %+v\n", bound)
checkList := []orb.Point{orb.Point{43.651989, -79.371993}, p1, p2, orb.Point{43, -79}}
fmt.Printf("\ncontains?\n")
for _, checkPoint := range checkList {
fmt.Printf(" %+v:%t\n", checkPoint, bound.Contains(checkPoint))
}
}
结果:
bound: {Min:[43.629845 -79.43585] Max:[43.672162 -79.314585]}
contains?
[43.651989 -79.371993]:true
[43.672162 -79.43585]:true
[43.629845 -79.314585]:true
[43 -79]:false