Guest User

Untitled

a guest
May 23rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. L1 => P1 - P2
  2. L2 => Q1 - Q2
  3.  
  4. L1 = P1 + a * V1 (V1 = P2-P1)
  5. L2 = Q1 + b * V2 (V2 = Q2-Q1)
  6.  
  7. P1 : (0, 2, 0)
  8. P2 : (4, 4, 0)
  9. Q1 : (-2, 4, 0)
  10. Q2 : (2, -4, 0)
  11.  
  12. public static Vector3 IntersectLines(Vector3 line1Point1, Vector3 line1Point2,
  13. Vector3 line2Point1, Vector3 line2Point2,
  14. out float a, out float b)
  15. {
  16. // Line1: line1Point1 + a * (line1Point2 - line1Point1) == P1 + aV1
  17. // Line2: line2Point1 + b * (line2Point2 - line2Point1) == P2 + bV2
  18. Vector3 V1 = line1Point2 - line1Point1;
  19. Vector3 V2 = line2Point2 - line2Point1;
  20. if (Vector3.Cross(V1, V2).magnitude.Equals(0)) // Not Coplanar
  21. throw new NoIntersectionException();
  22. // a(V1 X V2) = (P2-P1) X V2
  23. // a = |(P2-P1) X V2| / |V1 X V2|
  24. a = Vector3.Cross((line1Point1 - line2Point1), V2).magnitude / Vector3.Cross(V1, V2).magnitude;
  25. // Point is on Segment1 if 0<=a<=1 AND P1+a*V1 == Point (a is absolute value)
  26. // Point: P1 + a * V1
  27. Vector3 intersectPoint = line1Point1 + a * V1;
  28. // Point: P2 + b * V2
  29. //intersectPoint = line2Point1 + b * V2;
  30. b = (intersectPoint - line2Point1).magnitude/V2.magnitude;
  31. // Point is on Segment2 if 0<=b<=1 AND P2+b*V2 == Point (b is absolute value)
  32. return intersectPoint;
  33. }
Add Comment
Please, Sign In to add comment