Created
August 21, 2015 13:38
-
-
Save cassiel/3e725b49670356a9b936 to your computer and use it in GitHub Desktop.
2D line intersection test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)])) |
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
Thanks, I have blatantly stolen that here