Skip to content

Instantly share code, notes, and snippets.

@winobes
Last active August 29, 2015 14:14
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 winobes/75efe8580b1fe02cc58f to your computer and use it in GitHub Desktop.
Save winobes/75efe8580b1fe02cc58f to your computer and use it in GitHub Desktop.
def simplex_points(n):
"""
Generates subtriangles of the 3d 0-1 simplex (tripples of tripples).
`n` is the number of triangles along an edge (so it total it generates a
number of subtriangles equal to the n'th triangular number)
"""
def down(x,y,z):
return (x+1, y, z-1)
def left(x,y,z):
return (x-1, y+1, z)
def triangle(fst,snd,trd):
def point(x,y,z):
return (x/n, y/n, z/n)
return (point(*fst),point(*snd),point(*trd))
for i in range(1, n+1):
fst = ((n-i),0,i)
snd = down(*fst)
trd = left(*snd)
yield triangle(fst,snd,trd)
last_left = True
while not trd == ((n-i),i,0):
fst = snd
snd = trd
trd = down(*trd) if last_left else left(*trd)
last_left = not last_left
yield triangle(fst,snd,trd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment