Distance of point to triangle

Zermelo

Junior Member
Joined
Jan 7, 2021
Messages
148
Hello, let there be a triangle ABC in the Cartesian plane - A(x1, y1), B(x2, y2), C(x3, y3) are the vertices - and let's consider a point [math]X (x_0, y_0)[/math] outside of the triangle. I want to find the minimum distance from point X to the triangle ABC. My thoughts are: let's measure the distance of point X to the vertices A, B, C, and then measure the distance of X to each of the triangle's sides. Then the distance is the minimum of these measures. But my problem is, how to determine the distance from the triangle's sides?
I can easily compute the distance of the point X to a line connecting points A, B, aka it's the distance of X to the projection of X to the line. But the projected point doesn't have to be on the triangle's side, that's what I can't solve. Is there any way to check if the projected point is indeed on a triangle's side? My first hunch is checking if the projected point (xp, yp) satisfies [math]min\{x_1, x_2, x_3\} < x_p < max\{x_1, x_2, x_3\} \land min\{y_1, y_2, y_3\} < y_p < max\{y_1, y_2, y_3\}[/math]. Is there any other way of checking this? Or even better, is there a whole other way of solving this problem? Keep in mind that I have to program this algorithm so it works for any triangle, so sketching it out is not a viable solution. I had an idea of seperating the outside of the triangle to 6 regions, and using the distance to lines or vertices depending on which region the point X belongs to, but this seems like a lot harder thing to do abstractly, for every possible triangle.
 
Hello, let there be a triangle ABC in the Cartesian plane - A(x1, y1), B(x2, y2), C(x3, y3) are the vertices - and let's consider a point [math]X (x_0, y_0)[/math] outside of the triangle. I want to find the minimum distance from point X to the triangle ABC. My thoughts are: let's measure the distance of point X to the vertices A, B, C, and then measure the distance of X to each of the triangle's sides. Then the distance is the minimum of these measures. But my problem is, how to determine the distance from the triangle's sides?
I can easily compute the distance of the point X to a line connecting points A, B, aka it's the distance of X to the projection of X to the line. But the projected point doesn't have to be on the triangle's side, that's what I can't solve. Is there any way to check if the projected point is indeed on a triangle's side? My first hunch is checking if the projected point (xp, yp) satisfies [math]min\{x_1, x_2, x_3\} < x_p < max\{x_1, x_2, x_3\} \land min\{y_1, y_2, y_3\} < y_p < max\{y_1, y_2, y_3\}[/math]. Is there any other way of checking this? Or even better, is there a whole other way of solving this problem? Keep in mind that I have to program this algorithm so it works for any triangle, so sketching it out is not a viable solution. I had an idea of seperating the outside of the triangle to 6 regions, and using the distance to lines or vertices depending on which region the point X belongs to, but this seems like a lot harder thing to do abstractly, for every possible triangle.
Have you thought about using vectors in this problem?
 
Have you thought about using vectors in this problem?
Not really, I’m used to dealing only with vectors that originate from the coordinate beginning (0, 0). Even if I fixed one of the vertices to (0, 0), I don’t know how a vector would represent the opposing side. If I figured this out, the desired length would be the minimum of the projection lengths of the point X to each of the vectors! Could you give me some more hints please?
 
Not really, I’m used to dealing only with vectors that originate from the coordinate beginning (0, 0). Even if I fixed one of the vertices to (0, 0), I don’t know how a vector would represent the opposing side. If I figured this out, the desired length would be the minimum of the projection lengths of the point X to each of the vectors! Could you give me some more hints please?
The magnitude of the Cross-Product of the vectors will provide you with the length yo seek.

If A and B are two vectors, then:

|A X B| = |A|*|B|*sin(Φ) ........ where Φ is the angle between the two vectors when placed tail-to-tail.
 
Top