Beispiel #1
0
        public static ColisionInfo LinesIntersect(Vector2 Atmp, Vector2 Btmp, Vector2 Ctmp, Vector2 Dtmp)
        {
            DBLV A = new DBLV(Atmp), B = new DBLV(Btmp), C = new DBLV(Ctmp), D = new DBLV(Dtmp);

            DBLV line1 = ConvertToSI(A, B);
            DBLV line2 = ConvertToSI(C, D);

            // Coterminal?
            if (line1.Equals(line2))
            {
                if (PointOnLineSSI(A, B, C) || PointOnLineSSI(A, B, D))
                {
                    // C -> D -> B
                    DBLV Colision1 = (PointOnLineSSI(A, B, C) ? C : (PointOnLineSSI(A, B, D) ? D : B));
                    // A -> B -> D
                    DBLV Colision2 = (PointOnLineSSI(C, D, A) ? A : (PointOnLineSSI(C, D, B) ? B : D));

                    return(new ColisionInfo(true, 2, Colision1, Colision2));
                }
                return(new ColisionInfo(false, 0, null));
            }

            DBLV pointOfIntersection = new DBLV();

            if (!double.IsInfinity(line1.X) && !double.IsInfinity(line2.X))
            {
                pointOfIntersection.X = (line1.Y - line2.Y) / (line2.X - line1.X);
                pointOfIntersection.Y = (line1.X * pointOfIntersection.X) + line1.Y;
            }
            else if (!double.IsInfinity(line2.X))
            {
                pointOfIntersection.X = A.X;
                pointOfIntersection.Y = (line2.X * pointOfIntersection.X) + line2.Y;
            }
            else if (!double.IsInfinity(line1.X))
            {
                pointOfIntersection.X = C.X;
                pointOfIntersection.Y = (line1.X * pointOfIntersection.X) + line1.Y;
            }

            if (PointOnLineSSI(A, B, pointOfIntersection) && PointOnLineSSI(C, D, pointOfIntersection))
            {
                if (pointOfIntersection.Equals(A) || pointOfIntersection.Equals(B) || pointOfIntersection.Equals(C) || pointOfIntersection.Equals(D))
                {
                    return(new ColisionInfo(true, 1, pointOfIntersection));
                }
                return(new ColisionInfo(true, 3, pointOfIntersection));
            }
            return(new ColisionInfo(false, 0, null));
        }