Skip to content

Instantly share code, notes, and snippets.

@oisincar
Last active July 5, 2018 10:56
Show Gist options
  • Save oisincar/a41cf965ab4c51ae6e06b01078f7e4c9 to your computer and use it in GitHub Desktop.
Save oisincar/a41cf965ab4c51ae6e06b01078f7e4c9 to your computer and use it in GitHub Desktop.
import numpy as np
def main():
print intersectLines([0.,2.], [1.,1.], [3.,3.], [-1., -5.])
def intersectLines(s1, e1, s2, e2):
# Append 1 to all points. Make them (x, y, 1)
s1, e1, s2, e2 = [np.append(p, 1) for p in [s1, e1, s2, e2]]
# Equations of lines :O!
# (a, b, c) in: ax + by + c = 0
l1 = np.cross(s1, e1)
l2 = np.cross(s2, e2)
# Intersection (:O :O)
# (kx, ky, k).
# If k is 0, then no intersection/ intersection at 'infinity' (:O)
kx, ky, k = np.cross(l1, l2)
return [kx/k, ky/k]
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment