Пример #1
0
 /// <summary>
 /// return true if 2d coplanar line segments intersect and stores intersection in result
 /// </summary>
 /// <param name="line1In"></param>
 /// <param name="line2In"></param>
 /// <param name="result"></param>
 /// <returns></returns>
 public static IntersectionRecord2 LineLineXYIntersect(Line2 line1In, Line2 line2In)
 {
     try
     {
         IntersectionRecord2 result = new IntersectionRecord2();
         double x2min = Math.Min(line2In.Point1.X, line2In.Point2.X);
         double y2min = Math.Min(line2In.Point1.Y, line2In.Point2.Y);
         double x2max = Math.Max(line2In.Point1.X, line2In.Point2.X);
         double y2max = Math.Max(line2In.Point1.Y, line2In.Point2.Y);
         double x1min = Math.Min(line1In.Point1.X, line1In.Point2.X);
         double y1min = Math.Min(line1In.Point1.Y, line1In.Point2.Y);
         double x1max = Math.Max(line1In.Point1.X, line1In.Point2.X);
         double y1max = Math.Max(line1In.Point1.Y, line1In.Point2.Y);
         result = RayRayXYIntersect(line1In, line2In);
         if (result.Intersects)
         {
             if ((result.X <= x1max) && (result.X >= x1min) && (result.X <= x2max) && (result.X >= x2min) &&
                 (result.Y <= y1max) && (result.Y >= y1min) && (result.Y <= y2max) && (result.Y >= y2min))
             {
                 result.Intersects = true;
             }
         }
         return(result);
     }
     catch (Exception)
     {
         throw;
     }
 }
Пример #2
0
        /// <summary>
        /// returns true if 2D coplanar infinite rays intersect and stores intersection in result
        /// </summary>
        /// <param name="ray1"></param>
        /// <param name="ray2"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static IntersectionRecord2 RayRayXYIntersect(Line2 ray1, Line2 ray2)
        {
            try
            {
                IntersectionRecord2 result = new IntersectionRecord2();
                Vector2             ptOut  = new Vector2();

                if (ray1.SlopeXY == ray2.SlopeXY)
                {
                    double yt  = (ray1.Point1.X - ray2.Point1.X) * (ray2.Point2.Y - ray2.Point1.Y);
                    double yt2 = (ray2.Point2.X - ray2.Point1.X) * (ray2.Point1.Y - ray1.Point1.Y);

                    if ((yt2 - yt) > float.Epsilon)
                    {
                        result.Intersects = false;
                    }
                    else
                    {
                        result = new IntersectionRecord2(new Vector2(ray2.Point1.X, ray2.Point1.Y), true);
                    }
                }
                else
                {
                    double denom    = (ray1.Point1.X - ray1.Point2.X) * (ray2.Point1.Y - ray2.Point2.Y) - (ray2.Point1.X - ray2.Point2.X) * (ray1.Point1.Y - ray1.Point2.Y);
                    double xOut     = 0;
                    double yOut     = 0;
                    double line1Det = ray1.Point1.X * ray1.Point2.Y - ray1.Point2.X * ray1.Point1.Y;
                    double line2Det = ray2.Point1.X * ray2.Point2.Y - ray2.Point2.X * ray2.Point1.Y;
                    xOut   = (line1Det * (ray2.Point1.X - ray2.Point2.X) - line2Det * (ray1.Point1.X - ray1.Point2.X)) / denom;
                    yOut   = (line1Det * (ray2.Point1.Y - ray2.Point2.Y) - line2Det * (ray1.Point1.Y - ray1.Point2.Y)) / denom;
                    result = new IntersectionRecord2(new Vector2(xOut, yOut), true);
                }
                return(result);
            }
            catch (Exception)
            {
                throw;
            }
        }
Пример #3
0
 /// <summary>
 /// returns true if ray and line segment intersect and stores intersection in result
 /// </summary>
 /// <param name="RayIn"></param>
 /// <param name="line2In"></param>
 /// <param name="result"></param>
 /// <returns></returns>
 public static IntersectionRecord2 RayLineXYIntersect(Line2 rayIn, Line2 line2In)
 {
     try
     {
         double x2min = Math.Min(line2In.Point1.X, line2In.Point2.X);
         double y2min = Math.Min(line2In.Point1.Y, line2In.Point2.Y);
         double x2max = Math.Max(line2In.Point1.X, line2In.Point2.X);
         double y2max = Math.Max(line2In.Point1.Y, line2In.Point2.Y);
         IntersectionRecord2 result = RayRayXYIntersect(rayIn, line2In);
         if (result.Intersects)
         {
             if ((result.X > x2max) || (result.X < x2min) || (result.Y > y2max) || (result.Y < y2min))
             {
                 result.Intersects = false;
             }
         }
         return(result);
     }
     catch (Exception)
     {
         throw;
     }
 }