Find if a point is within a triangle: Difference between revisions

Added XPL0 example
m (→‎{{header|REXX}}: aligned statement.)
(Added XPL0 example)
Line 509:
Triangle is [[0.1, 0.11111111111111], [12.5, 33.333333333333], [-12.5, 16.666666666667]]
Point [5.4142857142857, 14.349206349206] is within triangle ? true
</pre>
 
=={{header|XPL0}}==
<lang XPL0>func real Dot(W,X,Y,Z); \Return the dot product of two 2D vectors
real W,X,Y,Z; \ (W-X) dot (Y-Z)
real WX(2), YZ(2);
[WX(0):= W(0)-X(0); WX(1):= W(1)-X(1);
YZ(0):= Y(0)-Z(0); YZ(1):= Y(1)-Z(1);
return WX(0)*YZ(0) + WX(1)*YZ(1);
];
 
real A,B,C; \triangle
 
func PointInTri(P); \Return 'true' if point P is inside triangle ABC
real P;
int S0,S1,S2; \signs
[S0:= Dot(P,A,B,A) >= 0.0;
S1:= Dot(P,B,C,B) >= 0.0;
S2:= Dot(P,C,A,C) >= 0.0;
return S0=S1 & S1=S2 & S2=S0;
];
 
[A:= [10.5, 6.3]; B:= [13.5, 3.6]; C:= [ 3.3, -1.6];
Text(0, if PointInTri([10.0, 3.0]) then "inside" else "outside"); CrLf(0);
Text(0, if PointInTri([-5.0,-2.2]) then "inside" else "outside"); CrLf(0);
Text(0, if PointInTri([10.5, 6.3]) then "inside" else "outside"); CrLf(0);
]</lang>
 
{{out}}
<pre>
inside
outside
inside
</pre>
772

edits