检查点是否位于凸包内
Check if points lies inside a convex hull
我正在使用 scipy.spatial
包制作一个凸包 http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.ConvexHull.html#scipy.spatial.ConvexHull
我试过搜索,但还不知道是否有一种简单的方法可以找到凸包内是否有任何点 (latitude/longitude)。有什么建议吗?
我之前用的方法是用Path
classmatplotlib
。这有一个 contains_point
方法可以做到这一点。还有一个 contains_points
允许您查询点数组。
要使用这个你会做
from scipy.spatial import ConvexHull
from matplotlib.path import Path
hull = ConvexHull( points )
hull_path = Path( points[hull.vertices] )
print hull_path.contains_point((1,2)) # Is (1,2) in the convex hull?
matplotlib Path 方法有效,但仅适用于二维数据。适用于任意维度的通用方法是对船体的小平面使用半空间方程。
import numpy as np
from scipy.spatial import ConvexHull
# Assume points are shape (n, d), and that hull has f facets.
hull = ConvexHull(points)
# A is shape (f, d) and b is shape (f, 1).
A, b = hull.equations[:, :-1], hull.equations[:, -1:]
eps = np.finfo(np.float32).eps
def contained(x):
# The hull is defined as all points x for which Ax + b <= 0.
# We compare to a small positive value to account for floating
# point issues.
#
# Assuming x is shape (m, d), output is boolean shape (m,).
return np.all(np.asarray(x) @ A.T + b.T < eps, axis=1)
# To test one point:
print('Point (2,1) is in the hull?', contained([[2, 1]]))
我正在使用 scipy.spatial
包制作一个凸包 http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.ConvexHull.html#scipy.spatial.ConvexHull
我试过搜索,但还不知道是否有一种简单的方法可以找到凸包内是否有任何点 (latitude/longitude)。有什么建议吗?
我之前用的方法是用Path
classmatplotlib
。这有一个 contains_point
方法可以做到这一点。还有一个 contains_points
允许您查询点数组。
要使用这个你会做
from scipy.spatial import ConvexHull
from matplotlib.path import Path
hull = ConvexHull( points )
hull_path = Path( points[hull.vertices] )
print hull_path.contains_point((1,2)) # Is (1,2) in the convex hull?
matplotlib Path 方法有效,但仅适用于二维数据。适用于任意维度的通用方法是对船体的小平面使用半空间方程。
import numpy as np
from scipy.spatial import ConvexHull
# Assume points are shape (n, d), and that hull has f facets.
hull = ConvexHull(points)
# A is shape (f, d) and b is shape (f, 1).
A, b = hull.equations[:, :-1], hull.equations[:, -1:]
eps = np.finfo(np.float32).eps
def contained(x):
# The hull is defined as all points x for which Ax + b <= 0.
# We compare to a small positive value to account for floating
# point issues.
#
# Assuming x is shape (m, d), output is boolean shape (m,).
return np.all(np.asarray(x) @ A.T + b.T < eps, axis=1)
# To test one point:
print('Point (2,1) is in the hull?', contained([[2, 1]]))