import pygame
import ImageDraw
from math import sqrt, acos, copysign
+
from geometry import l2ad, line, intersection
class UserQuitError(Exception):
pass
class Screen:
+ # TODO isn't this a duplicate of something?
def __init__(self, res):
pygame.init()
pygame.display.set_mode(res)
def display_picture(self, img):
pg_img = pygame.image.frombuffer(img.tostring(), img.size, img.mode)
- self._screen.blit(pg_img, (0,0))
+ self._screen.blit(pg_img, (0, 0))
pygame.display.flip()
def find_lines(im_orig):
+ # TODO rename, refactor, comment
im = im_orig.copy()
screen = Screen(im.size)
- done = False
clock = pygame.time.Clock()
draw = ImageDraw.Draw(im)
hoshi = lambda c: draw.ellipse((c[0] - 1, c[1] - 1, c[0] + 1, c[1] + 1),
line_width = 1
lines_r = []
- while not done:
+ while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
pygame.quit()
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
+ # TODO sort by distance
l_vert.sort()
l_hor.sort()
for i in [3, 9, 15]:
clock.tick(15)
def lines(corners):
- #TODO Error on triangle
+ # TODO Error on triangle
cor_d = [(corners[0], (c[0] - corners[0][0], c[1] - corners[0][1]), c) for c in
corners[1:]]
cor_d = [(float(a[0] * b[0] + a[1] * b[1]) / (sqrt(a[0] ** 2 + a[1] ** 2) *
[(corners[0], corners[1]), (corners[2], corners[3])])
def _lines(corners, n):
+ # TODO what is this?
if n == 0:
x = half_line(corners)
return (_lines([corners[0], x[0], x[1], corners[3]], n + 1) + [x] +
def half_line(corners):
+ # TODO what is this?
c = center(corners)
d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
if d:
p2 = intersection(l, line(corners[2], corners[3]))
return (p1, p2)
-
def center(corners):
+ """Given a list of four corner points, return the center of the square."""
return intersection(line(corners[0], corners[2]),
line(corners[1], corners[3]))