Skip to content

Instantly share code, notes, and snippets.

@mpereira
Last active August 29, 2015 13:57
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 mpereira/9574586 to your computer and use it in GitHub Desktop.
Save mpereira/9574586 to your computer and use it in GitHub Desktop.
Determine if a point is within a triangle
(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