POV-Ray : Newsgroups : povray.programming : Explanation of point inside a triangle code : Re: Explanation of point inside a triangle code Server Time
24 Apr 2024 04:36:22 EDT (-0400)
  Re: Explanation of point inside a triangle code  
From: clipka
Date: 6 Apr 2009 15:15:00
Message: <web.49da53e8b10008142b82b7c80@news.povray.org>
"queercore" <nomail@nomail> wrote:
> Thank you very much for your reply. I understood the projection part but can you
> please tell me what is the 2D linear math involved in finding whether the point
> is inside or offside of the triangle edges.

Okay, let's look at this in detail:

       if ((Triangle->P2[Y] - s) * (Triangle->P2[Z] - Triangle->P1[Z]) <
           (Triangle->P2[Z] - t) * (Triangle->P2[Y] - Triangle->P1[Y]))
       {
         return(false);
       }

It becomes clearer if we flip the signs:

       if ((s - Triangle->P2[Y]) * (Triangle->P1[Z] - Triangle->P2[Z]) <
           (t - Triangle->P2[Z]) * (Triangle->P1[Y] - Triangle->P2[Y]))
       {
         return(false);
       }

Note that all the differences in there actually constitute a translation by -P2,
so we get a statement of the form:

    if (A[Y] * B[Z] < A[Z] * B[Y]) ...

which is a more robust way of comparing A[Y]/A[Z] with B[Y]/B[Z], which gets
unnecessarily nasty in certain cases (and, as it seems, would be more
problematic regarding the signs, and choice of > vs. <).

So in essence the idea is to compare the "direction coefficients" (don't know if
that's the proper English word) of the line equations of (A) the line through P2
and the intersection point, and (B) the line through P2 and P1.

You can also think of it in terms of a vector dot product:

    if (A[Y] * B[Z] < A[Z] * B[Y]) ...

is equal to:

    if (A[Y] * B[Z] + A[Z] * (-B[Y]) < 0) ...

which happens to be the dot product of A and a vector perpendicular to B
(compared with zero).

Interpreted this way, the test boils down to checking whether the angle between
A, and that particular perpendicular to B, is greater or smaller than 90
degrees.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.