Пример #1
0
        public static bool Intersects(Line2D a, Segment2D b)
        {
            Vector2D v = new Vector2D(-a.Direction.Y, a.Direction.X);

            if (Vector2D.DotProduct(b.Start - a.Origin, v) >= 0.0 == Vector2D.DotProduct(b.End - a.Origin, v) >= 0.0)
            {
                return(false);
            }
            Vector2D delta = b.GetDelta();
            double   num1  = a.Direction.X * delta.Y - a.Direction.Y * delta.X;

            if (System.Math.Abs(num1) < 8.88178419700125E-16)
            {
                return(false);
            }
            Vector2D vector2D = b.Start - a.Origin;
            double   num2     = 1.0 / num1;
            double   num3     = (vector2D.X * a.Direction.Y - vector2D.Y * a.direction.X) * num2;

            if (num3 >= 0.0)
            {
                return(num3 <= 1.0);
            }
            return(false);
        }
Пример #2
0
        public static Point2D?GetIntersection(Line2D a, Segment2D b)
        {
            Vector2D v = new Vector2D(-a.Direction.Y, a.Direction.X);

            if (Vector2D.DotProduct(b.Start - a.Origin, v) >= 0.0 == Vector2D.DotProduct(b.End - a.Origin, v) >= 0.0)
            {
                return(new Point2D?());
            }
            Vector2D delta = b.GetDelta();
            double   num1  = a.Direction.X * delta.Y - a.Direction.Y * delta.X;

            if (System.Math.Abs(num1) < 8.88178419700125E-16)
            {
                return(new Point2D?());
            }
            Vector2D vector2D = b.Start - a.Origin;
            double   num2     = 1.0 / num1;
            double   num3     = (vector2D.X * delta.Y - vector2D.Y * delta.X) * num2;
            double   num4     = (vector2D.X * a.Direction.Y - vector2D.Y * a.direction.X) * num2;

            if ((num4 < 0.0 ? 0 : (num4 <= 1.0 ? 1 : 0)) == 0)
            {
                return(new Point2D?());
            }
            return(new Point2D?(num3 * a.direction + a.origin));
        }
Пример #3
0
        public static bool GetIntersectionCoefficients(
            Line2D a,
            Segment2D b,
            out double p,
            out double q,
            out bool areParallel,
            double precision)
        {
            double   length1 = a.Direction.GetLength();
            Vector2D v1      = a.Direction / length1;
            Vector2D v2      = new Vector2D(-v1.Y, v1.X);
            Vector2D u       = b.Start - a.Origin;
            double   a1      = Vector2D.DotProduct(u, v2);
            double   b1      = Vector2D.DotProduct(b.End - a.Origin, v2);

            if (a1 > b1)
            {
                MathUtil.Swap(ref a1, ref b1);
            }
            if (a1 < precision && b1 > -precision)
            {
                Vector2D delta = b.GetDelta();
                if (delta == Vector2D.Zero)
                {
                    areParallel = false;
                    p           = Vector2D.DotProduct(u, v1) / length1;
                    q           = 0.0;
                    return(true);
                }
                double   length2  = delta.GetLength();
                Vector2D vector2D = delta / length2;
                double   num1     = v1.X * vector2D.Y - v1.Y * vector2D.X;
                if (System.Math.Abs(num1) < precision)
                {
                    p           = double.NaN;
                    q           = double.NaN;
                    areParallel = true;
                    return(true);
                }
                double num2 = 1.0 / num1;
                q = (u.X * v1.Y - u.Y * v1.X) * num2 / length2;
                bool flag;
                if (!(flag = q >= -precision && q <= 1.0 + precision))
                {
                    p = double.NaN;
                    q = double.NaN;
                }
                p           = (u.X * vector2D.Y - u.Y * vector2D.X) * num2 / length1;
                areParallel = false;
                return(flag);
            }
            p           = double.NaN;
            q           = double.NaN;
            areParallel = false;
            return(false);
        }
Пример #4
0
        public Point2D[] GetIntersections(Segment2D segment, double precision)
        {
            Vector2D delta         = segment.GetDelta();
            double   lengthSquared = delta.GetLengthSquared();
            Vector2D center        = (Vector2D)this.center;
            Point2D  point2D1      = segment.Start - center;
            Point2D  point2D2      = point2D1 + delta;
            double   num1          = point2D1.X * point2D2.Y - point2D2.X * point2D1.Y;
            double   num2          = this.radius * this.radius * lengthSquared - num1 * num1;

            Point2D[] point2DArray = (Point2D[])null;
            if (MathUtil.AreApproxEqual(0.0, num2, precision))
            {
                double  num3  = num1 * delta.Y;
                double  num4  = -num1 * delta.X;
                double  num5  = 1.0 / lengthSquared;
                Point2D point = new Point2D(num3 * num5, num4 * num5) + center;
                if (segment.ContainsProjection(point, precision) && this.ContainsAngleProjection(point, precision))
                {
                    point2DArray = new Point2D[1] {
                        point
                    }
                }
                ;
            }
            else if (num2 > 0.0)
            {
                double         num3        = delta.Y < 0.0 ? -1.0 : 1.0;
                double         num4        = num1 * delta.Y;
                double         num5        = -num1 * delta.X;
                double         num6        = System.Math.Sqrt(num2);
                double         num7        = num3 * delta.X * num6;
                double         num8        = System.Math.Abs(delta.Y) * num6;
                List <Point2D> point2DList = new List <Point2D>(2);
                double         num9        = 1.0 / lengthSquared;
                Point2D        point1      = new Point2D((num4 + num7) * num9, (num5 + num8) * num9) + center;
                if (segment.ContainsProjection(point1, precision) && this.ContainsAngleProjection(point1, precision))
                {
                    point2DList.Add(point1);
                }
                Point2D point2 = new Point2D((num4 - num7) * num9, (num5 - num8) * num9) + center;
                if (segment.ContainsProjection(point2, precision) && this.ContainsAngleProjection(point2, precision))
                {
                    point2DList.Add(point2);
                }
                if (point2DList.Count > 0)
                {
                    point2DArray = point2DList.ToArray();
                }
            }
            return(point2DArray);
        }