Last active
August 29, 2015 13:57
-
-
Save mpereira/9574586 to your computer and use it in GitHub Desktop.
Determine if a point is within a triangle
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 point-in-triangle? [[px py] [[p0x p0y] [p1x p1y] [p2x p2y]]] | |
(let [n (* 0.5 | |
(+ (* (- p1y) p2x) | |
(* p0y (+ (- p1x) p2x)) | |
(* p0x (- p1y p2y)) | |
(* p1x p2y))) | |
sign (if (neg? n) -1 1) | |
s (* sign | |
(+ (* p0y p2x) | |
(- (* p0x p2y)) | |
(* (- p2y p0y) px) | |
(* (- p0x p2x) py))) | |
t (* sign | |
(+ (* p0x p1y) | |
(- (* p0y p1x)) | |
(* (- p0y p1y) px) | |
(* (- p1x p0x) py)))] | |
(and (>= s 0) (>= t 0) (<= (+ s t) (* 2 n sign))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment