A function in PHP to check if a point is in triangle.
function fAB($t, $x, $y) {
return ($y - $t['y1'])*($t['x2'] - $t['x1'])
-($x - $t['x1'])*($t['y2'] - $t['y1']);
}
function fBC($t, $x, $y) {
return ($y-$t['y2'])*($t['x3']-$t['x2'])
-($x-$t['x2'])*($t['y3']-$t['y2']);
}
function fCA($t, $x, $y) {
return ($y-$t['y3'])*($t['x1']-$t['x3'])
-($x-$t['x3'])*($t['y1']-$t['y3']);
}
function CheckPointInside($t, $x, $y) {
if (fAB($t, $x, $y)*fBC($t, $x, $y)>0
&& fBC($t, $x, $y)*fCA($t, $x, $y)>0) {
return true;
} else {
return false;
}
}
Function CheckPointInside expects three parameters: $t an associative array of triangle’s coordinates written as x1,y1, x2, y2, x3, y3 and $x, $y, the point’s coordinates.