gpt4 book ai didi

python - 如何扩展多边形直到其中一个边界到达一个点

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:33:28 24 4
gpt4 key购买 nike

我有扩展多边形的代码,它的工作原理是将 xs 和 ys 乘以一个因子,然后将所得多边形重新居中于原始多边形的中心。

给定多边形需要达到的点,我也有代码来查找扩展因子的值:

import numpy as np
import itertools as IT
import copy
from shapely.geometry import LineString, Point

def getPolyCenter(points):
"""
http://stackoverflow.com/a/14115494/190597 (mgamba)
"""
area = area_of_polygon(*zip(*points))
result_x = 0
result_y = 0
N = len(points)
points = IT.cycle(points)
x1, y1 = next(points)
for i in range(N):
x0, y0 = x1, y1
x1, y1 = next(points)
cross = (x0 * y1) - (x1 * y0)
result_x += (x0 + x1) * cross
result_y += (y0 + y1) * cross
result_x /= (area * 6.0)
result_y /= (area * 6.0)
return (result_x, result_y)

def expandPoly(points, factor):
points = np.array(points, dtype=np.float64)
expandedPoly = points*factor
expandedPoly -= getPolyCenter(expandedPoly)
expandedPoly += getPolyCenter(points)
return np.array(expandedPoly, dtype=np.int64)

def distanceLine2Point(points, point):
points = np.array(points, dtype=np.float64)
point = np.array(point, dtype=np.float64)

points = LineString(points)
point = Point(point)
return points.distance(point)

def distancePolygon2Point(points, point):
distances = []
for i in range(len(points)):
if i==len(points)-1:
j = 0
else:
j = i+1
line = [points[i], points[j]]
distances.append(distanceLine2Point(line, point))

minDistance = np.min(distances)
#index = np.where(distances==minDistance)[0][0]

return minDistance

"""
Returns the distance from a point to the nearest line of the polygon,
AND the distance from where the normal to the line (to reach the point)
intersets the line to the center of the polygon.
"""
def distancePolygon2PointAndCenter(points, point):
distances = []
for i in range(len(points)):
if i==len(points)-1:
j = 0
else:
j = i+1
line = [points[i], points[j]]
distances.append(distanceLine2Point(line, point))

minDistance = np.min(distances)
i = np.where(distances==minDistance)[0][0]
if i==len(points)-1:
j = 0
else:
j = i+1
line = copy.deepcopy([points[i], points[j]])

centerDistance = distanceLine2Point(line, getPolyCenter(points))

return minDistance, centerDistance

minDistance, centerDistance = distancePolygon2PointAndCenter(points, point)
expandedPoly = expandPoly(points, 1+minDistance/centerDistance)

此代码仅在该点与其中一条多边形线直接相对时才有效。

最佳答案

将您的方法 distancePolygon2PointAndCenter 修改为而不是

Returns the distance from a point to the nearest line of the polygon

返回从一个点到从中心到该点的射线相交的线段的距离。这是多边形完全展开后与点相交的线。要获得此线段,请取多边形每个线段的两个端点,并将它们代入与前面提到的射线平行和相交的直线方程中。即 y = ((centerY-pointY)/(centerX-pointX)) * (x - centerX) + centerY。您想要找到端点,其中一个端点与线相交,或者两个端点位于线的相对两侧。

然后,剩下要做的唯一一件事就是确保我们选择与直线右侧“侧”相交的线段。为此,有几个选项。故障安全方法是使用公式 cos(theta) = sqrt((centerX**2 + centerY**2)*(pointX**2 + pointY**2))/(centerX * pointX + centerY * pointY) 但是,您可以使用诸如比较 x 和 y 值、采用 arctan2() 等方法来确定哪个段位于正确的“一侧”的中心。您将只需要涵盖很多边缘情况。完成所有这些之后,您的两个端点(除非它不是凸的,在这种情况下取​​离您中心最远的线段)端点构成要扩展的线段。

关于python - 如何扩展多边形直到其中一个边界到达一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51111104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com