简体   繁体   中英

Finding the distance of a point from a triangle in 3d space

Imagine the surface of a triangle in cartesian space. How can I find the distance of a given point from the surface of that triangle?

Point 1: [ 30, 24, 22 ]
Point 2: [ 35, 13, 19 ]
Point 3: [ 21, 29, 85 ]

In this case, how far is the point [ 40, 25, 77 ] from the surface of the triangle defined above?

Or specifically - what would the formula be to determine that distance?

It all depends on where abouts you want the distance to come from (Which part of the triangle?). If you know the point of the triangle to start from (centre maybe?) then, from a maths perspective, the formula is:

Point 1: [a1,b1,c1] (point on triangle)
Point 2: [a2,b2,c2] (point to find distance to)

distance vector = [a2-a1, b2-b1, c2-c1]
distance = the magnitude of above = sqrt((a2-a1)^2 + (b2-b1)^2 + (c2-c1)^2))

I don't know C#, but this would be the pseudo code for it (the maths behind it)

Edit : To find centre of triangle...

Point 1: [x1, y1, z1]
Point 2: [x2, y2, z2]
Point 3: [x3, y3, z3]
Centre = [(x1+x2+x3)/3, (y1+y2+y3)/3, (z1+z2+z3)/3]

I will assume you mean minimal distance. Otherwise, the distance changes depending on which point on the triangle you pick. To see that, just draw a triangle on your table and hold a pen above it.

The short answer is, to find the minimal distance, you need to build a matrix with one point per column, like so:

    [ 30 35 21 ]
A = [ 24 13 29 ]
    [ 22 19 85 ]

And read about projections for the details. If it has been a while since you studied this kind of stuff, don't be intimidated. It isn't rocket science (actually, maybe it is in intro to rocket science), but it will take some investment to understand. To help you along, I'll mention a bit about the theory first, and then talk about implementation.

To give you some intuition, hold on to that pen.

Lets start with the easiest case: To get the minimal distance, hold the pen exactly perpendicular to the table, with the end of the pen in the triangle somewhere. Picture your point as the tip of the pen. You just proved that the point in the triangle closest to your point is the projection of that point onto the space defined by the three points in the triangle. In other words, the other end of the pen. Any other point in the triangle must have distance longer then the pen length.

Now lets look at a complication. Suppose that the point you want is not exactly "above" the triangle, but rather to the side somewhere. Say, off of the surface of the table altogether. In this case, the point in the triangle closest to the point will either be one of three points themselves, or a point on the three line segments connecting the three initial points.

Now for the implementation: the skinny is, if you can use a simple library for linear algebra, you will save yourself a lot of grief. The hardest step is inverting the matrix, and while inverting 3x3 matrices is usually easy, it can get complicated when points are co-linear (ie, think about three points that are aligned), when magnitudes are very different (ie, think of a very long skinny triangle), etc.

You could write a problem-specific algorithm for this problem, given that it is only 3x3, but at the end of the day you will need to solve a projection system, so your algorithm will be doing essentially the same math that is on the Wikipedia page. So you may end up rediscovering linear algebra, which is a sure way to waste A LOT of time.

My last implementation suggestion is to check the keyword "projection" in gaming or 3D libraries. If you are lucky there is a function you can just call. Otherwise get some decent CLR linear algebra library, build the matrix, and do the entire computation efficiently and in 100 lines of code or so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM