Check whether Point lies strictly inside the trian

2019-09-22 04:04发布

问题:

Given 3 integral coordinates of vertices of Triangle ABC and another integral coordinate P, how do I check if P strictly lies inside the triangle or not ?

I know how to check if P is inside triangle or not using Area method i.e, Area ABC = Area ABP + Area ACP + Area BCP.
But In this question, I want to P to be strictly inside the triangle.

回答1:

There is lot approaches to check whether point lies inside triangle. The simplest ones, I think:

1) Check whether point is on the same side of all edges (find signs of cross products for edge vectors and vertex-point vectors AB x AP, BC x BP, CA x CP)

2) Represent AP vector in basis of AB and AC vectors - coefficients and their sum should be in range 0..1. Delphi code:

function PtInTriangle(ax, ay, bx, by, cx, cy, px, py: Integer): Boolean;
var
  xb, yb, xc, yc, xp, yp, d: Integer;
  bb, cc, oned: Double;
begin
  Result := False;
  xb := bx - ax;
  yb := by - ay;
  xc := cx - ax;
  yc := cy - ay;
  xp := px - ax;
  yp := py - ay;
  d := xb * yc - yb * xc;
  if d <> 0 then begin
    oned := 1 / d;
    bb := (xp * yc - xc * yp) * oned;
    cc := (xb * yp - xp * yb) * oned;
    Result := (bb > 0) and (cc > 0) and (cc + bb < 1);
  end;
end;