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 Commenti

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.
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 il 8 Apr 2016
Modificato: John D'Errico il 8 Apr 2016
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.
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.
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 ?
I've given the necessary expression as an "Answer" here. It doesn't seem like a very slow method to me.
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

Accedi per commentare.

 Risposta accettata

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 Commenti

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.
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 il 10 Apr 2016
Modificato: Redwan Dan il 10 Apr 2016
thanks for both of you to involve and share your knowledge ..
T A
T A il 26 Nov 2018
Modificato: T A il 7 Ago 2019
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.
Hi there. what was the conditional statement that you were reffering to?

Accedi per commentare.

Più risposte (1)

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

Tag

Non è stata ancora inserito alcun tag.

Community Treasure Hunt

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

Start Hunting!

Translated by