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); }
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> /// 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); } } } }