Skip to content

Instantly share code, notes, and snippets.

@RobotGrrl
Created April 1, 2015 19:49
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 RobotGrrl/6f07d05ad808ae7df0ba to your computer and use it in GitHub Desktop.
Save RobotGrrl/6f07d05ad808ae7df0ba to your computer and use it in GitHub Desktop.
Pyramid morphed with a sphere
import fab
title('spheramid')
input('x0',float)
input('y0',float)
input('z0',float)
input('r',float)
input('width',float)
input('height',float)
input('weight',float)
# pyramid by Neil Gershenfeld 1/24/15
def pyramid_height(x0,y0,z0,w,h):
from fab.types import Shape, Transform
if (h >= 0):
# max(Y-y0+(w/2)*(z0+h-Z)/h,
# max(y0-(w/2)*(z0+h-Z)/h-Y,
# max(X-x0+(w/2)*(z0+h-Z)/h,
# max(x0-(w/2)*(z0+h-Z)/h-X,z0-Z))
return Shape(
'a-Y+f%(y0)g*/f%(w)gf2/-+f%(z0)gf%(h)gZf%(h)ga--f%(y0)g*/f%(w)gf2/-+f%(z0)gf%(h)gZf%(h)gYa-X+f%(x0)g*/f%(w)gf2/-+f%(z0)gf%(h)gZf%(h)ga--f%(x0)g*/f%(w)gf2/-+f%(z0)gf%(h)gZf%(h)gX-f%(z0)gZ' % locals(),
x0-w/2,y0-w/2,z0,x0+w/2,y0+w/2,z0+h)
else:
# max(Y-y0+(w/2)*(z0+h-Z)/h,
# max(y0-(w/2)*(z0+h-Z)/h-Y,
# max(X-x0+(w/2)*(z0+h-Z)/h,
# max(x0-(w/2)*(z0+h-Z)/h-X,Z-z0))
return Shape(
'a-Y+f%(y0)g*/f%(w)gf2/-+f%(z0)gf%(h)gZf%(h)ga--f%(y0)g*/f%(w)gf2/-+f%(z0)gf%(h)gZf%(h)gYa-X+f%(x0)g*/f%(w)gf2/-+f%(z0)gf%(h)gZf%(h)ga--f%(x0)g*/f%(w)gf2/-+f%(z0)gf%(h)gZf%(h)gX-Zf%(z0)g' % locals(),
x0-w/2,y0-w/2,z0+h,x0+w/2,y0+w/2,z0)
# morph by Neil Gershenfeld 2/1/15
def morph(shape0,shape1,weight):
from fab.types import Shape,Transform
# shape = weight*shape0+(1-weight)*shape1
w0 = weight
w1 = 1-weight
s0 = "*f"+str(w0)+shape0.math
s1 = "*f"+str(w1)+shape1.math
xmin = w0*shape0.bounds.xmin+w1*shape1.bounds.xmin
xmax = w0*shape0.bounds.xmax+w1*shape1.bounds.xmax
ymin = w0*shape0.bounds.ymin+w1*shape1.bounds.ymin
ymax = w0*shape0.bounds.ymax+w1*shape1.bounds.ymax
if math.isinf(shape0.bounds.zmin):
return Shape("+"+s0+s1,
xmin,ymin,xmax,ymax)
else:
zmin = w0*shape0.bounds.zmin+w1*shape1.bounds.zmin
zmax = w0*shape0.bounds.zmax+w1*shape1.bounds.zmax
return Shape("+"+s0+s1,
xmin,ymin,zmin,xmax,ymax,zmax)
s1 = pyramid_height(x0,y0,z0,width,height)
s2 = fab.shapes.sphere(x0+0.1, y0+0.1, z0-0.1, r)
output('transform', morph(s1,s2,weight))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment