0

I saw this post about how to plot a convex hull: Calculating and displaying a ConvexHull

And tried to mimic it for my needs.

The problem is that it seems that this only works for integer typed tuples, and not floating point tuples. So I have this function that gets 2D points (float tuples), and a plot, and I wish that it will return the updated plot (with the convex hull painted) and the hull points:

def get_cp(cluster, plt):
hull = ConvexHull(cluster)
# plt.plot(cluster[:, 0], cluster[:, 1], '.', color='k')
for simplex in hull.simplices:
    plt.plot(cluster[simplex, 0], cluster[simplex, 1], 'c')
plt.plot(cluster[hull.vertices, 0], cluster[hull.vertices, 1], 'o', mec='r', color='none', lw=1, markersize=10)
return hull.simplices, plt

Can anyone please assist?

John
  • 17
  • 3

1 Answers1

0

I'm not sure if I understand your question completely. The following is the code from the answer you linked with only one modified line: instead of generating random integer points, I'm generating random float points: points = 10 * np.random.rand(15, 2). The result looks good.

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

points = 10 * np.random.rand(15, 2)  # Random points in 2-D
# convert them to a list of tuples, to accomodate user request
points = [tuple(t) for t in points]
# Need to convert your list of tuples to a Numpy array.
# That's mandatory!!!
points = np.array(points)

hull = ConvexHull(points)

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 3))

for ax in (ax1, ax2):
    ax.plot(points[:, 0], points[:, 1], '.', color='k')
    if ax == ax1:
        ax.set_title('Given points')
    else:
        ax.set_title('Convex hull')
        for simplex in hull.simplices:
            ax.plot(points[simplex, 0], points[simplex, 1], 'c')
        ax.plot(points[hull.vertices, 0], points[hull.vertices, 1], 'o', mec='r', color='none', lw=1, markersize=10)
    ax.set_xticks(range(10))
    ax.set_yticks(range(10))
plt.show()

enter image description here

Davide_sd
  • 10,578
  • 3
  • 18
  • 30
  • Its another data type. If you try with a list of float tuples as points it would fail. – John Sep 13 '22 at 20:52