Find if a point is within a triangle.



Task

   Assume points are on a plane defined by (x, y) real number coordinates.
   Given a point P(x, y) and a triangle formed by points A, B, and C, determine if P is within triangle ABC. 
   You may use any algorithm. 
   Bonus: explain why the algorithm you chose works.


Related tasks

   Determine_if_two_triangles_overlap


Also see

 Discussion of several methods. [[1]]
 Determine if a point is in a polygon [[2]]
 Triangle based coordinate systems [[3]]
 Wolfram entry [[4]]


T := [[1.5, 2.4], [5.1, -3.1], [-3.8, 1.2]]
for i, p in [[0, 0], [0, 1], [3, 1], [5.4142857, 14.349206]]
    result .= "[" p.1 ", " p.2 "]  is within triangle?`t" (TriHasP(T, p) ? "ture" : "false") "`n"
MsgBox % result
return
 
TriHasP(T, P){
    Ax := TriArea(T.1.1, T.1.2,  T.2.1, T.2.2,  T.3.1, T.3.2)
    A1 := TriArea(P.1  , P.2  ,  T.2.1, T.2.2,  T.3.1, T.3.2)
    A2 := TriArea(T.1.1, T.1.2,  P.1  , P.2  ,  T.3.1, T.3.2)
    A3 := TriArea(T.1.1, T.1.2,  T.2.1, T.2.2,  P.1  , P.2)
    return (Ax = A1 + A2 + A3)
}
TriArea(x1, y1, x2, y2, x3, y3){
    return Abs((x1 * (y2-y3) + x2 * (y3-y1) + x3 * (y1-y2)) / 2)
}