Skip to content

Instantly share code, notes, and snippets.

@cassiel
Created August 21, 2015 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cassiel/3e725b49670356a9b936 to your computer and use it in GitHub Desktop.
Save cassiel/3e725b49670356a9b936 to your computer and use it in GitHub Desktop.
2D line intersection test
(defn intersection-2D
"Intersection of two lines in 2D space. No checks for parallel or coincident lines."
[[[x1 y1] [x2 y2]]
[[x3 y3] [x4 y4]]]
(let [denom (- (* (- x1 x2) (- y3 y4))
(* (- y1 y2) (- x3 x4)))
x1y2-y1x2 (- (* x1 y2) (* y1 x2))
x3y4-y3x4 (- (* x3 y4) (* y3 x4))
px-num (- (* x1y2-y1x2 (- x3 x4))
(* (- x1 x2) x3y4-y3x4))
py-num (- (* x1y2-y1x2 (- y3 y4))
(* (- y1 y2) x3y4-y3x4))]
[(/ px-num denom)
(/ py-num denom)]))
@simon-brooke
Copy link

Thanks, I have blatantly stolen that here

@cassiel
Copy link
Author

cassiel commented May 28, 2020

No problem. I can't remember offhand whether I actually wrote it or just found it somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment