•      Sign In
• Create Account

# Distance from point to polygon plane

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1jocke  Members   -  Reputation: 122

Like
Likes
Like

Posted 18 September 2001 - 10:07 PM

Yesterday i wrote a function that returns the distance between a point and the plane of a polygon. Here it is: float DistanceToPlane(Vertex_ptr p,Plane_ptr plane) { return(DotProduct(p,&plane->N) - plane->d); } Here is what the structures i use look like: typedef struct tagPlane { Vertex N; // Plane normal float d; }Plane,*Plane_ptr; typedef struct tagVertex { float x,y,z; }Vertex,*Vertex_ptr; And here are the cross and dot product functions: void CrossProduct(Vertex_ptr u,Vertex_ptr v,Vertex_ptr result) { result->x = (u->y*v->z - u->z*v->y); result->y = -(u->x*v->z - u->z*v->x); result->z = (u->x*v->y - u->y*v->x); } float DotProduct(Vertex_ptr u,Vertex_ptr v) { return( (u->x * v->x) + (u->y * v->y) + (u->z * v->z)); } Here is how i calculate the plane parameters: Plane wall; Vertex v1; Vertex v2; // Get vector between p2 and p1 VectorSubstract((Vertex_ptr)&p2,(Vertex_ptr)&p1,(Vertex_ptr)&v1); VectorNormalize((Vertex_ptr)&v1); // Get vector between p3 and p1 VectorSubstract((Vertex_ptr)&p3,(Vertex_ptr)&p1,(Vertex_ptr)&v2); VectorNormalize((Vertex_ptr)&v2); // Calculate plane parameters CrossProduct((Vertex_ptr)&v2,(Vertex_ptr)&v1, (Vertex_ptr)&wall.N); VectorNormalize((Vertex_ptr)&wall.N); wall.d = DotProduct((Vertex_ptr)&wall.N,(Vertex_ptr)&p1); The distance function works just fine as long as the polygon i''m testing is standing straight up(like a wall), but when it is sloped the distance is incorrect. To understand this better, here are the points a use: This is the "straight up" version: Vertex p1 = {-1.0f, 1.0f, 0.0f}, p2 = { 1.0f, 1.0f, 0.0f}, p3 = { 1.0f,-1.0f, 0.0f}; This is how it looks when renderd: +--------+ \ | \ | \ | \ | \ | \ | \ | \| + And this is the "sloped" version: Vertex p1 = {-1.0f, 1.0f, -1.0f}, p2 = { 1.0f, 1.0f, -1.0f}, p3 = { 1.0f,-1.0f, 0.0f}; Here i put the Z values of the upper vertices back into the scene to give it a "downhill" slope (rotated around the X axis) and thats when the distance function fails. However, i tried to but the polygon at diffrent angle around the Y axis and it worked out just fine. So the question is why does the distance function give incorrect results when the polygon is rotated around the X axis? Are my Dot and/or Cross functions incorrect? etc. Let me now if none of this doesn''t make any sense or if you need more information. Thanks. PS. I got my information for the DistanceToPlane calculation from the book "Beginning Direct3d game programming", if that means anything.

Sponsor:

### #2Physik  Members   -  Reputation: 122

Like
Likes
Like

Posted 18 September 2001 - 11:44 PM

don''t have much time but this may help you:

plane: ax + by + cz + d = 0
point: u/v/w
distance = k

k = |(a*u + b*v + c*w + d) / (a^2 + b^2 + c^2)^(0.5)|

please send any questions, comments or if it''s even wrong to
___@swissonline.ch

### #3browny  Members   -  Reputation: 139

Like
Likes
Like

Posted 19 September 2001 - 12:41 AM

first of all i hope u know that the distance calculated is always the pependicular distance from that point
to the plane. I found lot of peoples making mistakes at this point. I hope u dont fall into that group

well.. the function
float DistanceToPlane(Vertex_ptr p,Plane_ptr plane)
{
return(DotProduct(p,&plane->N) - plane->d);
}
will return correct value ONLY IF the normal of the plane is Normalized. Otherwise it will return incorrect values.
So in order make sure the function always return correct values use the following code

float DistanceToPlane(Vertex_ptr p,Plane_ptr plane)
{
return( (DotProduct(p,&plane->N)/vectorNormalize(&plane->N) ) - plane->d);
}

If it still doens''t solve ur problem then plz check the CrossProduct function once again. ( sorry i dont
have the time.... )

The DotProduct function seems to be alright to me

browny

### #4 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 19 September 2001 - 01:31 AM

Thanx for the help guys, but i will not be able to try your
advice right now because i''m in a bit of a hurry.

To browny:

I''m pretty sure that by CrossProduct is correct because
i draw a line for the center of the world (0,0,0) to the
normal vector, and it''s always perpendicular to the triangle.

Now a question about the modified version of the distance
function you wrote:

float DistanceToPlane(Vertex_ptr p,Plane_ptr plane)
{
return( (DotProduct(p,&plane->N)/vectorNormalize(&plane->N) ) -plane->d);
}

Just to be sure i assume that the "vectorNormalize()" is
supposed to return the length of the normal vector, but i''m
not sure. Please correct me if i''m wrong.

### #5 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 19 September 2001 - 02:32 AM

VectorNormalaize() will return the normalaized vector of the
input vector (a vector with a lenght of 1)

### #6kudi  Members   -  Reputation: 158

Like
Likes
Like

Posted 20 September 2001 - 09:50 AM

Hallo,

das isch ja schön, dass es sogar i dem forum no än schwiizer hät.
hät ich nöd erwartet.

kudi

### #7jocke  Members   -  Reputation: 122

Like
Likes
Like

Posted 21 September 2001 - 01:57 AM

You wana repeat that in english please

### #8kudi  Members   -  Reputation: 158

Like
Likes
Like

Posted 21 September 2001 - 03:29 AM

Sorry jocke,

I''ve realized that Physik is also from Switzerland and that''s our language.

kudi

### #9jocke  Members   -  Reputation: 122

Like
Likes
Like

Posted 21 September 2001 - 05:38 AM

Oh,ok then.

Anyway, do you have an answer to my question, or do you
know a site that has the answer?

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS