PDA

View Full Version : point to a polygon


Cheesemonk13
02-10-2003, 08:15 PM
How can I find the closest distance from a point to a polygon?

Dia Kharrat
02-10-2003, 08:16 PM
Try this article:

http://www-compsci.swan.ac.uk/~csmark/PDFS/dist.pdf

dabeav
02-11-2003, 07:44 PM
This might be a start, you must first find the distance to the plane containing the poly. Like so.

//NOTE:
/*
both the vectorstruct and the pointstruct are float X,Y,Z; values, to make things simplier
N1is the polys normal
POP is ANY point on that plane, AKA one of the 3 verts of the triangle poly
P1, is the point checking the distance from
*/

float PointPlaneDistance(VectorStruct N1, PointStruct POP, PointStruct P1)
{
float Distance;

Distance = ((N1.X*P1.X + N1.Y*P1.Y + N1.Z*P1.Z + (-N1.X*POP.X - N1.Y*POP.Y - N1.Z*POP.Z)));

return Distance;
}

That will return the distance to the plane (if negative you are behind the plane)
If you need to find the distance to a poly, that is not a perpendicular plane line, you would have to find the distance to each of the lines, and see which is closest, why? Because if you arent closest to the face (Plane distance) then you have to be off to the side of one of the lines, thus the perpendicular to one of those lines is the closest point. Got it?

//This returns the closest point on the line (which of the lines of the triangle you pass) along with the
// distance in a pass by value.
NOTE:
/*
SP1 is segment end point one, sp2 is segment end point 2, p1 is the point checking from, and distance is self explanitory
POI is point of impact, or the closest point on the line
Build vector simply creates a NON normalized vector
dot vector, is self explanitory
Step point along vector does exactly what it says, it moves the point along the vector passes, along with the distance passed, and returns the new point.
*/

PointStruct ClosestLineSegmentPoint(PointStruct SP1, PointStruct SP2, PointStruct P1, float& Distance)
{
//This directly returns a point, and also pass by returns the distance.
VectorStruct Vec[4];
PointStruct POI;
float c1, c2, c3;

Vec[0] = BuildVector(SP2, SP1);
Vec[1] = BuildVector(P1, SP1);
Vec[2] = BuildVector(P1, SP2);

//Dot product of the first 2 vectors
c1 = DotVectors(Vec[0], Vec[1]);

if(c1<=0)
{
Distance = sqrt(DotVectors(Vec[1], Vec[1]));
POI = SP1;
}

else
{
//Dot product of the next 2 vectors
c2 = DotVectors(Vec[0], Vec[0]);

if(c2<=c1)
{
Distance = sqrt(DotVectors(Vec[2], Vec[2]));
POI = SP2;
}

else
{
c3 = c1/c2;

//Holding variable of new point
POI = StepPointAlongVector(SP1, Vec[0], c3);
Vec[3] = BuildVector(P1, POI);

Distance = DotVectors(Vec[3], Vec[3]);
}
}

return POI;
}


Hope this helps, sorry for the quick post, but off to more coding.