from Project.PaixianOnline.basic.Coordinate import *
from tool.Distance import *
from tool.Convex_hull_find import boundary_find
import math
import matplotlib.pyplot as plt
#判断点是否在多边形上
def isOnEdge(point, P):
x, y = point.longitude, point.latitude
for i in range(len(P)-1):
x1, y1, x2, y2 = P[i].longitude, P[i].latitude, P[i+1].longitude, P[i+1].latitude
if x >= min(x1, x2) and x <= max(x1, x2) and y >= min(y1, y2) and y <= max(y1, y2) and (y-y1)*(x2-x1) == (y2-y1)*(x-x1):
return True
return False
#判断点是否在多边形内部,这里边界上的点算作该多边形里
def isInteriorPoint(point, P):
if isOnEdge(point, P):
return True
vertx = [p.longitude for p in P]
verty = [p.latitude for p in P]
x, y = point.longitude, point.latitude
res = False
for i in range(1, len(P)):
j = i-1
#点的纵坐标在中间(一边大于y,一边小于等于y),纵坐标平齐的按这个逻辑都会忽略
if (((verty[i] > y) != (verty[j] > y)) and (x < (vertx[j] - vertx[i]) * (y - verty[i]) / (verty[j] - verty[i]) + vertx[i])):
res = not res
return res
#点到多边形端点的距离衡量点到多边形的距离
def disPoint2District(point, P):
if len(P) == 0:
return -1
dis0 = calculate_distance(point.longitude, point.latitude, P[0].longitude, P[0].latitude)
for i in range(1, len(P)):
disi = calculate_distance(point.longitude, point.latitude, P[i].longitude, P[i].latitude)
if dis0 > disi:
dis0 = disi
return dis0
#画多边形的外包,要求点都为内点,默认顺序为逆时针(boundary_find产出为逆时针)
def polygonCover(points, e):
boundary = boundary_find([[p.longitude, p.latitude] for p in points])
#找每条边往外扩的线
lines = []
for i in range(len(boundary)-1):
x1, y1, x2, y2 = boundary[i][0], boundary[i][1], boundary[i+1][0], boundary[i+1][1]
if x1 == x2 and y1 == y2:
continue
a = y2-y1
b = x1-x2
c = (x2-x1)*y1 - (y2-y1)*x1
c1 = c+e*math.sqrt(a*a+b*b)
c2 = c-e*math.sqrt(a*a+b*b)
if a == 0:
x3, y3 = 0, -c1/b
else:
x3, y3 = -c1/a, 0
if (x3-x1)*(y2-y1)-(x2-x1)*(y3-y1) >= 0:
lines.append([a,b,c1])
else:
lines.append([a,b,c2])
#把第一条边加到末尾
lines.append(lines[0])
#算交点
boundary1 = []
for i in range(len(lines)-1):
a1, b1, c1 = lines[i]
a2, b2, c2 = lines[i+1]
#平行,直接过
if a1*b2-a2*b1 == 0:
continue
boundary1.append([(c2*b1-c1*b2)/(a1*b2-a2*b1), (a1*c2-a2*c1)/(a2*b1-a1*b2)])
boundary1.append(boundary1[0])
return boundary1
def is_overlap(p1, p2, p3, p4):
x1, y1, x2, y2, x3, y3, x4, y4 = p1[1], p1[2], p2[1], p2[2], p3[1], p3[2], p4[1], p4[2]
is_cross1 = True if ((x3 - x1) * (y2 - y1) - (x2 - x1) * (y3 - y1)) * (
(x4 - x1) * (y2 - y1) - (x2 - x1) * (y4 - y1)) < 0 else False
is_cross2 = True if ((x2 - x3) * (y4 - y3) - (x4 - x3) * (y2 - y3)) * (
(x1 - x3) * (y4 - y3) - (x4 - x3) * (y1 - y3)) < 0 else False
if is_cross1 and is_cross2:
return True
return False
def boundarys_is_overlap(boundarys):
for i in range(len(boundarys)-1):
boundaryi = boundarys[i]
for j in range(i+1, len(boundarys)):
boundaryj = boundarys[j]
#判定两多边形是否相交
for k in range(len(boundaryi)-1):
for l in range(len(boundaryj)-1):
#[""]这里只是为了套用方法,不起其他作用
if is_overlap([""]+boundaryi[k], [""]+boundaryi[k+1], [""]+boundaryj[l], [""]+boundaryj[l+1]):
return True
return False
#给定一堆多边形,每个多变形往外扩一点,直到多边形两两都不相交了
def LPCover(LP):
#先判定原始的多边形凸包是否相交
boundarys = [boundary_find([[p.longitude, p.latitude] for p in points]) for points in LP]
if boundarys_is_overlap(boundarys):
return []
#如果原始不交,则逐步尝试取外包
e = 0.01
boundarys1 = [polygonCover(points, e) for points in LP]
while boundarys_is_overlap(boundarys1):
e = e / 2
boundarys1 = [polygonCover(points, e) for points in LP]
return boundarys1
def boundaryPlot(points, boundary):
x = [p.longitude for p in points]
y = [p.latitude for p in points]
plt.scatter(x, y)
x_b = [p[0] for p in boundary]
y_b = [p[1] for p in boundary]
plt.plot(x_b, y_b, color='r')
plt.show()
def boundarysPlot(LP, boundarys):
for i in range(len(LP)):
x = [p.longitude for p in LP[i]]
y = [p.latitude for p in LP[i]]
plt.scatter(x, y)
x_b = [p[0] for p in boundarys[i]]
y_b = [p[1] for p in boundarys[i]]
plt.plot(x_b, y_b, color='r')
plt.show()
if __name__ == '__main__':
#例1-判断点是否在多边形内部
# point = Coordinate(1,0)
# P = [Coordinate(0,0), Coordinate(0,2), Coordinate(2,2), Coordinate(2,0), Coordinate(0,0)]
# print(isInteriorPoint(point, P))
#例2-判断点距离多边形的距离
# print(disPoint2District(point, P))
#例3-画多边形外包
# points = [Coordinate(0,0), Coordinate(0,2), Coordinate(2,2), Coordinate(2,0), Coordinate(0,0)]
# points = [Coordinate(0, 1), Coordinate(-1, 0), Coordinate(0, -1), Coordinate(1, 0), Coordinate(0, 1)]
# boundary = polygonCover(points, e=0.01)
# boundaryPlot(points, boundary)
#例4-画多个多边形的外包,且要保证互不相交
LP = [[Coordinate(0, 1), Coordinate(-1, 0), Coordinate(0, -1), Coordinate(1, 0), Coordinate(0, 1)],
[Coordinate(2.1, 1), Coordinate(1.1, 0), Coordinate(2.1, -1), Coordinate(3.1, 0), Coordinate(2.1, 1)],
[Coordinate(0, 3.05), Coordinate(-1, 2.05), Coordinate(0, 1.05), Coordinate(1, 2.05), Coordinate(0, 3.05)]]
boundarys = LPCover(LP)
boundarysPlot(LP, boundarys)
79、多边形点边关系、求外包
最新推荐文章于 2022-08-31 20:51:35 发布