11-20-2023, 10:20 AM
For such general 3D graphics problems, it is best to search the internet for the anwser.
For example, I found an answer to your question on stackoverflow (https://stackoverflow.com/questions/2316...ne-segment) - on answer mentioned the code on https://bloodstrawberry.tistory.com/1037
I have updated the code to work with WPF 3D objects (Point3D and Vector3D):
For example, I found an answer to your question on stackoverflow (https://stackoverflow.com/questions/2316...ne-segment) - on answer mentioned the code on https://bloodstrawberry.tistory.com/1037
I have updated the code to work with WPF 3D objects (Point3D and Vector3D):
Code:
public double GetDistanceTwoLine(Point3D A, Point3D B, Point3D C, Point3D D)
{
Vector3D AB = A - B;
Vector3D CD = C - D;
Vector3D AC = A - C;
Vector3D line = Vector3D.CrossProduct(AB, CD);
return Math.Abs(Vector3D.DotProduct(AC, line)) / line.Length;
}
public Point3D GetContactPoint(Vector3D normal, Point3D planeDot, Point3D A, Point3D B)
{
Vector3D nAB = B - A;
nAB.Normalize();
return A + nAB * Vector3D.DotProduct(normal, planeDot - A) / Vector3D.DotProduct(normal, nAB);
}
public (Point3D point1, Point3D point2) GetShortestPath(Point3D A, Point3D B, Point3D C, Point3D D)
{
Vector3D AB = A - B;
Vector3D CD = C - D;
Vector3D line = Vector3D.CrossProduct(AB, CD);
Vector3D crossLineAB = Vector3D.CrossProduct(line, AB);
Vector3D crossLineCD = Vector3D.CrossProduct(line, CD);
return (GetContactPoint(crossLineAB, A, C, D), GetContactPoint(crossLineCD, C, A, B));
}
Andrej Benedik

