From 6bc7833cd5e47bf60805125a2cbb71ee6097ec28 Mon Sep 17 00:00:00 2001
From: panlei <2799247126@qq.com>
Date: 星期一, 16 十二月 2019 17:04:47 +0800
Subject: [PATCH] ---
---
ruleserver/geoPolygon.go | 121 ++++++++++++++++++++++++++++++++++++++++
1 files changed, 121 insertions(+), 0 deletions(-)
diff --git a/ruleserver/geoPolygon.go b/ruleserver/geoPolygon.go
new file mode 100644
index 0000000..bb74b6c
--- /dev/null
+++ b/ruleserver/geoPolygon.go
@@ -0,0 +1,121 @@
+package ruleserver
+
+import (
+ "math"
+ "ruleprocess/structure"
+)
+
+func min(num1 float64, num2 float64) float64 {
+ if num1 < num2 {
+ return num1
+ }
+ return num2
+
+}
+
+func max(num1 float64, num2 float64) float64 {
+ if num1 < num2 {
+ return num2
+ }
+ 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 {
+ var nCross int = 0
+ for i := 0; i < len(polygon); i++ {
+
+ p1 := polygon[i]
+ p2 := polygon[(i+1)%len(polygon)]
+ // 鏍规嵁鎽勫儚鏈烘墍鎷嶅浘鍍忎笌鍓嶇鐢诲竷鐨勬瘮渚嬪皢鍖哄煙鏀惧ぇ
+ p1.X = p1.X * widthScale
+ p1.Y = p1.Y * heightScale
+ p2.X = p2.X * widthScale
+ p2.Y = p2.Y * heightScale
+ if p1.Y == p2.Y {
+ continue
+ }
+ if point.Y < min(p1.Y, p2.Y) {
+ continue
+ }
+ if point.Y > max(p1.Y, p2.Y) {
+ continue
+ }
+ X := (point.Y-p1.Y)*(p2.X-p1.X)/(p2.Y-p1.Y) + p1.X
+
+ if X > point.X {
+ nCross++
+ }
+ }
+ return nCross%2 != 0
+}
+
+//GetLocation 灏嗕竴涓粰瀹氳捣濮嬪潗鏍囷紝瀹藉害闀垮害鐨勭煩褰㈠尯鍩熷潎鍒嗕负n鏂逛唤骞惰繑鍥炰腑蹇冨潗鏍囷紙n涓哄崟杈瑰钩鍒嗘暟鍊硷級鍜岄潰绉�
+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)
+ for i := 0; i < n; i++ {
+ xArr[i] = rect.X + (rect.Width/float64(2*n))*float64(2*i+1)
+ yArr[i] = rect.Y + (rect.Height/float64(2*n))*float64(2*i+1)
+ }
+ for i := 0; i < n; i++ {
+ for j := 0; j < n; j++ {
+ point := structure.Pointfloat{X: xArr[i], Y: yArr[j]}
+ pointArr = append(pointArr, point)
+ }
+ }
+ s := rect.Width * rect.Height
+ return pointArr, s
+}
+
+//ComputePolygonArea 璁$畻浠绘剰澶氳竟褰㈤潰绉�
+func ComputePolygonArea(polygon []structure.Point) float64 {
+ pointNum := len(polygon)
+ var s float64 = 0
+ if pointNum < 3 {
+ s = 0
+ }
+ for i := 0; i < pointNum; i++ {
+ s += polygon[i].X*polygon[(i+1)%pointNum].Y - polygon[i].Y*polygon[(i+1)%pointNum].X
+ }
+ area := math.Abs(s / 2)
+ return area
+}
+
+//PgsInterPercent calculate percent of two polygon intersection 璁$畻涓や釜澶氳竟褰㈢殑閲嶅彔鍗犳瘮
+func PgsInterPercent(pgpts []structure.Point, box structure.Rect, widthScale float64, heightScale float64) (percent float64) {
+
+ areapts, areaBox := GetLocation(box, 10)
+ var count = 0
+ for _, pts := range areapts {
+ if PintIsInPolygon(pts, pgpts, widthScale, heightScale) {
+ count++
+ }
+ }
+ perInterBox := float64(count) / float64(len(areapts)) // 閲嶅悎闈㈢Н鍗犵煩褰㈢殑姣斾緥
+ areaInter := perInterBox * areaBox
+ areaPg := ComputePolygonArea(pgpts)
+ perInterPg := areaInter / areaPg // 閲嶅悎闈㈢Н鍗犲杈瑰舰鍖哄煙鐨勬瘮渚�
+ // 鍝釜鍗犵殑姣斾緥澶ф寜鍝釜璁$畻
+ if perInterBox > perInterPg {
+ return (perInterBox*100)
+ }
+ return (perInterPg*100)
+}
--
Gitblit v1.8.0