R3.Geometry.Euclidean3D.ProjectOntoLine C# (CSharp) Method

ProjectOntoLine() public static method

public static ProjectOntoLine ( Vector3D nl, Vector3D pl, Vector3D point ) : Vector3D
nl Vector3D
pl Vector3D
point Vector3D
return Vector3D
        public static Vector3D ProjectOntoLine( Vector3D nl, Vector3D pl, Vector3D point )
        {
            // http://gamedev.stackexchange.com/a/72529
            // A + dot(AP,AB) / dot(AB,AB) * AB
            Vector3D AP = point - pl;
            Vector3D AB = nl;
            return pl + AB * AP.Dot( AB ) / AB.Dot( AB );
        }

Usage Example

Example #1
0
 public static void TranslateSphere(Sphere s, Vector3D t)
 {
     if (s.IsPlane)
     {
         Vector3D offsetAlongNormal = Euclidean3D.ProjectOntoLine(s.Normal, new Vector3D(), t);
         s.Offset += offsetAlongNormal;
     }
     else
     {
         s.Center += t;
     }
 }