Nieb_

Vector Projection

May 17th, 2021
787
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4. FUNCTION prj3(Point REF AS Vec3, LinePointA REF AS Vec3, LinePointB REF AS Vec3) // "Projection" Get ClosestPoint on Line from Point.
  5.     DeltaAP_X AS FLOAT : DeltaAP_X = Point.x - LinePointA.x
  6.     DeltaAP_Y AS FLOAT : DeltaAP_Y = Point.y - LinePointA.y
  7.     DeltaAP_Z AS FLOAT : DeltaAP_Z = Point.z - LinePointA.z
  8.  
  9.     DeltaAB_X AS FLOAT : DeltaAB_X = LinePointB.x - LinePointA.x
  10.     DeltaAB_Y AS FLOAT : DeltaAB_Y = LinePointB.y - LinePointA.y
  11.     DeltaAB_Z AS FLOAT : DeltaAB_Z = LinePointB.z - LinePointA.z
  12.  
  13.     DotAP_AB AS FLOAT : DotAP_AB = (DeltaAP_X * DeltaAB_X) + (DeltaAP_Y * DeltaAB_Y) + (DeltaAP_Z * DeltaAB_Z)
  14.     DotAB_AB AS FLOAT : DotAB_AB = (DeltaAB_X * DeltaAB_X) + (DeltaAB_Y * DeltaAB_Y) + (DeltaAB_Z * DeltaAB_Z) // This is almost Pythagorean.    It's the Squared Length of DeltaAB.
  15.  
  16.     ProjectedPoint_DistanceFrom_LinePointA_AsMultipleOf_DeltaAB AS FLOAT // lol
  17.     ProjectedPoint_DistanceFrom_LinePointA_AsMultipleOf_DeltaAB = DotAP_AB / DotAB_AB
  18.  
  19.     Result AS Vec3
  20.     Result.x = LinePointA.x + (DeltaAB_X * ProjectedPoint_DistanceFrom_LinePointA_AsMultipleOf_DeltaAB)
  21.     Result.y = LinePointA.y + (DeltaAB_Y * ProjectedPoint_DistanceFrom_LinePointA_AsMultipleOf_DeltaAB)
  22.     Result.z = LinePointA.z + (DeltaAB_Z * ProjectedPoint_DistanceFrom_LinePointA_AsMultipleOf_DeltaAB)
  23. ENDFUNCTION Result
  24.  
  25.  
  26. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. FUNCTION prj3n(Point REF AS Vec3, LinePosition REF AS Vec3, LineNormal REF AS Vec3) // "Projection" Get ClosestPointOnLine from Point.
  28.     DeltaAP_X AS FLOAT : DeltaAP_X = Point.x - LinePosition.x
  29.     DeltaAP_Y AS FLOAT : DeltaAP_Y = Point.y - LinePosition.y
  30.     DeltaAP_Z AS FLOAT : DeltaAP_Z = Point.z - LinePosition.z
  31.  
  32.     DotAP_AB AS FLOAT : DotAP_AB = (DeltaAP_X * LineNormal.x) + (DeltaAP_Y * LineNormal.y) + (DeltaAP_Z * LineNormal.z)
  33.  
  34.     Result AS Vec3
  35.     Result.x = LinePosition.x + (LineNormal.x * DotAP_AB)
  36.     Result.y = LinePosition.y + (LineNormal.y * DotAP_AB)
  37.     Result.z = LinePosition.z + (LineNormal.z * DotAP_AB)
  38. ENDFUNCTION Result
RAW Paste Data