I have a sample of points that trace a very clear geometrical shape. I am trying to recover the shape. I can do it with simple shapes, but I cannot do it for more complex ones.
Here is my attempt:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
# Define the size of the squares and the number of points
square_size = 3
num_points = 200
# Generate random points inside the first and second squares
square1_points = np.random.uniform(0, square_size, (num_points, 2))
square2_points = np.random.uniform(square_size - 1, 2 * square_size - 1, (num_points, 2))
# Combine the points from both squares
combined_points = np.vstack((square1_points, square2_points))
# Create a DataFrame
df_points = pd.DataFrame(combined_points, columns=['x', 'y'])
fig, ax = plt.subplots(figsize=(7, 7))
ax.grid(False)
ax.scatter(df_points['x'], df_points['y'], color='red')
# Compute and plot the convex hull
hull = ConvexHull(combined_points)
for simplex in hull.simplices:
ax.plot(combined_points[simplex, 0], combined_points[simplex, 1], 'b-', linewidth=4)
plt.show()
The output looks like this:
Clearly the blue contour does not follow the contour generated by the red dots. How can I fix it?
As you can see, I obtain the expected output for simple geometrical figures (square and circle):