检查点是否严格位于三角形内 [英] Check whether Point lies strictly inside the triangle
问题描述
给出三角形 ABC 的顶点的3个积分坐标和另一个 P 的积分坐标,如何检查 P 是否严格位于三角形内还是不?
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 ?
我知道如何使用Area方法检查 P 是否在三角形内,即Area ABC = Area ABP + Area ACP +区域 BCP .
但是在这个问题上,我希望P严格位于三角形内.
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.
推荐答案
有很多方法可以检查点是否位于三角形内.我认为最简单的方法是:
There is lot approaches to check whether point lies inside triangle. The simplest ones, I think:
1)检查点是否在所有边的同一侧(边向量和顶点点向量AB x AP, BC x BP, CA x CP
的叉积的正负号)
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)用AB和AC向量表示AP向量-系数及其和应在0..1范围内.德尔福代码:
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;
这篇关于检查点是否严格位于三角形内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!