我试图从点列表中得到凸包,这是基于格雷厄姆扫描法
points = []
for element in elements:
#getpoints function returns four corners of element bounding box with z overwritten to 0 so that we are working in 2D coordinates.
for p in getPoints(element):
points.append(p)
p0 = None
points.sort(key=lambda x: x.X)
points.sort(key=lambda x: x.Y)
#isolate lower left point
p0 = points.pop(0)
#sort by angle to x axis
points.sort(key=lambda x: DB.XYZ.BasisX.AngleTo(x-p0))
# We know the second point is correct, we are starting by checking the third point.
stack = [p0, points.pop(0), points.pop(1)]
while len(points) > 0:
vector1 = stack[-2] - stack[-1]
vector2 = points[0] - stack[-1]
angle1 = math.atan2(vector1.X, vector1.Y)
angle2 = math.atan2(vector2.X, vector2.Y)
angleDiff = angle1 - angle2
if angleDiff >= 0:
stack.pop(-1)
######stack.append(points.pop(0)) I don't think this is needed, but it doesn't solve my problem when removed.
else:
stack.append(points.pop(0))
curves = []
for i, point in enumerate(stack):
curves.append(DB.Line.CreateBound(point, stack[i-1]))
“凸包”的输出明显不正确:
编辑以澄清:
以最左端为最低点。 按与x轴的角度排列所有其他点。 将前三个点添加到堆栈中。 环 如果下一个排序点将创建顺时针旋转,请移除堆栈顶部。 否则,如果创建逆时针旋转,则将候选排序点添加到堆栈顶部
我将致力于建立一个可复制的案例
YouTube链接到带有图形的解释
期望输出:
我用python重新构建了它,并使它开始工作。我认为问题在于我如何计算
angleDiff
,在这里可以更容易地看到叉积z值的符号谢谢jason,
stack = [p0, points.pop(0), points.pop(1)]
确实会跳过一个值在最初的示例中,我还向后看了
vector1
。它应该是stack[-1] - stack[-2]
,而不是stack[-2] - stack[-1]
相关问题 更多 >
编程相关推荐