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

Content added Content deleted
(Fortran implementation using convex hull algorithm)
(J)
Line 1,113: Line 1,113:
·········+···········
·········+···········
·····················</pre>
·····················</pre>

=={{header|J}}==
Implementation, using complex numbers to represent x,y coordinates:
<lang J>area=: [:| 0.5-/ .*@,.+. NB. signed area of triangle
I3=: =i.3 NB. identity matrix
inside=: {{ (area y)=+/area"1|:(I3*x)+(1-I3)*y }}</lang>

This is based on the algorithm documented for the ada implementation: compute the area of triangles using the determinant method (we want the absolute area here), and check whether the triangles formed with the test point and the sides of the test triangle matches the area of the test triangle.

Examples:<lang J> 0j0 inside 1.5j2.4 5.1j_3.1 _3.8j1.2
1
0j1 inside 1.5j2.4 5.1j_3.1 _3.8j1.2
1
3j1 inside 1.5j2.4 5.1j_3.1 _3.8j1.2
0
5.414285714285714j14.349206349206348 inside 0.1j1r9 12.5j100r3 25j100r9
1
5.414285714285714j14.349206349206348 inside 0.1j1r9 12.5j100r3 _12.5j100r6
1</lang>


=={{header|Java}}==
=={{header|Java}}==