The shortest distance of two line segments
Float distancelinetoline (const OSG: vec3d & P1, const OSG: vec3d & p2, const OSG: vec3d & P3, const OSG: vec3d & P4) {float distance; float X1 = p1.x (); // a coordinate (x1, Y1, Z1) float Y1 = p1.y (); float z1 = p1.z (); float X2 = p2.x (); // coordinate of point B (X2, Y2, Z2) float y2 = p2.y (); float Z2 = p2.z (); float X3 = p3.x (); // coordinate of point C (X3, y3, Z3) float Y3 = p3.y (); float Z3 = p3.z (); float X4 = p4.x (); // D coordinate (X4, Y4, Z4) float Y4 = p4.y (); float z4 = P4.z (); float a = (x2-x1) * (x2-x1) + (y2-y1) * (y2-y1) + (z2-z1) * (z2-z1); float B =-(x2-x1) * (x4-x3) + (y2-y1) * (y4-y3) + (z2-z1) * (z4-z3); float c =-(x1-x2) * (x1-x3) + (y1-y2) * (y1-y3) + (Z1-Z2) * (z1-z3); float d =-(x2-x1) * (x4-x3) + (y2-y1) * (y4-y3) + (z2-z1) * (z4-z3); float E = (x4-x3) * (x4-x3) + (y4-y3) * (y4-y3) + (z4-z3) * (z4-z3 ); float F =-(x1-x3) * (x4-x3) + (y1-y3) * (y4-y3) + (z1-z3) * (z4-z3 )); if (A * E-B * d) = 0 & (B * D-A * E) = 0) // Parallel {float d1 = (p1-p3). Length (); float D2 = (p1-p4). Length (); distance = (d1 <D2 )? D1: D2; return distance;} float S = (B * F-E * C)/(A * E-B * D ); float t = (A * F-D * C)/(B * D-A * E ); if (0 <= S & S <= 1 & 0 <= T & T <= 1) // It indicates that the point P falls on the line AB, Q points fall on the vertical segment pq of the {// 2 line segments on the Line Segment CD; // point coordinate float x = X1 + S * (x2-x1 ); float y = Y1 + S * (y2-y1); float z = Z1 + S * (z2-z1); // qpoint coordinate float u = X3 + T * (x4-x3 ); float v = Y3 + T * (y4-y3); float W = Z3 + T * (z4-z3); OSG: vec3d p (x, y, z); OSG :: vec3d Q (U, V, W); distance = (P-Q ). length ();} else {float d1 = distancepointtoline (P3, P4, P1); float D2 = distancepointtoline (P3, P4, P2); float D3 = distancepointtoline (P1, P2, P3); float D4 = distancepointtoline (P1, P2, p4); distance = (d1 <D2 )? D1: D2; distance = (distance <D3 )? Distance: D3; distance = (distance <D4 )? Distance: D4;} return distance ;}
The shortest distance from a point to a line segment in a space
Float distancepointtoline (const OSG: vec3d & star, const OSG: vec3d & End, const OSG: vec3d & Center) {float distance; float X0 = center. X (); // point coordinate (x0, y0, z0) float Y0 = center. Y (); float z0 = center. Z (); float X1 = Star. X (); // a coordinate (x1, Y1, Z1) float Y1 = Star. Y (); float z1 = Star. Z (); float X2 = end. X (); // coordinate of point B (X2, Y2, Z2) float y2 = end. Y (); float Z2 = end. Z (); float t = (x1-x0) * (x1-x2) + (y1-y0) * (y1-y2) + (z1-z0) * (Z1-Z2)/(x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) + (Z1-Z2) * (Z1-Z2); If (0 <= T & T <= 1) // The qpoint of the vertical foot falls on the line AB {float x = X1 + T * (x2-x1); float y = Y1 + T * (y2-y1 ); float z = Z1 + T * (z2-z1); OSG: vec3d Q (x, y, z); distance = (Q-center ). length () ;}if (T <0) // If the qpoint falls on the AB of the online segment, it falls on the extended line of BA {distance = (Star-center ). length ();} If (T> 1) // If the QPS falls on the AB extension line instead of the online AB segment. {distance = (end-center ). length ();} return distance ;}
Mathematical formula. I uploaded it directly! Address: http://download.csdn.net/detail/liying426/5058179