question:
have two lines/rays in 3d and want to get the shortest line that connects both (or similarly: the 3d point that has the shortest distance to both lines)
code :
// get plane p1 that contains l1 and is parallel to l2
Plane_3 p1( l1, l1.point(0) + l2.to_vector() );
// get plane p2 that contains l1 and is perpendicular to p1
Plane_3 p2( l1, l1.point(0) + p1.orthogonal_vector() );
// get intersection i1 between p2 and l2
Point_3 i1;
CGAL::Object result = CGAL::intersection( p2, l2 );
if( !assign( i1, result ) ) {
Line_3 il;
if( assign( il, result ) )
std::cout << "intersection between plane and line is a line --> l1 and l2 are parallel!" << std::endl;
return -1.0;
}
// get intersection i2 on l1
Point_3 i2 = l1.projection( i1 );
// final point is (i1+i2)/2
Point_3 f((i1.x() + i2.x()) / 2.0, (i1.y() + i2.y()) / 2.0, (i1.z() + i2.z()) / 2.0);