X-Git-Url: http://git.tomasm.cz/imago.git/blobdiff_plain/f16fe1e775f2159741146264a6494e63b0e2d618..e3efa906f4d1c8203c88b5129cfa63598dbf3476:/geometry.py?ds=sidebyside diff --git a/geometry.py b/geometry.py index b8aea09..23f4095 100644 --- a/geometry.py +++ b/geometry.py @@ -1,6 +1,6 @@ """Imago geometry module""" -from manual import line, intersection +from math import sin, cos, atan, pi class V(object): def __init__(self, x, y): @@ -39,3 +39,41 @@ class V(object): def projection(p, l, v): return V(*intersection(line(p, p + v.normal), line(*l))) + +def l2ad((a, b), size): + """Represent line as (angle, distance). + + Take a line (represented by two points) and image size. + Return the line represented by its angle and distance + from the center of the image. + + """ + if (a[0] - b[0]) == 0: + angle = pi / 2 + else: + q = float(a[1] - b[1]) / (a[0] - b[0]) + angle = atan(q) + + if angle < 0: + angle += pi + if angle > pi: + angle -= pi + + distance = (((a[0] - (size[0] / 2)) * sin(angle)) + + ((a[1] - (size[1] / 2)) * - cos(angle))) + return (angle, distance) + +def line(x, y): + """Return parametric representation of line.""" + a = x[1] - y[1] + b = y[0] - x[0] + c = a * y[0] + b * y[1] + return (a, b, c) + +def intersection(p, q): + """Return intersection of two lines.""" + det = p[0] * q[1] - p[1] * q[0] + if det == 0: + return None + 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)))