Point in Triangle

Triangle bounds test

A simple method for dermining if a 2D coordinate, x,y falls inside of the bounds of the triangle described by the points p1, p2, p3.

View example

function pointInTriangle(x, y, p1, p2, p3)
{
	var alpha = ((p2[1] - p3[1]) * (x - p3[0]) + (p3[0] - p2[0]) * (y - p3[1])) /
		((p2[1] - p3[1]) * (p1[0] - p3[0]) + (p3[0] - p2[0]) * (p1[1] - p3[1]));
	var beta = ((p3[1] - p1[1]) * (x - p3[0]) + (p1[0] - p3[0]) * (y - p3[1])) /
		((p2[1] - p3[1]) * (p1[0] - p3[0]) + (p3[0] - p2[0]) * (p1[1] - p3[1]));
	var gamma = 1 - alpha - beta;

	return (alpha>0 && beta>0 && gamma>0);
}

Page loaded in 0.011 second(s).