Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix Drawing.scale_to_fit to center correctly #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions axi/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,38 @@ def scale_to_fit_width(self, width, padding=0):
return self.scale_to_fit(width, 1e9, padding)

def scale_to_fit(self, width, height, padding=0):
width -= padding * 2
height -= padding * 2
scale = min(width / self.width, height / self.height)
s_width = width - padding * 2
s_height = height - padding * 2
scale = min(s_width / self.width, s_height / self.height)
return self.scale(scale, scale).center(width, height)

def rotate_and_scale_to_fit(self, width, height, padding=0, step=5):
drawings = []
width -= padding * 2
height -= padding * 2
s_width = width - padding * 2
s_height = height - padding * 2
for angle in range(0, 180, step):
drawing = self.rotate(angle)
scale = min(width / drawing.width, height / drawing.height)
scale = min(s_width / drawing.width, s_height / drawing.height)
drawings.append((scale, drawing))
scale, drawing = max(drawings)
return drawing.scale(scale, scale).center(width, height)

def crop(self, width, height):
paths = []
for path in self.paths:
ok = True
new_path = []
for x, y in path:
if x < 0 or y < 0 or x > width or y > height:
if new_path:
paths.append(new_path)
new_path = []
else:
new_path.append((x,y))
if new_path:
paths.append(new_path)
return Drawing(paths)

def remove_paths_outside(self, width, height):
paths = []
for path in self.paths:
Expand Down