--- /dev/null
+
+def intersection(l1, l2):
+ a1, b1, c1 = points_to_line(*l1)
+ a2, b2, c2 = points_to_line(*l2)
+ delim = float(a1 * b2 - b1 * a2)
+ x = (b1 * c2 - c1 * b2) / delim
+ y = (c1 * a2 - a1 * c2) / delim
+ return x, y
+
+def points_to_line((x1, y1), (x2, y2)):
+ return (y2 - y1, x1 - x2, x2 * y1 - x1 * y2)
+
+def fill(l1, l2, lh, n):
+ if n == 0:
+ return []
+ l11, l12 = l1
+ l21, l22 = l2
+ lh1, lh2 = lh
+ lmt = intersection((lh1, l21), (lh2, l11))
+ lmb = intersection((lh1, l22), (lh2, l12))
+ lm = (intersection((l11, l21), (lmt, lmb)),
+ intersection((l12, l22), (lmt, lmb)))
+ if n == 1:
+ return [lm]
+ if n % 2 == 1:
+ lhc = intersection(lh, lm)
+ return (fill(l1, lm, (lh1, lhc), n / 2) +
+ [lm] +
+ fill(lm, l2, (lhc, lh2), n / 2))
+ elif n == 2:
+ # TODO fine tune this
+ nlt = intersection((lh1, l21), (l11, l22))
+ nlb = intersection((lh1, l22), (l12, l21))
+ nrt = intersection((lh2, l11), (l12, l21))
+ nrb = intersection((lh2, l12), (l11, l22))
+ nl = (intersection((l11, l21), (nlt, nlb)),
+ intersection((l12, l22), (nlt, nlb)))
+ nr = (intersection((l11, l21), (nrt, nrb)),
+ intersection((l12, l22), (nrt, nrb)))
+ return [nl, nr]
+
+def expand(l1, l2, lh):
+ l11, l12 = l1
+ l21, l22 = l2
+ lh1, lh2 = lh
+ nt = intersection((l12, lh2), (l11, l21))
+ nb = intersection((l11, lh2), (l12, l22))
+ return (nt, nb)
--- /dev/null
+import pickle
+import matplotlib.pyplot as pyplot
+from math import sqrt
+import random
+import sys
+
+import src.linef as linef
+import src.gridf as gridf
+from src.manual import lines as g_grid
+
+import new_geometry as gm
+
+def plot_line(line, c):
+ points = linef.line_from_angl_dist(line, (520, 390))
+ pyplot.plot(*zip(*points), color=c)
+
+def dst((x, y), (a, b, c)):
+ return abs(a * x + b * y + c) / sqrt(a*a+b*b)
+
+def points_to_line((x1, y1), (x2, y2)):
+ return (y2 - y1, x1 - x2, x2 * y1 - x1 * y2)
+
+def to_general(line):
+ points = linef.line_from_angl_dist(line, (520, 390))
+ return points_to_line(*points)
+
+def nearest(lines, point):
+ return min(map(lambda l: dst(point, l), lines))
+
+size = (520, 390)
+
+points = pickle.load(open('edges.pickle'))
+
+lines = pickle.load(open('lines.pickle'))
+
+r_lines = pickle.load(open('r_lines.pickle'))
+
+#pyplot.scatter(*zip(*sum(r_lines, [])))
+#pyplot.show()
+
+l1, l2 = lines
+
+while True:
+ l1s = random.sample(l2, 2)
+ l1s.sort(key=lambda l: l[1])
+ corners = map(lambda l:linef.line_from_angl_dist(l, size), l1s)
+ middle = ((0, 195),(520, 195))
+ # TODO! can I assume anything to be perspectively disorted square?
+ # TODO! take lower and middle and construct top
+ lh = (gm.intersection(corners[0], middle), gm.intersection(corners[1], middle))
+ grid = gm.fill(corners[0], corners[1], lh , 3)
+ grid = [corners[0]] + grid + [corners[1]]
+ grid.append(gm.expand(grid[-2], grid[-1], ((gm.intersection(middle, grid[-2]),
+ (gm.intersection(middle, grid[-1]))))))
+ grid.append(gm.expand(grid[-2], grid[-1], ((gm.intersection(middle, grid[-2]),
+ (gm.intersection(middle, grid[-1]))))))
+ map(lambda l: pyplot.plot(*zip(*l), color='b'), grid)
+
+ plot_line(l1s[0], 'g')
+ plot_line(l1s[1], 'r')
+
+ pyplot.xlim(0, 520)
+ pyplot.ylim(0, 390)
+ pyplot.show()
+
+sys.exit()
+
+lines_general = map(to_general, sum(lines, []))
+
+near_points = [p for p in points if nearest(lines_general, p) <= 2]
+
+pyplot.scatter(*zip(*near_points))
+
+
+for l in lines[0]:
+ plot_line(l, 'g')
+
+for l in lines[1]:
+ plot_line(l, 'g')
+
+pyplot.xlim(0, 520)
+pyplot.ylim(0, 390)
+pyplot.show()