28 Apr 2021

How to test if a point is inside of a triangle?

收藏到CSDN网摘

To determine if a point P is inside or outside a given triangle defined by 3 vertices P1, P2, and P3. The edges can be calculated first as 3 vectors: P12 = P1-P2; P23 = P2-P3; P31 = P3-P1. Then, its a simple task.
% check if a point is within a triangle regions [p1,p2,p3]
withinTriangle = @(P,P1,P2,P3,P12,P23,P31) sign(det([P31;P23]))*sign(det([P3-P;P23])) >= 0 & ...
   sign(det([P12;P31]))*sign(det([P1-P;P31])) >= 0 & ...
   sign(det([P23;P12]))*sign(det([P2-P;P12])) >= 0 ;

No comments :

Post a Comment