Add "classical" Flexboxes

This commit is contained in:
Florian Festi 2013-05-30 18:38:10 +02:00
parent eb634d054e
commit 012442de6c
2 changed files with 170 additions and 0 deletions

83
flexbox.py Executable file
View File

@ -0,0 +1,83 @@
#!/usr/bin/python
import boxes
import math
class FlexBox(boxes.Boxes):
def __init__(self, x, y, z, r=None, thickness=3.0):
c4 = math.pi * r * 0.5
self.x = x
self.y = y
self.z = z
self.r = r or min(x, y)/2.0
width = 2*x + 2*y - 8*r + 4*c4 + 4*thickness
height = y + z + 40
boxes.Boxes.__init__(self, width, height, thickness=thickness)
@boxes.restore
def flexBoxSide(self, x, y, r, callback=None):
space, finger = self.fingerJointSettings
lock = 2.1*space + finger
self.moveTo(r, 0)
for i, l in zip(range(2), (x, y)):
self.cc(callback, i)
self.fingerJoint(l-2*r)
self.corner(90, r)
self.cc(callback, 2)
self.edge(x-2*r)
self.corner(90, r)
self.cc(callback, 3)
self.fingerJoint(lock)
self.cc(callback, 4)
self.fingerJoint(y-2*r-lock)
self.corner(90, r)
def surroundingWall(self):
x, y, z, r = self.x, self.y, self.z, self.r
c4 = math.pi * r * 0.5
space, finger = self.fingerJointSettings
lock = 2.1*space + finger
self.fingerJoint(y-2*r-lock, False)
self.flex(c4, z+2*self.thickness)
self.fingerJoint(x-2*r, False)
self.flex(c4, z+2*self.thickness)
self.fingerJoint(y-2*r, False)
self.flex(c4, z+2*self.thickness)
self.edge(x-2*r)
self.flex(c4, z+2*self.thickness)
self.fingerJoint(lock, False)
self.corner(90)
self.edge(z+2*self.thickness)
self.corner(90)
self.fingerJoint(lock, False)
self.edge(c4)
self.edge(x-2*r)
self.edge(c4)
self.fingerJoint(y-2*r, False)
self.edge(c4)
self.fingerJoint(x-2*r, False)
self.edge(c4)
self.fingerJoint(y-2*r-lock, False)
self.corner(90)
self.edge(z+2*self.thickness)
self.corner(90)
def render(self):
self.moveTo(self.thickness, self.thickness)
self.surroundingWall()
self.moveTo(self.thickness, self.z+4*self.thickness)
self.flexBoxSide(self.x, self.y, self.r)
self.moveTo(2*self.x+3*self.thickness, 0)
self.ctx.scale(-1, 1)
self.flexBoxSide(self.x, self.y, self.r)
self.ctx.stroke()
self.surface.flush()
if __name__=="__main__":
b = FlexBox(200, 200, 200, r=50)
b.render()

87
flexbox2.py Executable file
View File

@ -0,0 +1,87 @@
#!/usr/bin/python
import boxes
import math
class FlexBox(boxes.Boxes):
def __init__(self, x, y, z, r=None, thickness=3.0):
self.x = x
self.y = y
self.z = z
self.r = r or min(x, y)/2.0
c4 = math.pi * r * 0.5
width = 2*x + y - 3*r + 2*c4 + 18*thickness # lock
height = y + z + 8*thickness
boxes.Boxes.__init__(self, width, height, thickness=thickness)
space, finger = self.fingerJointSettings
self.lock = 2.1*space + finger
@boxes.restore
def flexBoxSide(self, x, y, r, callback=None):
self.cc(callback, 0)
self.fingerJoint(x)
self.corner(90, 0)
self.cc(callback, 1)
self.fingerJoint(y-r)
self.corner(90, r)
self.cc(callback, 2)
self.edge(x-2*r)
self.corner(90, r)
self.cc(callback, 3)
self.fingerJoint(self.lock)
self.cc(callback, 4)
self.fingerJoint(y-r-self.lock)
self.corner(90)
def surroundingWall(self):
x, y, z, r = self.x, self.y, self.z, self.r
c4 = math.pi * r * 0.5
lock = self.lock
self.fingerJoint(y-r, False)
self.flex(c4, z+2*self.thickness)
self.edge(x-2*r)
self.flex(c4, z+2*self.thickness)
self.fingerJoint(lock, False)
self.corner(90)
self.edge(z+2*self.thickness)
self.corner(90)
self.fingerJoint(lock, False)
self.edge(c4)
self.edge(x-2*r)
self.edge(c4)
self.fingerJoint(y-r, False)
self.corner(90)
self.edge(self.thickness)
self.fingerJoint(z)
self.edge(self.thickness)
self.corner(90)
def render(self):
c4 = math.pi * self.r * 0.5
self.moveTo(2*self.thickness, self.thickness)
self.ctx.save()
self.surroundingWall()
self.moveTo(self.x+self.y-3*self.r+2*c4+self.lock+1*self.thickness, 0)
self.rectangularWall(self.x, self.z, edges="FFFF")
self.ctx.restore()
self.moveTo(0, self.z+4*self.thickness)
self.flexBoxSide(self.x, self.y, self.r)
self.moveTo(2*self.x+3*self.thickness, 0)
self.ctx.scale(-1, 1)
self.flexBoxSide(self.x, self.y, self.r)
self.ctx.scale(-1, 1)
self.moveTo(2*self.thickness, 0)
self.rectangularWall(self.z, self.y-self.r-self.lock, edges="fFeF")
self.ctx.stroke()
self.surface.flush()
if __name__=="__main__":
b = FlexBox(200, 200, 200, r=50)
b.render()