algorithm – How to determine if a point is in a 2D triangle? – Stack Overflow

If you are looking for speed, hera is a operation that might help you .
Sort the triangle vertices on their ordinates. This takes at worst three comparisons. Let Y0, Y1, Y2 be the three sorted values. By drawing three horizontals through them you partition the airplane into two half planes and two slabs. Let Y be the ordain of the question steer .

if Y < Y1
    if Y <= Y0 -> the point lies in the upper half plane, outside the triangle; you are done
    else Y > Y0 -> the point lies in the upper slab
    if Y >= Y2 -> the point lies in the lower half plane, outside the triangle; you are done
    else Y < Y2 -> the point lies in the lower slab

Costs two more comparisons. As you see, agile rejection is achieved for points outside of the “ jump slab ” .
optionally, you can supply a test on the abscissa for quick rejection on the leftover and on the veracious ( X <= X0' or X >= X2' ). This will implement a quick bounce box test at the same prison term, but you ‘ll need to sort on the abscissa besides.

finally you will need to compute the sign of the given point with regard to the two sides of the triangle that delimit the relevant slab ( upper or lower ). The test has the form :

((X - Xi) * (Y - Yj) > (X - Xi) * (Y - Yj)) == ((X - Xi) * (Y - Yk) > (X - Xi) * (Y - Yk))

The complete discussion of i, j, k combinations ( there are six of them, based on the consequence of the kind ) is out of the setting of this answer and “ left as an exercise to the subscriber ” ; for efficiency, they should be hard-coded .
If you think that this solution is complex, observe that it chiefly involves bare comparisons ( some of which can be precomputed ), plus 6 subtractions and 4 multiplies in case the restrict box test fails. The latter monetary value is hard to beat as in the worst case you can not avoid comparing the test point against two sides ( no method in other answers has a lower cost, some make it worse, like 15 subtractions and 6 multiplies, sometimes divisions ).

update : fast with a shear transform
As explained merely above, you can cursorily locate the point inside one of the four horizontal bands delimited by the three vertex ordinates, using two comparisons .
You can optionally perform one or two extra ten tests to check insideness to the jump box ( scatter lines ).

then consider the “ fleece ” translate given by X'= X - m Y, Y' = Y, where m is the slope DX/DY for the highest edge. This transform will make this side of the triangulum vertical. And since you know on what side of the middle horizontal you are, it suffices to test the sign with obedience to a single side of the triangle .
enter image description here
Assuming you precomputed the gradient m, vitamin a well as the X' for the sheared triangle vertices and the coefficients of the equations of the sides as X = m Y + p, you will need in the worst case

  • two ordinate comparisons for vertical classification;
  • optionally one or two abscissa comparisons for bounding box rejection;
  • computation of X' = X - m Y;
  • one or two comparisons with the abscissas of the sheared triangle;
  • one sign test X >< m' Y + p' against the relevant side of the sheared triangle.
informant :
Category : How To

Related Posts

Leave a Reply

Your email address will not be published.