Skip to main content
3 of 10
deleted 6 characters in body
bobobobo
  • 9.8k
  • 21
  • 76
  • 91

A common technique in a computer program is to use barycentric coordinates.

Barycentric coordinates are a lot easier to find than any web resources indicate, so I'm not linking to them.

The easiest way to obtain barycentric coordinates of a point P, given a triangle with vertices described by the vectors A, B, C is likely this method:

$ AreaABC = \frac{ \left| \overline{AB} \times \overline{AC} \right| }{ 2 } $

$ \alpha = \frac{ \left| \overline{PB} \times \overline{PC} \right| }{ 2AreaABC } $

$ \beta = \frac{ \left| \overline{PC} \times \overline{PA} \right| }{ 2AreaABC } $

$ \gamma = 1 - \alpha - \beta $

Here $\alpha$ is the ratio of the area of a subtriangle PBC over the area of the whole triangle ABC, as shown in this image from Peter Shirley's book:

http://i.imgur.com/bMpMa.png

If ALL of $ 0 \le \alpha \le 1, 0 \le \beta \le 1, 0 \le \gamma \le 1 $, then the point P is inside the triangle.

If ANY of $\alpha$,$\beta$,$\gamma$ are outside those ranges, then the point P is not inside the triangle.

Note also when one of $\alpha$,$\beta$,$\gamma$ is 0, and the other 2 coordinates are between 0 and 1, the point P is on an edge of the triangle.

When one of $\alpha$,$\beta$,$\gamma$ is 1 and the other two are 0, then the point P is exactly at a vertex of the triangle.

Of course, these computations assume P is already in the plane of the triangle. If P is not in the plane of the triangle, then you should project it there first, before computing the barycentric coordinates.

bobobobo
  • 9.8k
  • 21
  • 76
  • 91