下凹凸包

huangapple go评论133阅读模式
英文:

Lower convex hull

问题

我正在实现一个用Python计算Newton-Polygon的算法。

在Scipy中已经有一个计算给定点集的凸包的函数,但我不太明白如何取其下边界,有人有想法吗?

我已经使用了Scipy中的ConvexHull函数,并尝试只取前面几个条目,直到右侧的点,但有时结果是上凸包。

英文:

At the moment I am implementing an algorithm to compute the Newton-Polygon in Python.

In Scipy there is already a function to compute the convex hull of a given set of points, but I am not understanding how to take its lower boundary, does someone have an idea?

I already applied the ConvexHull function from Scipy and tried only taking the first few entries up until the point on the right, but it sometimes it resulted in the upper convex hull.

答案1

得分: 2

以下是您要翻译的部分:

我会通过找到极端的x值,并获取两者之间的点来解决这个问题。 SciPy保证会以逆时针顺序发射一组2D点的凸包的坐标,因此您可以从最小x值的位置读取到最大x值的位置,然后获取下凸包。

代码:

def get_lower(polygon):
    minx = np.argmin(polygon[:, 0])
    maxx = np.argmax(polygon[:, 0]) + 1
    if minx >= maxx:
        lower_curve = np.concatenate([polygon[minx:], polygon[:maxx]])
    else:
        lower_curve = polygon[minx:maxx]
    return lower_curve

如何使用此示例:

import scipy.spatial
import matplotlib.pyplot as plt
import numpy as np

rng = np.random.default_rng()
points = rng.random((10, 2))
hull = scipy.spatial.ConvexHull(points)

plt.plot(points[:,0], points[:,1], 'o')
for simplex in hull.simplices:
    plt.plot(points[simplex, 0], points[simplex, 1], 'k-')

lower_curve = get_lower(points[hull.vertices])
plt.plot(lower_curve[:, 0], lower_curve[:, 1])
英文:

I would solve this by finding the extreme x values, and getting the points between the two. SciPy is guaranteed to emit coordinates for the convex hull of a set of 2D points in counter-clockwise order, so you can read from the position of the minimum x value to the position of the maximum x value, and get the lower convex hull.

Code:

def get_lower(polygon):
    minx = np.argmin(polygon[:, 0])
    maxx = np.argmax(polygon[:, 0]) + 1
    if minx >= maxx:
        lower_curve = np.concatenate([polygon[minx:], polygon[:maxx]])
    else:
        lower_curve = polygon[minx:maxx]
    return lower_curve

Example of how to use this:

import scipy.spatial
import matplotlib.pyplot as plt
import numpy as np

rng = np.random.default_rng()
points = rng.random((10, 2))
hull = scipy.spatial.ConvexHull(points)

plt.plot(points[:,0], points[:,1], 'o')
for simplex in hull.simplices:
    plt.plot(points[simplex, 0], points[simplex, 1], 'k-')

lower_curve = get_lower(points[hull.vertices])
plt.plot(lower_curve[:, 0], lower_curve[:, 1])

huangapple
  • 本文由 发表于 2023年8月5日 02:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定