public static bool SegmentIntersects(IGeometry g1, IGeometry g2)
 {
     Coordinate[] pt1 = g1.Coordinates;
     Coordinate[] pt2 = g2.Coordinates;
     RobustLineIntersector ri = new RobustLineIntersector();
     ri.ComputeIntersection(pt1[0], pt1[1], pt2[0], pt2[1]);
     return ri.HasIntersection;
 }
 public void TestCollinear4() {
     RobustLineIntersector i = new RobustLineIntersector();
     Coordinate p1 = new Coordinate(30, 10);
     Coordinate p2 = new Coordinate(20, 10);
     Coordinate q1 = new Coordinate(10, 10);
     Coordinate q2 = new Coordinate(30, 10);
     i.ComputeIntersection(p1, p2, q1, q2);
     Assert.AreEqual(RobustLineIntersector.Collinear, i.IntersectionNum);
     Assert.IsTrue(i.HasIntersection);
 }
 public void Test2Lines() {
     RobustLineIntersector i = new RobustLineIntersector();
     Coordinate p1 = new Coordinate(10, 10);
     Coordinate p2 = new Coordinate(20, 20);
     Coordinate q1 = new Coordinate(20, 10);
     Coordinate q2 = new Coordinate(10, 20);
     Coordinate x = new Coordinate(15, 15);
     i.ComputeIntersection(p1, p2, q1, q2);
     Assert.AreEqual(RobustLineIntersector.DoIntersect, i.IntersectionNum);
     Assert.AreEqual(1, i.IntersectionNum);
     Assert.AreEqual(x, i.GetIntersection(0));
     Assert.IsTrue(i.IsProper);
     Assert.IsTrue(i.HasIntersection);
 }
Exemple #4
0
        /// <summary>
        /// Tests whether a point lies on the line defined by a list of
        /// coordinates.
        /// </summary>
        /// <param name="p">The point to test</param>
        /// <param name="line">The line coordinates</param>
        /// <returns>
        /// <c>true</c> if the point is a vertex of the line or lies in the interior
        /// of a line segment in the line
        /// </returns>
        public static bool IsOnLine(Coordinate p, Coordinate[] line)
        {
            var lineIntersector = new RobustLineIntersector();

            for (int i = 1; i < line.Length; i++)
            {
                var p0 = line[i - 1];
                var p1 = line[i];
                lineIntersector.ComputeIntersection(p, p0, p1);
                if (lineIntersector.HasIntersection)
                {
                    return(true);
                }
            }
            return(false);
        }
        /// <summary>
        /// Tests whether a point lies on the line segments defined by a
        /// list of coordinates.
        /// </summary>
        /// <param name="p"></param>
        /// <param name="pt"></param>
        /// <returns>true if the point is a vertex of the line
        /// or lies in the interior of a line segment in the linestring
        /// </returns>
        public static bool IsOnLine(Coordinate p, Coordinate[] pt)
        {
            LineIntersector lineIntersector = new RobustLineIntersector();

            for (int i = 1; i < pt.Length; i++)
            {
                Coordinate p0 = pt[i - 1];
                Coordinate p1 = pt[i];
                lineIntersector.ComputeIntersection(p, p0, p1);
                if (lineIntersector.HasIntersection)
                {
                    return(true);
                }
            }
            return(false);
        }
        public static IGeometry SegmentIntersectionDd(IGeometry g1, IGeometry g2)
        {
            Coordinate[] pt1 = g1.Coordinates;
            Coordinate[] pt2 = g2.Coordinates;

            // first check if there actually is an intersection
            RobustLineIntersector ri = new RobustLineIntersector();
            ri.ComputeIntersection(pt1[0], pt1[1], pt2[0], pt2[1]);
            if (!ri.HasIntersection)
            {
                // no intersection => return empty point
                return g1.Factory.CreatePoint((Coordinate)null);
            }

            Coordinate intPt = CGAlgorithmsDD.Intersection(pt1[0], pt1[1], pt2[0], pt2[1]);
            return g1.Factory.CreatePoint(intPt);
        }
