Skip to content

Instantly share code, notes, and snippets.

@cohnt
Created August 5, 2022 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cohnt/d27d2ed427429a5a8d31d0994c2087b3 to your computer and use it in GitHub Desktop.
Save cohnt/d27d2ed427429a5a8d31d0994c2087b3 to your computer and use it in GitHub Desktop.
# Experimenting with alphashapes to generate 2D worlds with randomly sized and shaped obstacles.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from descartes import PolygonPatch
import alphashape
n_points = 200
alpha = 25.
points = np.random.random(size=(n_points, 2))
gen = alphashape.alphasimplices(points)
tris = []
for simplex, r in gen:
if r < 1/alpha:
tris.append(points[simplex])
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
# ax1.scatter(*(points.T))
# ax2.scatter(*(points.T))
shape = alphashape.alphashape(points, alpha)
ax1.add_patch(PolygonPatch(shape, alpha=0.2))
for tri in tris:
ax2.add_patch(Polygon(tri))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment