Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- L1 => P1 - P2
- L2 => Q1 - Q2
- L1 = P1 + a * V1 (V1 = P2-P1)
- L2 = Q1 + b * V2 (V2 = Q2-Q1)
- P1 : (0, 2, 0)
- P2 : (4, 4, 0)
- Q1 : (-2, 4, 0)
- Q2 : (2, -4, 0)
- public static Vector3 IntersectLines(Vector3 line1Point1, Vector3 line1Point2,
- Vector3 line2Point1, Vector3 line2Point2,
- out float a, out float b)
- {
- // Line1: line1Point1 + a * (line1Point2 - line1Point1) == P1 + aV1
- // Line2: line2Point1 + b * (line2Point2 - line2Point1) == P2 + bV2
- Vector3 V1 = line1Point2 - line1Point1;
- Vector3 V2 = line2Point2 - line2Point1;
- if (Vector3.Cross(V1, V2).magnitude.Equals(0)) // Not Coplanar
- throw new NoIntersectionException();
- // a(V1 X V2) = (P2-P1) X V2
- // a = |(P2-P1) X V2| / |V1 X V2|
- a = Vector3.Cross((line1Point1 - line2Point1), V2).magnitude / Vector3.Cross(V1, V2).magnitude;
- // Point is on Segment1 if 0<=a<=1 AND P1+a*V1 == Point (a is absolute value)
- // Point: P1 + a * V1
- Vector3 intersectPoint = line1Point1 + a * V1;
- // Point: P2 + b * V2
- //intersectPoint = line2Point1 + b * V2;
- b = (intersectPoint - line2Point1).magnitude/V2.magnitude;
- // Point is on Segment2 if 0<=b<=1 AND P2+b*V2 == Point (b is absolute value)
- return intersectPoint;
- }
Add Comment
Please, Sign In to add comment