check points inside triangle or on edge with example

Good evening everyone
function or coding for finding point is inside triangle or sub triangle or its on edges
thanks for involving your knowledge to be share to answer question

7 件のコメント

John D'Errico
John D'Errico 2016 年 4 月 8 日
編集済み: John D'Errico 2016 年 4 月 8 日
help inpolygon
If this is homework (as I expect from your statement) then why not make an effort? We are not here to provide detailed code for your every homework problem.
Redwan Dan
Redwan Dan 2016 年 4 月 8 日
編集済み: Walter Roberson 2016 年 4 月 9 日
i check that one too, is not assignment and i check isinterior function too but doesn't do what i want ?
a = [17 14 2 15 16 12 17 8 12 10; 13 1 12 12 9 6 14 8 9 10 ];
p0=[a(1,p),a(2,q)]
scatter(xmax(1)+5,ylow-10,'*')
px = (xmax(1)+5)
py =(ylow-10)
p1= [px,py]
scatter(xlow-10,ymax,'*')
ppx = (xlow-10)
ppy = ymax
p2=[ppx,ppy]
px0 = a(1,p)
py0=a(2,q)
x= plot([ px ppx ],[py ppy])
y= plot([px0 px],[py0 py])
z= plot([px0 ppx],[py0 ppy])
T1=[x y z]
T= [T1]
xv=(px0+px+ppx)/3
yv=(py0+py+ppy)/3
xq=a(1,1)
yq=a(2,1)
[in,on] = inpolygon(xq,yq,xv,yv)
numel(xq(in))
numel(xq(on))
John D'Errico
John D'Errico 2016 年 4 月 8 日
編集済み: John D'Errico 2016 年 4 月 8 日
You don't actually say what it is that you expect to happen. Anyway, your code does not run at all, since it is missing the values of p, q, xmax, and ylow to start with. Possibly others, but I've not bothered to look past the first few lines. We cannot decipher uncommented code that will not even execute.
So why did inpolygon NOT suffice? Your question is apparently about a triangle, given the title, yet it is not at all obvious from the code what you are doing. If you want an answer, you need to provide some explanation.
Roger Stafford
Roger Stafford 2016 年 4 月 8 日
編集済み: Roger Stafford 2016 年 4 月 8 日
Even if 'inpolygon' is not used for some reason, checking that a point lies inside a triangle is very easily done. It requires three inequalities to be satisfied. Each one checks that a corresponding vertex lies on the same side of the line of the other two vertices as the given point. This is a comparison between two linear expressions. The point lies inside the triangle if and only if all three are true.
Redwan Dan
Redwan Dan 2016 年 4 月 9 日
Dear Roger
you mean by this check sides with dot product method but i think this method is slow to process what do you think about barycentric coordinate system method isn't better than previous one ?
Roger Stafford
Roger Stafford 2016 年 4 月 9 日
I've given the necessary expression as an "Answer" here. It doesn't seem like a very slow method to me.
Walter Roberson
Walter Roberson 2016 年 4 月 9 日
Redwan Dan comments to John D'Errico:
am not here to prove you any thing and the way your answer and close is not nice treat with me if you don't know just don't comment or close

サインインしてコメントする。

 採用された回答

Roger Stafford
Roger Stafford 2016 年 4 月 9 日

2 投票

Suppose P1 = [x1,y1], P2 = [x2,y2], and P3 = [x3,y3] are row vectors giving the coordinates of the three vertices of a triangle, and P = [x,y] is a row vector for the coordinates of a point P. To determine whether P lies inside the triangle P1P2P3 do this:
P12 = P1-P2; P23 = P2-P3; P31 = P3-P1;
t = 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 ;
Point P lies within the triangle if and only if t is true.

5 件のコメント

Roger Stafford
Roger Stafford 2016 年 4 月 9 日
The following is simplified from the above:
s = det([P1-P2;P3-P1]);
t = s*det([P3-P;P2-P3])>=0 & s*det([P1-P;P3-P1])>=0 & s*det([P2-P;P1-P2])>=0;
Again point P lies within the triangle P1P2P3 if and only if t is true.
Roger Stafford
Roger Stafford 2016 年 4 月 9 日
You mentioned barycentric coordinates. It may interest you to know that the previous computations are closely related to barycentric coordinates:
s = det([P1-P2;P3-P1]);
w1 = det([P3-P;P2-P3])/s;
w2 = det([P1-P;P3-P1])/s
w3 = det([P2-P;P1-P2])/s;
The numbers w1, w2, and w3 are the barycentric coordinate of P and the equation
P = w1*P1 + w2*P2 + w3*P3
holds true (within round-off accuracy of course.)
Redwan Dan
Redwan Dan 2016 年 4 月 10 日
編集済み: Redwan Dan 2016 年 4 月 10 日
thanks for both of you to involve and share your knowledge ..
T A
T A 2018 年 11 月 26 日
編集済み: T A 2019 年 8 月 7 日
UPDATED FOR CLARITY
With regard to assessing whether a point is on an edge/edges using the conditional statements given in Roger's answer,
  • P is on a triangle edge when one of the three conditional statements is zero
  • P is on a triangle vertex when two of the three conditional statements are zero
With regard to computational efficiency, the process can be costly if you're searching across many triangles. In such a case, the easiest thing to do is to only perform the calculations when P is within the bounding box of the current triangle:
Ptri=[P1;P2;P3];
if P(1)<=max(Ptri(:,1))&&P(1)>=min(Ptri(:,1))...
&&P(2)<=max(Ptri(:,2))&&P(2)>=min(Ptri(:,2))
%do the suggested calculations...
end
Evaluating this conditional statement is very, very cheap.
Muhamad Amirulfaris Abdullah
Muhamad Amirulfaris Abdullah 2019 年 7 月 6 日
Hi there. what was the conditional statement that you were reffering to?

サインインしてコメントする。

その他の回答 (1 件)

Gary Bikini
Gary Bikini 2021 年 6 月 12 日

1 投票

You can use the built-in function
[in,on]=inpolygon(xq,yq,xv,yv)

カテゴリ

タグ

タグが未入力です。

質問済み:

2016 年 4 月 8 日

回答済み:

2021 年 6 月 12 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by