From 36f292ecceb8d5978bffcfce45b5ac804b5d9486 Mon Sep 17 00:00:00 2001 From: Tomas Musil Date: Mon, 21 Jul 2014 23:02:27 +0200 Subject: [PATCH] much better manual mode --- src/manual.py | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/manual.py b/src/manual.py index 67f58e0..9b22316 100644 --- a/src/manual.py +++ b/src/manual.py @@ -22,6 +22,9 @@ class Screen: self._screen.blit(pg_img, (0, 0)) pygame.display.flip() +def dst((x1, y1), (x2, y2)): + return (x1 - x2) ** 2 + (y1 - y2) ** 2 + def find_lines(im_orig): # TODO rename, refactor, comment @@ -34,7 +37,7 @@ def find_lines(im_orig): hoshi = lambda c: draw.ellipse((c[0] - 1, c[1] - 1, c[0] + 1, c[1] + 1), fill=(255, 64, 64)) corners = [] - color = (64, 64, 255) + color = (32, 255, 32) line_width = 1 lines_r = [] @@ -47,29 +50,31 @@ def find_lines(im_orig): else: raise UserQuitError if event.type == pygame.MOUSEBUTTONDOWN: + np = pygame.mouse.get_pos() if len(corners) >= 4: - corners = [] + corners.sort(key=lambda p: dst(p, np)) + corners = corners[1:] + corners.append(np) + (x, y) = corners[-1] + draw.line((x-2, y, x + 2, y), fill=color) + draw.line((x, y+2, x, y-2), fill=color) + if len(corners) == 4: im = im_orig.copy() draw = ImageDraw.Draw(im) - - if len(corners) < 4: - corners.append(pygame.mouse.get_pos()) - draw.point(corners[:-1], fill=color) - if len(corners) == 4: - l_vert, l_hor = lines(corners) - for l in l_vert: - draw.line(l, fill=color, width=line_width) - for l in l_hor: - draw.line(l, fill=color, width=line_width) - # TODO sort by distance - l_vert.sort() - l_hor.sort() - for i in [3, 9, 15]: - for j in [3, 9, 15]: - hoshi(intersection(line(l_vert[i][0], l_vert[i][1]), - line(l_hor[j][0], l_hor[j][1]))) - lines_r = [[l2ad(l, im.size) for l in l_vert], - [l2ad(l, im.size) for l in l_hor]] + l_vert, l_hor = lines(corners) + for l in l_vert: + draw.line(l, fill=color, width=line_width) + for l in l_hor: + draw.line(l, fill=color, width=line_width) + # TODO sort by distance + l_vert.sort() + l_hor.sort() + for i in [3, 9, 15]: + for j in [3, 9, 15]: + hoshi(intersection(line(l_vert[i][0], l_vert[i][1]), + line(l_hor[j][0], l_hor[j][1]))) + lines_r = [[l2ad(l, im.size) for l in l_vert], + [l2ad(l, im.size) for l in l_hor]] screen.display_picture(im) clock.tick(15) -- 2.4.2