簡體   English   中英

Python中2D凸包的周長

[英]Perimeter of a 2D convex hull in Python

如何在Python中計算凸包的周長? 我知道SciPy具有凸包的area參數; 但是,我需要perimeter

您可以遍歷凸包的點並計算連續點之間的距離:

import numpy as np
from scipy.spatial.qhull import ConvexHull
from scipy.spatial.distance import euclidean

points = np.random.rand(30, 2)
hull = ConvexHull(points)

vertices = hull.vertices.tolist() + [hull.vertices[0]]
perimeter = np.sum([euclidean(x, y) for x, y in zip(points[vertices], points[vertices][1:])])
print(perimeter)

產量

3.11

注意:您還需要添加對(最后一個,第一個)

UPDATE

或者,假設數據為2D,則可以使用hull.area 也就是說,上述方法返回的值等於area屬性的值。 如果需要真實區域 ,則需要查詢hull.volume

進一步

  1. scipy凸包中的面積是多少

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM