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]]


def sum_of_squares(stream): reduce stream as $x (0; . + $x * $x);
 
def distanceSquared(P1; P2): sum_of_squares(P1[0]-P2[0], P1[1]-P2[1]);
 
# Emit {x1,y1, ...} for the input triangle
def xy:
  { x1: .[0][0],
    y1: .[0][1],
    x2: .[1][0],
    y2: .[1][1],
    x3: .[2][0],
    y3: .[2][1] };
 
def EPS: 0.001;
def EPS_SQUARE: EPS * EPS; 