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


-- triangle.ads
generic
	type Dimension is private;
	Zero, Two: Dimension;
	with function "*"(Left, Right: in Dimension) return Dimension is <>;
	with function "/"(Left, Right: in Dimension) return Dimension is <>;
	with function "+"(Left, Right: in Dimension) return Dimension is <>;
	with function "-"(Left, Right: in Dimension) return Dimension is <>;
	with function ">"(Left, Right: in Dimension) return Boolean is <>;
	with function "="(Left, Right: in Dimension) return Boolean is <>;
	with function Image(D: in Dimension) return String is <>;
package Triangle is
 
	type Point is record
		X: Dimension;
		Y: Dimension;
	end record;
 
	type Triangle_T is record
		A,B,C: Point;
	end record;
 
	function Area(T: in Triangle_T) return Dimension;
 
	function IsPointInTriangle(P: Point; T: Triangle_T) return Boolean;
 
	function Image(P: Point) return String is
		("(X="&Image(P.X)&", Y="&Image(P.Y)&")")
		with Inline;
 
	function Image(T: Triangle_T) return String is
		("(A="&Image(T.A)&", B="&Image(T.B)&", C="&Image(T.C)&")")
		with Inline;
end;
 