Ejemplo n.º 1
0
        /// <summary>
        /// check whether two linear segments intersect on the plane 
        /// </summary>
        /// <param name="seg1"></param>
        /// <param name="seg2"></param>
        /// <returns></returns>
        public static bool CheckSegmentIntersection(Segment seg1, Segment seg2)
        {
            bool isSameSide1 = CheckPointOnSameSide(seg1, seg2);
            bool isSameSide2 = CheckPointOnSameSide(seg2, seg1);

            return (!isSameSide1) & (!isSameSide2);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Basic sample for intersec / Not-intersec
        /// ([1.0, 2.0], [2.0, 3.0]) ([1.0, 1.0], [2.0, 2.0])  - false
        /// ([1.0, 2.0], [2.0, 2.0]) ([1.0, 1.0], [2.0, 3.0])  - ture
        /// 
        /// one segment is vertical , other segment is random (vertical, horizontal ...)
        /// ([0,0], [0,2])  ([1,1], [1,3])  - false
        /// ([0,0], [0,2])  ([0,-2], [0,5])  - true
        /// ([0,0], [0,2])  ([0,-2], [1,3])  - false
        /// ([0,0], [0,2])  ([0, 1], [1,3])  - true
        /// 
        /// one segment is horizontal , other segment is random (vertical, horizontal ...)
        /// ([0,0], [2,0])  ([1,1], [2,3]   -false
        /// ([0,0], [2,0])  ([-2,0], [5,0])  - true
        /// ([0,0], [2,0])  ([-2,0], [1,3])  - false
        /// ([0,0], [2,0])  ([1,0], [1,3])  - true
        /// 
        /// segment is just a point, the other is random (point, pass the point ...)
        /// ([0,0], [0,0])  ([1,1], [2,3]   -false
        /// ([0,0], [0,0])  ([-1,1], [1,1]   -true
        /// ([0,0], [0,0])  ([1,1], [2,2]   -true
        /// ([0,0], [0,0])  ([1,1], [1,1]   -false
        /// 
        /// </summary>
        static void TestSegmentIntersec()
        {
            Segment seg1 = new Segment
            {
            P1 = new Point { X = 0.0, Y = 0.0 },
            P2 = new Point { X = 2.0, Y = 0.0 }
            };

            Segment seg2 = new Segment
            {
            P1 = new Point { X = 1.0, Y = 1.0},
            P2 = new Point { X = 1.0, Y = 3.0}
            };

            Console.WriteLine(Algorithm.CheckSegmentIntersection(seg1, seg2));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// check the points on targetSeg is on the same side of a segment
 /// </summary>
 /// <param name="seg"></param>
 /// <param name="targetSeg"></param>
 /// <returns></returns>
 private static bool CheckPointOnSameSide(Segment seg, Segment targetSeg)
 {
     PointLocation location1 = CheckPointLocation(seg, targetSeg.P1);
     PointLocation location2 = CheckPointLocation(seg, targetSeg.P2);
     return Convert.ToBoolean(location1 & location2);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Check point location toward a segment 
        /// </summary>
        /// <param name="seg"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        private static PointLocation CheckPointLocation(Segment seg, Point point)
        {
            double yValue = 0.0;
            if ((seg.Slope != null) && (seg.Intercept != null))
            {
                yValue = (double)seg.Slope * point.X + (double)seg.Intercept;

                if (point.Y > yValue)
                    return PointLocation.Above;
                else
                    if (point.Y == yValue)
                        return PointLocation.OnSeg;
                    else
                        return PointLocation.Below;
            }
            else
            {
                if (point.X == seg.P1.X)
                {
                    if (point.Y > Math.Max(seg.P2.Y, seg.P1.Y))
                        return PointLocation.Above;
                    else
                        if (point.Y < Math.Min(seg.P2.Y, seg.P1.Y))
                            return PointLocation.Below;
                        else
                            return PointLocation.OnSeg;
                }
                else
                {
                    return (point.X > seg.P1.X) ? PointLocation.Below : PointLocation.Above;
                }
            }
        }