> See Wikipedia for the usual solution, given points on both lines: > > http://en.wikipedia.org/wiki/Line-line_intersection # -*- coding: utf-8 -*- import numpy as N
def intersect(line1, line2): """\begin{align} P(x,y)= \bigg(&\frac{(x_1 y_2-y_1 x_2)(x_3-x_4)- (x_1-x_2)(x_3 y_4-y_3 x_4)}{(x_1-x_2)(y_3-y_4)-(y_1-y_2)(x_3-x_4)}, \\ &\frac{(x_1 y_2-y_1 x_2)(y_3-y_4)-(y_1-y_2)(x_3 y_4-y_3 x_4)}{(x_1-x_2) (y_3-y_4)-(y_1-y_2)(x_3-x_4)}\bigg) \end{align}""" x_1 = line1.x0[0] y_1 = line1.x0[1] x_2 = line1.x1[0] y_2 = line1.x1[1] x_3 = line2.x0[0] y_3 = line2.x0[1] x_4 = line2.x1[0] y_4 = line2.x1[1] try: denom = float((x_1 - x_2) * (y_3 - y_4) - (y_1 - y_2) * (x_3 - x_4)) x = ((x_1 * y_2 - y_1 * x_2) * (x_3 - x_4) - (x_1 - x_2) * (x_3 * y_4 - y_3 * x_4)) / denom y = ((x_1 * y_2 - y_1 * x_2) * (y_3 - y_4) - (y_1 - y_2) * (x_3 * y_4 - y_3 * x_4)) / denom except ZeroDivisionError: return return x, y class Line(object): def __init__(self, pkts=None): self.x0 = N.array(pkts[0]) self.x1 = N.array(pkts[1]) if __name__ == "__main__": line1 = Line(((0., 0.), (1., 1.))) line2 = Line(((0., 1.), (1., 0.))) print intersect(line1, line2) -- http://mail.python.org/mailman/listinfo/python-list