如何确定点是否位于凹面外壳(Alpha形状)内?

2024-06-03 01:22:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我在2D平面上有一组坐标,我希望从中构造一个凹壳(Alpha形状)。在此之后,我需要确定某个点是位于成型船体的内部还是外部

虽然我可以使用附带的代码为凸面外壳实现这一点,但我还没有找到一种方法为凹面外壳实现同样的效果

# Detection in Convex Hulls
from scipy.spatial import Delaunay

hull = Delaunay(points)
return hull.find_simplex(test_point) >= 0

如何实现凹面外壳的相同功能


Tags: 方法代码alpha外壳附带delaunay平面形状
2条回答

使用Alpha Shape软件包。下面是一个示例代码:

import matplotlib.pyplot as plt
from descartes import PolygonPatch
import alphashape
import random
from shapely.geometry import Point

points = [(0., 0.), (0., 1.), (1., 1.), (1., 0.),
          (0.5, 0.25), (0.5, 0.75), (0.25, 0.5), (0.75, 0.5)]

alpha_shape = alphashape.alphashape(points, 2.0) # Create the alpha shape

# Plotting the alpha shape over the input data
fig, ax = plt.subplots()
ax.scatter(*zip(*points), c='green')
ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2))

N = 10 # number of random points
for i in range(N):
    x = round(random.uniform(0, 1), 2)
    y = round(random.uniform(0, 1), 2)
    point = Point(x,y) # analysis point
    if alpha_shape.contains(point) == True:
        plt.scatter(x,y,c='blue')
    else:
        plt.scatter(x,y,c='red')

enter image description here

图例:

  1. 红色表示凹多边形外部的点
  2. 在绿色中,顶点指向
  3. 蓝色表示凹多边形内的点

Shapely包含point.In(多边形)和polygon.contains(点)方法

相关问题 更多 >