Exemple #7
0
        /// <summary>
        /// Tests whether a point lies on the line defined by a list of
        /// coordinates.
        /// </summary>
        /// <param name="p">The point to test</param>
        /// <param name="line">The line coordinates</param>
        /// <returns>
        /// <c>true</c> if the point is a vertex of the line or lies in the interior
        /// of a line segment in the line
        /// </returns>
        public static bool IsOnLine(Coordinate p, ICoordinateSequence line)
        {
            var lineIntersector = new RobustLineIntersector();
            var p0 = new Coordinate();
            var p1 = new Coordinate();
            int n  = line.Count;

            for (int i = 1; i < n; i++)
            {
                line.GetCoordinate(i - 1, p0);
                line.GetCoordinate(i, p1);
                lineIntersector.ComputeIntersection(p, p0, p1);
                if (lineIntersector.HasIntersection)
                {
                    return(true);
                }
            }
            return(false);
        }
 public static IGeometry SegmentIntersection(IGeometry g1, IGeometry g2)
 {
     Coordinate[] pt1 = g1.Coordinates;
     Coordinate[] pt2 = g2.Coordinates;
     RobustLineIntersector ri = new RobustLineIntersector();
     ri.ComputeIntersection(pt1[0], pt1[1], pt2[0], pt2[1]);
     switch (ri.IntersectionNum)
     {
         case 0:
             // no intersection => return empty point
             return g1.Factory.CreatePoint((Coordinate)null);
         case 1:
             // return point
             return g1.Factory.CreatePoint(ri.GetIntersection(0));
         case 2:
             // return line
             return g1.Factory.CreateLineString(
                 new Coordinate[] {
                     ri.GetIntersection(0),
                     ri.GetIntersection(1)
                 });
     }
     return null;
 }
 /// <summary>
 /// Computes an intersection point between two segments, if there is one.
 /// There may be 0, 1 or many intersection points between two segments.
 /// If there are 0, null is returned. If there is 1 or more, a single one
 /// is returned (chosen at the discretion of the algorithm).  If
 /// more information is required about the details of the intersection,
 /// the {RobustLineIntersector} class should be used.
 /// </summary>
 /// <param name="line">A line segment</param>
 /// <returns> An intersection point, or <c>null</c> if there is none.</returns>
 /// <see cref="RobustLineIntersector"/>
 public Coordinate Intersection(LineSegment line)
 {
     LineIntersector li = new RobustLineIntersector();
     li.ComputeIntersection(_p0, _p1, line._p0, line._p1);
     if (li.HasIntersection)
         return li.GetIntersection(0);
     return null;
 }
 /// <summary> 
 /// Tests whether a point lies on the line segments defined by a
 /// list of coordinates.
 /// </summary>
 /// <param name="p"></param>
 /// <param name="pt"></param>
 /// <returns>true if the point is a vertex of the line
 /// or lies in the interior of a line segment in the linestring
 /// </returns>
 public static bool IsOnLine(Coordinate p, Coordinate[] pt) 
 {
     LineIntersector lineIntersector = new RobustLineIntersector();
     for (int i = 1; i < pt.Length; i++) 
     {
         Coordinate p0 = pt[i - 1];
         Coordinate p1 = pt[i];
         lineIntersector.ComputeIntersection(p, p0, p1);
         if (lineIntersector.HasIntersection) 
             return true;                
     }
     return false;
 }
        public void CheckInputNotAltered(Coordinate[] pt, int scaleFactor)
        {
            // save input points
            Coordinate[] savePt = new Coordinate[4];
            for (int i = 0; i < 4; i++)
            {
                savePt[i] = new Coordinate(pt[i]);
            }

            LineIntersector li = new RobustLineIntersector();
            li.PrecisionModel = new PrecisionModel(scaleFactor);
            li.ComputeIntersection(pt[0], pt[1], pt[2], pt[3]);

            // check that input points are unchanged
            for (int i = 0; i < 4; i++)
            {
                Assert.AreEqual(savePt[i], pt[i], "Input point " + i + " was altered - ");
            }
        }
        /// <summary>
        /// Check that intersection of segment defined by points in pt array
        /// is equal to the expectedIntPt value (up to the given distanceTolerance)
        /// </summary>
        /// <param name="pt"></param>
        /// <param name="expectedIntersectionNum"></param>
        /// <param name="expectedIntPt">the expected intersection points (maybe null if not tested)</param>
        /// <param name="distanceTolerance">tolerance to use for equality test</param>
        private void CheckIntersection(Coordinate[] pt,
            int expectedIntersectionNum,
            Coordinate[] expectedIntPt,
            double distanceTolerance)
        {
            LineIntersector li = new RobustLineIntersector();
            li.ComputeIntersection(pt[0], pt[1], pt[2], pt[3]);

            int intNum = li.IntersectionNum;
            Assert.AreEqual(expectedIntersectionNum, intNum, "Number of intersections not as expected");

            if (expectedIntPt != null)
            {
                Assert.AreEqual(intNum, expectedIntPt.Length, "Wrong number of expected int pts provided");
                // test that both points are represented here
                if (intNum == 1)
                {
                    CheckIntPoints(expectedIntPt[0], li.GetIntersection(0), distanceTolerance);
                }
                else if (intNum == 2)
                {
                    CheckIntPoints(expectedIntPt[1], li.GetIntersection(0), distanceTolerance);
                    CheckIntPoints(expectedIntPt[1], li.GetIntersection(0), distanceTolerance);

                    if (!(Equals(expectedIntPt[0], li.GetIntersection(0), distanceTolerance)
                          || Equals(expectedIntPt[0], li.GetIntersection(1), distanceTolerance)))
                    {
                        CheckIntPoints(expectedIntPt[0], li.GetIntersection(0), distanceTolerance);
                        CheckIntPoints(expectedIntPt[0], li.GetIntersection(1), distanceTolerance);
                    }
                    else if (!(Equals(expectedIntPt[1], li.GetIntersection(0), distanceTolerance)
                               || Equals(expectedIntPt[1], li.GetIntersection(1), distanceTolerance)))
                    {
                        CheckIntPoints(expectedIntPt[1], li.GetIntersection(0), distanceTolerance);
                        CheckIntPoints(expectedIntPt[1], li.GetIntersection(1), distanceTolerance);
                    }
                }
            }
        }