Skip to content

Instantly share code, notes, and snippets.

@bengolder
Created August 3, 2011 01:49
Show Gist options
  • Save bengolder/1121702 to your computer and use it in GitHub Desktop.
Save bengolder/1121702 to your computer and use it in GitHub Desktop.
CakeBoids ARCH124B sec 001 example
from Rhino.Geometry import Vector3d, Point3d, Curve
class CakeBot:
def __init__(self, radius, center):
self.radius = radius
self.location = center
self.height = 0.5
self.icingFlavor = "vanilla"
self.numSlices = 8
self.heading = Vector3d( -center.X, -center.Y, -center.Z )
self.trail = [center]
def findDirection(self, others ):
stepSize = 0.5
if others:
# find new heading
self.align(others)
self.cohere(others)
self.heading = Vector3d.Multiply( self.heading, stepSize )
def align(self, others):
vector = others[0].heading
for other in others[1:]:
vector = Vector3d.Add( vector, other.heading)
vector.Unitize()
self.heading = vector
def cohere(self, others):
vector = Point3d.Subtract( self.location, others[0].location )
for other in others[1:]:
newVector = Point3d.Subtract( self.location, others[0].location)
vector = Vector3d.Add( vector, newVector )
vector.Unitize()
self.heading = Vector3d.Add( vector, self.heading )
self.heading.Unitize()
def findOthers(self, all):
# find the other cakes within range
neighbors = []
for cake in all:
if cake != self:
distance = self.location.DistanceTo( cake.location )
if distance < sensingRadius:
neighbors.append(cake)
return neighbors
def fly(self):
newPoint = Point3d.Add( self.location, self.heading )
self.trail.append( newPoint)
self.location = newPoint
print self, 'just flew to', newPoint, '!!'
def changeIcing(self, newFlavor):
self.icingFlavor = newFlavor
# here is the main function
# where we do everything
# create all the cakes
cakeList = []
for point in initialPoints:
bot = CakeBot(0.5, point)
bot.height = cakeHeight
bot.icingFlavor = defaultFlavor
cakeList.append( bot )
# make Cakes Fly
for i in range(numSteps):
for cake in cakeList:
neighbors = cake.findOthers(cakeList)
cake.findDirection( neighbors )
for cake in cakeList:
cake.fly()
# output the results
locationList = []
for bot in cakeList:
locationList.append( bot.location )
a = locationList
bots = cakeList
for bot in cakeList:
print bot.icingFlavor
trails = []
for bot in cakeList:
crv = Curve.CreateControlPointCurve( bot.trail, 3 )
trails.append(crv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment