printrun-src/printrun/packer.py

Sat, 08 Sep 2018 12:00:12 +0200

author
mdd
date
Sat, 08 Sep 2018 12:00:12 +0200
changeset 39
74801c0f2709
parent 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

bad idea :(

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 # Imported from python-rectangle-packer commit 32fce1aaba
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2 # https://github.com/maxretter/python-rectangle-packer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 # Python Rectangle Packer - Packs rectangles around a central point
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # Copyright (C) 2013 Max Retter
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 import math
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 import Polygon
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 import Polygon.Utils
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 class Vector2(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 """Simple 2d vector / point class."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 def __init__(self, x=0, y=0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 self.x = float(x)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 self.y = float(y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 def __eq__(self, other):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 return self.x == other.x and self.y == other.y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 def add(self, other):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 return Vector2(self.x + other.x, self.y + other.y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 def sub(self, other):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 return Vector2(self.x - other.x, self.y - other.y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 def scale(self, factor):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 return Vector2(self.x * factor, self.y * factor)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 def magnitude(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 return math.sqrt(self.dot_product(self))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 def unit(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 """Build unit vector."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 return self.scale(1 / self.magnitude())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 def dot_product(self, other):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 return self.x * other.x + self.y * other.y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 def distance(self, other):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 """Distance forumla for other point."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 return math.sqrt(
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 (other.x - self.x) ** 2 +
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 (other.y - self.y) ** 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 )
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 class Rect(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 """Simple rectangle object."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 def __init__(self, width, height, data={}):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 self.width = width
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 self.height = height
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 self.data = data
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 # upper left
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 self.position = Vector2()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 def half(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 """Half width and height."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 return Vector2(
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 self.width / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 self.height / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 )
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 def expand(self, width, height):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 """Builds a new rectangle based on this one with given offsets."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 expanded = Rect(self.width + width, self.height + height)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 expanded.set_center(self.center())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 return expanded
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 def point_list(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 top = self.position.y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 right = self.position.x + self.width
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 bottom = self.position.y + self.height
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 left = self.position.x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 return PointList([
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 (left, top),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 (right, top),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 (right, bottom),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 (left, bottom),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 ])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 def center(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 """Center of rect calculated from position and dimensions."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 return self.position.add(self.half())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 def set_center(self, center):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 """Set the position based on a new center point."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 self.position = center.sub(self.half())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 def area(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 """Area: length * width."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 return self.width * self.height
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 class PointList(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 """Methods for transforming a list of points."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 def __init__(self, points=[]):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 self.points = points
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 self._polygon = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 def polygon(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 """Builds a polygon from the set of points."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 if not self._polygon:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 self._polygon = Polygon.Polygon(self.points)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 return self._polygon
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 def segments(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 """Returns a list of LineSegment objects."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 segs = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 for i, point in enumerate(self.points[1:]):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 index = i + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 segs.append(LineSegment(
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 Vector2(self.points[index - 1][0], self.points[index - 1][1]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 Vector2(self.points[index][0], self.points[index][1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 ))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 segs.append(LineSegment(
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 Vector2(self.points[-1][0], self.points[-1][1]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 Vector2(self.points[0][0], self.points[0][1]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 ))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 return segs
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 class LineSegment(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 def __init__(self, start, end):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 self.start = start
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 self.end = end
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 def length(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 """Length of segment vector."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 return self.end.sub(self.start).magnitude()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 def closest_point_to_point(self, point):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 """Point along segment that is closest to given point."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 segment_vector = self.end.sub(self.start)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 point_vector = point.sub(self.start)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 seg_mag = segment_vector.magnitude()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 # project point_vector on segment_vector
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 projection = segment_vector.dot_product(point_vector)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 # scalar value used to interpolate new point along segment_vector
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 scalar = projection / seg_mag ** 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 # clamp on [0,1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 scalar = 1.0 if scalar > 1.0 else scalar
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 scalar = 0.0 if scalar < 0.0 else scalar
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 # interpolate scalar along segment and add start point back in
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 return self.start.add(segment_vector.unit().scale(scalar * seg_mag))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 def closest_distance_to_point(self, point):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 """Helper method too automatically return distance."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 closest_point = self.closest_point_to_point(point)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 return closest_point.distance(point)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180 class Packer(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 def __init__(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 self._rects = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 def add_rect(self, width, height, data={}):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 self._rects.append(Rect(width, height, data))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 def pack(self, padding=0, center=Vector2()):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 # init everything
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 placed_rects = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 sorted_rects = sorted(self._rects, key=lambda rect: -rect.area())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 # double padding due to halfing later on
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 padding *= 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 for rect in sorted_rects:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 if not placed_rects:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 # first rect, right on target.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 rect.set_center(center)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 # Expand each rectangle based on new rect size and padding
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 # get a list of points
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 # build a polygon
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204 point_lists = [
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 pr.expand(rect.width + padding, rect.height + padding).point_list().polygon()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 for pr in placed_rects
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 ]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 # take the union of all the polygons (relies on + operator override)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 # the [0] at the end returns the first "contour", which is the only one we need
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 bounding_points = PointList(sum(
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 point_lists[1:],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 point_lists[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 )[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 # find the closest segment
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 closest_segments = sorted(
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 bounding_points.segments(),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 key=lambda segment: segment.closest_distance_to_point(center)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 )
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 # get the closest point
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 place_point = closest_segments[0].closest_point_to_point(center)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 # set the rect position
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 rect.set_center(place_point)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 placed_rects.append(rect)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 return placed_rects

mercurial