boxespy/lamp.py

77 lines
2.0 KiB
Python
Raw Normal View History

2013-04-16 04:25:37 +02:00
#!/usr/bin/python
from boxes import Boxes
2013-06-07 12:51:04 +02:00
import math
2013-04-16 04:25:37 +02:00
class Lamp(Boxes):
def __init__(self):
2013-05-14 17:54:47 +02:00
Boxes.__init__(self, width=1000, height=1000)
2013-04-16 04:25:37 +02:00
self.fingerJointSettings = (5, 5)
def ring(self, r, w):
self.ctx.save()
d = 2*(r+w)
self.roundedPlate(d, d, r)
self.moveTo(r+w, w)
self.corner(360, r)
self.ctx.restore()
2013-06-07 12:51:04 +02:00
def roundedTriangle(self, length, angle, r=0.0):
x = 0.5*(length-2*r)*math.tan(math.radians(angle))
y = 0.5*(length)
self.hole(x, y, 2)
l = 0.5 * (length-2*r) / math.cos(math.radians(angle))
self.corner(90-angle, r)
self.edge(l)
self.corner(2*angle, r)
self.edge(l)
self.corner(90-angle, r)
def side(self, y, h):
self.fingerJoint(y)
self.corner(90)
self.fingerJoint(h)
self.roundedTriangle(y, 70, 25)
self.fingerJoint(h)
self.corner(90)
2013-04-16 04:25:37 +02:00
def render(self, r, w, x, y, h):
2013-04-16 04:25:37 +02:00
"""
r : radius of lamp
w : width of surrounding ring
"""
2013-06-09 18:46:23 +02:00
t = self.thickness
2013-04-16 04:25:37 +02:00
d = 2*(r+w)
self.ctx.save()
2013-06-09 18:46:23 +02:00
self.moveTo(2*self.thickness, 2*t)
2013-04-16 04:25:37 +02:00
self.ring(r, w)
2013-06-09 18:46:23 +02:00
self.moveTo(2*(r+w)+3*t, 0)
2013-05-14 17:54:47 +02:00
self.roundedPlate(d, d, r, holesMargin=w/2.0)
2013-04-16 04:25:37 +02:00
self.ctx.restore()
2013-06-09 18:46:23 +02:00
self.moveTo(2*t, 2*(r+w)+4*t)
2013-04-16 17:02:43 +02:00
self.surroundingWall(d, d, r, 150, top='h', bottom='h')
2013-06-09 18:46:23 +02:00
self.moveTo(0, 150+6*t)
2013-04-16 04:25:37 +02:00
2013-05-14 17:54:47 +02:00
self.rectangularWall(x, y, edges="fFfF", holesMargin=5)
2013-06-09 18:46:23 +02:00
self.moveTo(x+3*t, 0)
2013-05-14 17:54:47 +02:00
self.rectangularWall(x, y, edges="fFfF", holesMargin=5)
2013-06-09 18:46:23 +02:00
self.moveTo(x+4*t, 0)
2013-06-07 12:51:04 +02:00
self.side(y, h)
2013-06-09 18:46:23 +02:00
self.moveTo(y+3*t, 0)
2013-06-07 12:51:04 +02:00
self.side(y, h)
2013-05-14 17:54:47 +02:00
2013-06-09 18:46:23 +02:00
self.moveTo(0, y+3*t)
self.moveTo(-x-y-7*t, 0)
2013-06-07 12:51:04 +02:00
self.rectangularWall(x, h, edges='hFFF', holesMargin=5)
2013-06-09 18:46:23 +02:00
self.moveTo(-x-3*t, 0)
2013-05-14 17:54:47 +02:00
self.rectangularWall(x, h, edges='hFFF', holesMargin=5)
2013-04-16 04:25:37 +02:00
self.ctx.stroke()
self.surface.flush()
l = Lamp()
l.render(100, 20, 250, 140, 120)