   closet point on triangle
  i got one algorithm but i don''t anderstand somthingPoint closestPointOnTriangle(Point a, Point b, Point c, Point p){Point Rab = closestPointOnLine(a, b, p);Point Rbc = closestPointOnLine(b, c, p);Point Rca = closestPointOnLine(c, a, p);return closest [Rab, Rbc, Rca] to p;}the problem is the function closet because it doesn''t exist.we know closetpointonlinewe know closetpointontriangle //that what we are doing but what is closet    love and peace  
  Thank you for the sufficiently detailed post and question!You're going about this problem wrong. You can't easily derive the closest point from the closest points to the edges. You have to work with the plane of the triangle first. Here is the proper logic:1) Find the point on the plane containing the triangle a-b-c that is closest the point p. Call this closest point d.2) If d is contained in the interior of the triangle (point-in-polygon test) then the closest point on the triangle ispoint d.3) If d is not contained in the interior of the triangle, the closest point on the triangle to point p is one of the closest points to the edges---calculated using your closestPointOnLine() method. It will be the point that is actually closest to p, so you have to have logic to see which of the closest edge points is also closest to p.So, in pseudo-code,Point closestPointOnTriangle(a, b, d, p){  Point d = closestPointOnPlane(a, b, c, p)  if (true == pointIsInTriangle(a, b, c, d))  {    return(d);  }// else, point is not in triangle and you have to deal// with the edges  Point e = closestPointOnLine(a, b, p);  float closest_distance = lengthOfVector(p - e);// assume e is the closest point for now. it might change as you// walk through the other edges  Point closest_point = e;  e = closestPointOnLine(b, c, p);  float distance = lengthOfVector(p - e);  if (distance < closest_distance)  {    closest_point = e;    closest_distance = distance;  }// finally, check the last edge  e = closestPointOnLine(c, a, p);  distance = lengthOfVector(p - e);  if (distance < closest_distance) closest_point = e;// and we're done!  return(closest_point);}  To find ways of implementing pointIsInTriangle(a, b, c, d), see this page:http://www.acm.org/pubs/tog/editors/erich/ptinpoly/And here is pseudo code for closestPointOnPlane(a, b, c, p), which is derived from code in another current thread.Point closestPointOnPlane(a, b, c, p){// find a normal vector to the plane  Point normal = crossProduct( b - a, c - a);  normal.normalize();// find the vector from point a to the point of interest  Point closest_point = p - a;// find the perpendicular distance from p to the plane, with// a positive distance being in the direction of the normal// to the plane.  float offset = dotProduct(normal, closest_point);// subtract away the portion of (p - a) that is perpendicular// to the plane.  closest_point -= (normal * offset);// add back a to get the closest point in world coordinates  closest_point += a;// we're done!  return(closest_point);}  Graham RhodesSenior ScientistApplied Research Associates, Inc.[edited by - grhodes_at_work on April 26, 2002 5:13:05 PM];  yes it was not my algorithmmmmm it is the logic of the autor see what he saysUntil now, I’ve tried to stick to dealing only with collisions with planes. If ourscene was as simple as a single convex space we could stop here, but fewscenes are. Here’s an example of what you might run into if you only try to collidewith planes rather than polygons, in a non-convex space:Figure 5: Collisions with planes in a concave world just doesn’t workFigure 5 is a clear example of two cases that show the difference betweenpolygon and plane collisions.At some point, we’ll need to know which point inside the polygon is closest to ourintersection point on the plane. Since the polygon and the intersection point bothlie on the plane, this problem may be as simple as determining if our intersectionpoint is within the polygon. If so, then our intersection point is the closest point inthe polygon. However, often times, our intersection point will lie beyond theextents of the polygon (as shown in both cases of figure 5). In this case, theclosest point in the polygon will lie on its perimeter.So, it’s time to find the closest point on a polygon’s perimeter to a point.Figure 6: Finding the closest point on a polygon’s perimeterIn the above example we show the closest point (R) to P. R is always on theperimeter of the polygon. The first step in solving this problem is to determine theclosest point on a line to P. This is performed for each edge of the polygon andthe closest resulting point from each edge is the winner. We call this resultingpoint ‘R’. Here’s an example that shows how to determine the closest point on atriangle’s perimeter to a point on its plane:Point closestPointOnTriangle(Point a, Point b, Point c, Point p){Point Rab = closestPointOnLine(a, b, p);Point Rbc = closestPointOnLine(b, c, p);Point Rca = closestPointOnLine(c, a, p);return closest [Rab, Rbc, Rca] to p;}Point closestPointOnLine(Point a, Point b, Point p){// Determine t (the length of the vector from ‘a’ to ‘p’)Vector c = p - a;Vector V = Normalized vector ;<br>double d = distance from a to b;<br>double t = V * c;<br>// Check to see if ‘t’ is beyond the extents of the line segment<br>if (t < 0) return a;<br>if (t > d) return b;<br>// Return the point between ‘a’ and ‘b’<br>set length of V to t;<br>return a + V;<br>}<br><br>now see what i do<br><br><br>//the function pointproche is a function representing closestpointonline<br>//dist is a distance<br><br><br>function closestpointOnTriangle(a,b,c,p:Tvector):Tvector;<br>var<br>Rab,Rbc,Rca:Tvector;<br>da,db,dc:glfloat;<br>begin<br> Rab:=pointproche(a,b,p);<br> Rbc:=pointproche(b,c,p);<br> Rca:=pointproche(c,a,p);<br> da:=dist(Rab,p);<br> db:=dist(Rbc,p);<br> dc:=dist(Rca,p);<br> if (da<=db) and (da<=dc) then<br> Result:=Rab;<br> if (db<=da) and (db<=dc) then<br> Result:=Rbc;<br> if (dc<=da) and (dc<=db) then<br> Result:=Rca;<br>end;<br><br><br>what do you think about that is taht a good idear will it work??????????????????<br><br><br>sorry i''m a oop coder<br>    <br><br>love and peace    ;  Im not sure exactly what you plan to find, but the functiosn you seem to need i have here for your viewing pleasure These are the way i implemented them....   bool SameSide(Vector p1, Vector p2, Vector a, Vector b){Vector cp1 = Cross((b-a), (p1-a));Vector cp2 = Cross((b-a), (p2-a));if (Dot(cp1, cp2) >= 0)return true;return false;}bool PointInTriangle(Vector p, Vector a, Vector b, Vector c){return (SameSide(p, a, b, c) && SameSide(p, b, a, c) &&SameSide(p, c, a, b));}Vector closestPointOnLine(Vector &vA, Vector &vB, Vector &vPoint){Vector vVector1 = vPoint - vA;    Vector vVector2 = vB - vA;    float d = Magnitude(vVector2);vVector2 = Normalize(vVector2);    float t = Dot(vVector2, vVector1);    if (t < 0.0f) return vA;    if (t > d) return vB;    Vector vVector3 = vVector2 * t;    Vector vClosestPoint = vA + vVector3;return vClosestPoint;}Vector closestPointOnTriangle(Vector a, Vector b, Vector c, Vector p){Vector Rab = closestPointOnLine(a, b, p);Vector Rbc = closestPointOnLine(b, c, p);Vector Rca = closestPointOnLine(c, a, p);double dAB = Magnitude(p-Rab);double dBC = Magnitude(p-Rbc);double dCA = Magnitude(p-Rca);double min = dAB;Vector result = Rab;if (dBC < min){min = dBC;result = Rbc;}if (dCA < min)result = Rca;return (result);}  ;  your the best i thank you a lot for your help.but what do you think about the technik that the autohr use????????was bad??????    love and peace  ;  Thanks rgirard413 for providing a working (albeit slow) point in polygon test! I guess okouya has his complete answer now.okouya, the text from the author of the original method is exactly what my code does---his description is good. I just found the code itself to be difficult to read. Possibly, there is a typo. Or possibly you were using it wrong. It took me less time to rewrite the function than to figure out what was wrong. Don't you find my code much easier to read, and understand?Graham RhodesSenior ScientistApplied Research Associates, Inc.[edited by - grhodes_at_work on April 26, 2002 11:34:11 PM];quote:Original post by okouya  Point closestPointOnTriangle(Point a, Point b, Point c, Point p){Point Rab = closestPointOnLine(a, b, p);Point Rbc = closestPointOnLine(b, c, p);Point Rca = closestPointOnLine(c, a, p);return closest [Rab, Rbc, Rca] to p;}  I now know what the author means by closest [Rab, Rbc, Rca] to p;closest is not a function. It is logic. He just means that you should return one of the points, Rab, Rbc, or Rca----the one which is closest to p.quote:Original post by okouya  function closestpointOnTriangle(a,b,c,p:Tvector):Tvector;varRab,Rbc,Rca:Tvector;da,db,dc:glfloat;begin Rab:=pointproche(a,b,p); Rbc:=pointproche(b,c,p); Rca:=pointproche(c,a,p); da:=dist(Rab,p); db:=dist(Rbc,p); dc:=dist(Rca,p); if (da<=db) and (da<=dc) then Result:=Rab; if (db<=da) and (db<=dc) then Result:=Rbc; if (dc<=da) and (dc<=db) then Result:=Rca;end;  YES!!! Sort of right, sort of wrong. This code will return what the original author means by closest. But it is not guaranteed to be the closest point on the triangle in all cases. If the projection of p into the plane of the triangle lies within the triangle, then this edge stuff will give the wrong answer. The original author describes this situation: "Since the polygon and the intersection point both lie on the plane, this problem may be as simple as determining if our intersection point is within the polygon. If so, then our intersection point is the closest point in the polygon. However, often times, our intersection point will lie beyond the extents of the polygon (as shown in both cases of figure 5). In this case, the closest point in the polygon will lie on its perimeter." The functions you have presented here deal only with the perimeter case---not the interior case.Did you try my code, combined with rgirard413''s point in polygon function? These properly deal with ALL   cases. Plus, as I mentioned before, the code is easier to read.quote:Original post by okouya  sorry i''m a oop coder  Me too, . Graham RhodesSenior ScientistApplied Research Associates, Inc.  