Beispiel #1
0
        private static bool PointOnLineSSI(DBLV A, DBLV B, DBLV point)
        {
            bool onX = (point.X >= Math.Min(A.X, B.X) && point.X <= Math.Max(A.X, B.X)) || FloatHelp.ApproxEquals(point.X, A.X, .001) || FloatHelp.ApproxEquals(point.X, B.X, .001);
            bool onY = (point.Y >= Math.Min(A.Y, B.Y) && point.Y <= Math.Max(A.Y, B.Y)) || FloatHelp.ApproxEquals(point.Y, A.Y, .001) || FloatHelp.ApproxEquals(point.Y, B.Y, .001);

            return(onX && onY);
        }
Beispiel #2
0
        private static DBLV ConvertToSI(DBLV A, DBLV B)
        {
            double slope     = (B.Y - A.Y) / (B.X - A.X);
            double intersect = A.Y + (-A.X * slope);

            return(new DBLV(slope, intersect));
        }
Beispiel #3
0
 public override bool Equals(object obj)
 {
     if (obj is DBLV)
     {
         DBLV other = (DBLV)obj;
         return(other.X == X && other.Y == Y);
     }
     return(false);
 }
Beispiel #4
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.ApproxEquals(A, .001) || pointOfIntersection.ApproxEquals(B, .001) || pointOfIntersection.ApproxEquals(C, .001) || pointOfIntersection.ApproxEquals(D, .001))
                {
                    return(new ColisionInfo(true, 1, pointOfIntersection));
                }
                return(new ColisionInfo(true, 3, pointOfIntersection));
            }
            return(new ColisionInfo(false, 0, null));
        }
Beispiel #5
0
 public bool ApproxEquals(DBLV obj, double margin)
 {
     return(FloatHelp.ApproxEquals(this, obj, margin));
 }
Beispiel #6
0
 public static bool ApproxEquals(DBLV A, DBLV B, double margin)
 {
     return(Math.Abs(A.X - B.X) < margin && Math.Abs(A.Y - B.Y) < margin);
 }
Beispiel #7
0
 private static bool PointOnLineSSI(DBLV A, DBLV B, DBLV point)
 {
     return(point.X >= Math.Min(A.X, B.X) && point.X <= Math.Max(A.X, B.X) && point.Y >= Math.Min(A.Y, B.Y) && point.Y <= Math.Max(A.Y, B.Y));
 }