/// <summary> /// Computes an intersection of the lines /// </summary> public static bool IntersectLineLine(Vector2 originA, Vector2 directionA, Vector2 originB, Vector2 directionB, out IntersectionLineLine2 intersection) { intersection = new IntersectionLineLine2(); Vector2 originBToA = originA - originB; float denominator = VectorE.PerpDot(directionA, directionB); float perpDotB = VectorE.PerpDot(directionB, originBToA); if (Mathf.Abs(denominator) < Epsilon) { // Parallel float perpDotA = VectorE.PerpDot(directionA, originBToA); if (Mathf.Abs(perpDotA) > Epsilon || Mathf.Abs(perpDotB) > Epsilon) { // Not collinear intersection.type = IntersectionType.None; return(false); } // Collinear intersection.type = IntersectionType.Line; intersection.point = originA; return(true); } // Not parallel intersection.type = IntersectionType.Point; intersection.point = originA + directionA * (perpDotB / denominator); return(true); }
/// <summary> /// Computes an intersection of the lines /// </summary> public static bool LineLine(Vector2 originA, Vector2 directionA, Vector2 originB, Vector2 directionB, out IntersectionLineLine2 intersection) { Vector2 originBToA = originA - originB; float denominator = VectorE.PerpDot(directionA, directionB); float perpDotB = VectorE.PerpDot(directionB, originBToA); if (Mathf.Abs(denominator) < Geometry.Epsilon) { // Parallel float perpDotA = VectorE.PerpDot(directionA, originBToA); if (Mathf.Abs(perpDotA) > Geometry.Epsilon || Mathf.Abs(perpDotB) > Geometry.Epsilon) { // Not collinear intersection = IntersectionLineLine2.None(); return(false); } // Collinear intersection = IntersectionLineLine2.Line(originA); return(true); } // Not parallel intersection = IntersectionLineLine2.Point(originA + directionA * (perpDotB / denominator)); return(true); }
/// <summary> /// Computes an intersection of the lines /// </summary> public static bool IntersectLineLine(Line2 lineA, Line2 lineB, out IntersectionLineLine2 intersection) { return(IntersectLineLine(lineA.origin, lineA.direction, lineB.origin, lineB.direction, out intersection)); }