panlei
2019-12-13 d9abec2f9d18673954ad68f85c9c7fecab1018ba
ruleserver/geoPolygon.go
@@ -20,10 +20,24 @@
   return num1
}
//Rect 检测目标
type Rect struct {
   X      float64
   Y      float64
   Width  float64
   Height float64
}
//Pointfloat 坐标点
type Pointfloat struct {
   X float64 `json:"x"`
   Y float64 `json:"y"`
}
//PintIsInPolygon 判断点是否在多边形内部
//point为要判断的坐标点
//polygon是多边形各点数组
func pintIsInPolygon(point structure.Pointfloat, polygon []structure.Point, widthScale float64, heightScale float64) bool {
func PintIsInPolygon(point structure.Pointfloat, polygon []structure.Point, widthScale float64, heightScale float64) bool {
   var nCross int = 0
   for i := 0; i < len(polygon); i++ {
@@ -53,11 +67,7 @@
}
//GetLocation 将一个给定起始坐标,宽度长度的矩形区域均分为n方份并返回中心坐标(n为单边平分数值)和面积
<<<<<<< HEAD
func getLocation(rect structure.Rect, n int) ([]structure.Pointfloat, float64) {
=======
func getLocation(rect structure.Rect, n int) ([]Pointfloat, float64) {
>>>>>>> master
func GetLocation(rect structure.Rect, n int) ([]structure.Pointfloat, float64) {
   xArr := make([]float64, n) // 用切片不用数组,数组不能用变量定义长度
   yArr := make([]float64, n)
   pointArr := make([]structure.Pointfloat, 0, n*n)
@@ -76,7 +86,7 @@
}
//ComputePolygonArea 计算任意多边形面积
func computePolygonArea(polygon []structure.Point) float64 {
func ComputePolygonArea(polygon []structure.Point) float64 {
   pointNum := len(polygon)
   var s float64 = 0
   if pointNum < 3 {
@@ -90,22 +100,18 @@
}
//PgsInterPercent calculate percent of two polygon intersection  计算两个多边形的重叠占比
<<<<<<< HEAD
func PgsInterPercent(pgpts []structure.Point, box structure.Rect, widthScale float64, heightScale float64) (percent float64) {
=======
func PgsInterPercent(pgpts []Point, box structure.Rect, widthScale float64, heightScale float64) (percent float64) {
>>>>>>> master
   areapts, areaBox := getLocation(box, 10)
   areapts, areaBox := GetLocation(box, 10)
   var count = 0
   for _, pts := range areapts {
      if pintIsInPolygon(pts, pgpts, widthScale, heightScale) {
      if PintIsInPolygon(pts, pgpts, widthScale, heightScale) {
         count++
      }
   }
   perInterBox := float64(count) / float64(len(areapts)) // 重合面积占矩形的比例
   areaInter := perInterBox * areaBox
   areaPg := computePolygonArea(pgpts)
   areaPg := ComputePolygonArea(pgpts)
   perInterPg := areaInter / areaPg // 重合面积占多边形区域的比例
   // 哪个占的比例大按哪个计算
   if perInterBox > perInterPg {