summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStan Seibert <stan@mtrr.org>2012-05-22 11:59:35 -0400
committertlatorre <tlatorre@uchicago.edu>2021-05-09 08:42:39 -0700
commit6fdd210e4850acb503de0e7dfc14127e07eac27b (patch)
tree0597cce9e50a3601d7b0fb13f6036384ea2daa3b
parent94bd48dc40e957ec15e4ba4679e1606a8355977e (diff)
downloadchroma-6fdd210e4850acb503de0e7dfc14127e07eac27b.tar.gz
chroma-6fdd210e4850acb503de0e7dfc14127e07eac27b.tar.bz2
chroma-6fdd210e4850acb503de0e7dfc14127e07eac27b.zip
Add a function to create a Mesh from the coordinates for a convex polygon.
-rw-r--r--chroma/make.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/chroma/make.py b/chroma/make.py
index 9c4c38f..cef287a 100644
--- a/chroma/make.py
+++ b/chroma/make.py
@@ -141,3 +141,19 @@ def torus(radius, offset, nsteps=64, circle_steps=None):
circle_steps = nsteps
profile_angles = np.linspace(0, 2*np.pi, circle_steps)
return rotate_extrude(np.cos(profile_angles) + offset, np.sin(profile_angles), nsteps)
+
+def convex_polygon(x, y):
+ """
+ Return a polygon mesh in the x-y plane. `x` and `y` are the x and
+ y coordinates for the points in the polygon. The simple triangulation
+ method used here requires that the polygon be convex and the points
+ are specified in order.
+ """
+ vertices = np.column_stack( (x, y, np.zeros_like(x)) )
+ # Every triangle includes
+ triangles = np.empty(shape=(len(vertices)-2,3), dtype=np.int32)
+ triangles[:,0] = 0 # Every triangle includes vertex zero
+ triangles[:,1] = np.arange(1, len(vertices)-1)
+ triangles[:,2] = np.arange(2, len(vertices))
+
+ return Mesh(vertices=vertices, triangles=triangles)