printrun-src/printrun/packer.py

changeset 46
cce0af6351f0
parent 15
0bbb006204fc
equal deleted inserted replaced
45:c82943fb205f 46:cce0af6351f0
21 21
22 import Polygon 22 import Polygon
23 import Polygon.Utils 23 import Polygon.Utils
24 24
25 25
26 class Vector2(object): 26 class Vector2:
27 """Simple 2d vector / point class.""" 27 """Simple 2d vector / point class."""
28 28
29 def __init__(self, x=0, y=0): 29 def __init__(self, x=0, y=0):
30 self.x = float(x) 30 self.x = float(x)
31 self.y = float(y) 31 self.y = float(y)
58 (other.x - self.x) ** 2 + 58 (other.x - self.x) ** 2 +
59 (other.y - self.y) ** 2 59 (other.y - self.y) ** 2
60 ) 60 )
61 61
62 62
63 class Rect(object): 63 class Rect:
64 """Simple rectangle object.""" 64 """Simple rectangle object."""
65 def __init__(self, width, height, data={}): 65 def __init__(self, width, height, data={}):
66 self.width = width 66 self.width = width
67 self.height = height 67 self.height = height
68 self.data = data 68 self.data = data
108 def area(self): 108 def area(self):
109 """Area: length * width.""" 109 """Area: length * width."""
110 return self.width * self.height 110 return self.width * self.height
111 111
112 112
113 class PointList(object): 113 class PointList:
114 """Methods for transforming a list of points.""" 114 """Methods for transforming a list of points."""
115 def __init__(self, points=[]): 115 def __init__(self, points=[]):
116 self.points = points 116 self.points = points
117 self._polygon = None 117 self._polygon = None
118 118
140 )) 140 ))
141 141
142 return segs 142 return segs
143 143
144 144
145 class LineSegment(object): 145 class LineSegment:
146 def __init__(self, start, end): 146 def __init__(self, start, end):
147 self.start = start 147 self.start = start
148 self.end = end 148 self.end = end
149 149
150 def length(self): 150 def length(self):
175 """Helper method too automatically return distance.""" 175 """Helper method too automatically return distance."""
176 closest_point = self.closest_point_to_point(point) 176 closest_point = self.closest_point_to_point(point)
177 return closest_point.distance(point) 177 return closest_point.distance(point)
178 178
179 179
180 class Packer(object): 180 class Packer:
181 def __init__(self): 181 def __init__(self):
182 self._rects = [] 182 self._rects = []
183 183
184 def add_rect(self, width, height, data={}): 184 def add_rect(self, width, height, data={}):
185 self._rects.append(Rect(width, height, data)) 185 self._rects.append(Rect(width, height, data))

mercurial