From 75241f5f9ddb1e48744878e987136224f9526845 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sun, 31 Dec 2017 22:38:15 -0500 Subject: trim unnecessary whitespace --- embroider_simulate.py | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/embroider_simulate.py b/embroider_simulate.py index 6fdff57c..0bee6327 100644 --- a/embroider_simulate.py +++ b/embroider_simulate.py @@ -53,6 +53,7 @@ class EmbroiderySimulator(wx.Frame): else: return + self.trim_margins() self.width, self.height = self.get_dimensions() def adjust_speed(self, duration): @@ -159,15 +160,48 @@ class EmbroiderySimulator(wx.Frame): return segments + def all_coordinates(self): + for segment in self.segments: + start, end = segment[0] + + yield start + yield end + + def trim_margins(self): + """remove any unnecessary whitespace around the design""" + + min_x = sys.maxint + min_y = sys.maxint + + for x, y in self.all_coordinates(): + min_x = min(min_x, x) + min_y = min(min_y, y) + + + new_segments = [] + + for segment in self.segments: + (start, end), color = segment + + new_segment = ( + ( + (start[0] - min_x, start[1] - min_y), + (end[0] - min_x, end[1] - min_y), + ), + color + ) + + new_segments.append(new_segment) + + self.segments = new_segments + def get_dimensions(self): width = 0 height = 0 - for segment in self.segments: - (start_x, start_y), (end_x, end_y) = segment[0] - - width = max(width, start_x, end_x) - height = max(height, start_y, end_y) + for x, y in self.all_coordinates(): + width = max(width, x) + height = max(height, y) return width, height -- cgit v1.2.3