X-Git-Url: http://git.tomasm.cz/imago.git/blobdiff_plain/b91b0e43b25e64b3516b9156ca42e7309665628b..e3efa906f4d1c8203c88b5129cfa63598dbf3476:/geometry.py?ds=inline diff --git a/geometry.py b/geometry.py index 09b0199..23f4095 100644 --- a/geometry.py +++ b/geometry.py @@ -1,5 +1,7 @@ """Imago geometry module""" +from math import sin, cos, atan, pi + class V(object): def __init__(self, x, y): self.x = x @@ -15,7 +17,7 @@ class V(object): return V(other * self.x, other * self.y) def __len__(self): - return 2; + return 2 def __getitem__(self, key): if key == 0: @@ -35,5 +37,43 @@ class V(object): def normal(self): return V(-self.y, self.x) -def projection(point, line, vector): - return V(*intersection(g_line(point, point + vector.normal), g_line(*line))) +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)))