# Find if a point is inside a triangle using weighted averages

I have been watching this series on khancademy and in it they try to explain how to find out if a ray intersects a triangle. They do a good job explaining how to find the point of intersection of the plane of the triangle but they do an incomplete job of explaining how after that checking if the point is in the triangle is done.

They explain that you can use weighted averages to calculate this by first calculating them and then checking if it's negative or not but in the explanation in the exercises they only pick points of triangles that can exactly be added or subtracted from each other to cancel out. thus being useless for any other situation (which i need because i'm trying to make a ray tracer).

So i want to know how can you calculate the weights of the points that together make up the weighted average

For instance you have 3 points A, B and C each with their xyz value and you have an intersection point I. What do the weights of A,B and C have to be to get point I.

exercises https://www.khanacademy.org/partner-content/pixar/rendering/rendering-2/e/triangle-intersection-3d (notice how in these exercises they pick points that are always able to cancel each other out which makes their method in the exercises useless when trying to make a ray tracer)

## 1 Answer

I think your question has already been answered here.

But, if not, the basic idea is that the "weights" (also known as "barycentric coordinates") are areas of certain triangles. When you say they "cancel out", maybe you mean that they add up to $1$. This will always be the case -- if you take any point in the plane of the triangle, the sum of its barycentric coordinates will be equal to $1$. This is a mathematical fact; it's not because the Kahn folks chose some special unrealistic examples.

There are ways to do ray-triangle intersection directly, without computing and checking ray-plane intersections. See this page, which includes the necessary code, and this page, which describes the whole process in great detail.

• these barycentric coordinates indeed seem to be what i'm looking for altough i dont quite yet know how to get the weights yet from that other question. i'l look into those barycentric coordinates and hope that i can use that to see if a point is in a triangle Dec 30 '15 at 15:06
• Barycentric coords and formulae for calculating them are given here: en.wikipedia.org/wiki/Barycentric_coordinate_system. Section 2.4 shows how their values can be used to determine whether or not a point lies in a triangle. Dec 31 '15 at 2:08