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),
+ fill=(255, 64, 64))
corners = []
color=(64, 64, 255)
line_width = 2
pygame.quit()
raise UserQuitError
if event.type == pygame.MOUSEBUTTONDOWN:
- if len(corners) >= 4:
+ if len(corners) >= 4:
corners = []
im = im_orig.copy()
draw = ImageDraw.Draw(im)
width=line_width)
draw.line((corners[3], corners[0]), fill=color,
width=line_width)
- l1 = half_line(corners)
- draw.line(l1, fill=color, width=line_width)
- l2 = half_line(corners[1:4] + [corners[0]])
- draw.line(l2, fill=color, width=line_width)
- c = center(corners)
- draw.ellipse((c[0] - 1, c[1] - 1, c[0] + 1, c[1] + 1),
- fill=(255, 64, 64))
+ l_vert = lines(corners, 0)
+ for l in l_vert:
+ draw.line(l, fill=color, width=line_width)
+ l_hor = lines(corners[1:4] + [corners[0]], 0)
+ for l in l_hor:
+ draw.line(l, fill=color, width=line_width)
+ l_vert = sorted(l_vert)
+ l_hor = sorted(l_hor)
+ for i in [3, 8, 14]:
+ for j in [3, 8, 14]:
+ hoshi(intersection(line(l_vert[i][0], l_vert[i][1]),
+ line(l_hor[j][0], l_hor[j][1])))
screen.display_picture(im)
clock.tick(15)
+def lines(corners, n):
+ if n == 0:
+ x = half_line(corners)
+ return (lines([corners[0], x[0], x[1], corners[3]], n + 1) + [x] +
+ lines([x[0], corners[1], corners[2], x[1]], n + 1))
+ else:
+ x = half_line(corners)
+ c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))
+ d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
+ l = (intersection(line(corners[0], corners[1]), line(c,d)),
+ intersection(line(corners[2], corners[3]), line(c,d)))
+ l2 = half_line([corners[0], l[0], l[1], corners[3]])
+ if n == 1:
+ return ([l, l2] + lines([l[0], l2[0], l2[1], l[1]], 2)
+ + lines([corners[0], l2[0], l2[1], corners[3]], 2)
+ + lines([l[0], corners[1], corners[2], l[1]], 2))
+ if n == 2:
+ return [l, l2]
+
+
def half_line(corners):
c = center(corners)
- d1 = intersection(line(corners[0], corners[3]),
- line(corners[1], corners[2]))
- p1 = intersection(line(c,d1), line(corners[0],
- corners[1]))
- p2 = intersection(line(c,d1), line(corners[2],
- corners[3]))
+ d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
+ if d:
+ l = line(c, d)
+ else:
+ l = line(c, (c[0] + corners[0][0] - corners[3][0],
+ c[1] + corners[0][1] - corners[3][1]))
+ p1 = intersection(l, line(corners[0], corners[1]))
+ p2 = intersection(l, line(corners[2], corners[3]))
return (p1, p2)
+
def center(corners):
- return intersection(line(corners[0], corners[2]), line(corners[1],
- corners[3]))
+ return intersection(line(corners[0], corners[2]),
+ line(corners[1], corners[3]))
def line(x, y):
- a = float(x[1] - y[1])
- b = float(y[0] - x[0])
+ a = x[1] - y[1]
+ b = y[0] - x[0]
c = a * y[0] + b * y[1]
return (a, b, c)
det = p[0] * q[1] - p[1] * q[0]
if det == 0:
return None
- return (int(round((q[1] * p[2] - p[1] * q[2]) / det)), int(round((p[0] *
- q[2] - q[0] * p[2]) / det)))
+ return (int(round(float(q[1] * p[2] - p[1] * q[2]) / det)),
+ int(round(float(p[0] * q[2] - q[0] * p[2]) / det)))