public void LineAlgorithmsIsParallelTest() { // parallel lines Coordinate firstLineStart = new Coordinate(1, 1); Coordinate firstLineEnd = new Coordinate(4, 1); Coordinate secondLineStart = new Coordinate(2, 2); Coordinate secondLineEnd = new Coordinate(4, 2); Assert.IsTrue(LineAlgorithms.IsParallel(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); Assert.IsFalse(LineAlgorithms.Intersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); firstLineStart = new Coordinate(1.2, 1.2); firstLineEnd = new Coordinate(1.2, 7.2); secondLineStart = new Coordinate(2.2, 8.2); secondLineEnd = new Coordinate(2.2, 15.2); Assert.IsTrue(LineAlgorithms.IsParallel(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); Assert.IsFalse(LineAlgorithms.Intersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); firstLineStart = new Coordinate(1, 1); firstLineEnd = new Coordinate(1, 7); secondLineStart = new Coordinate(1, 1); secondLineEnd = new Coordinate(1, 7); Assert.IsTrue(LineAlgorithms.IsParallel(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); Assert.IsTrue(LineAlgorithms.Intersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); // non parallel lines firstLineStart = new Coordinate(1, 1); firstLineEnd = new Coordinate(1, 7); secondLineStart = new Coordinate(2, 1); secondLineEnd = new Coordinate(2.2, 8); Assert.IsFalse(LineAlgorithms.IsParallel(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); firstLineStart = new Coordinate(1, 1); firstLineEnd = new Coordinate(1, 7); secondLineStart = new Coordinate(0, 2); secondLineEnd = new Coordinate(10, 2); Assert.IsFalse(LineAlgorithms.IsParallel(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); // parallel infinite lines firstLineStart = new Coordinate(1, 1); CoordinateVector firstVector = new CoordinateVector(0, 1); secondLineStart = new Coordinate(2, 2); CoordinateVector secondVector = new CoordinateVector(0, 1); Assert.IsTrue(LineAlgorithms.IsParallel(firstLineStart, firstVector, secondLineStart, secondVector)); firstLineStart = new Coordinate(2, 5); firstVector = new CoordinateVector(0, 1); secondLineStart = new Coordinate(2, 4); secondVector = new CoordinateVector(0, 4); Assert.IsTrue(LineAlgorithms.Coincides(firstLineStart, firstVector, secondLineStart, secondVector)); Assert.IsTrue(LineAlgorithms.IsParallel(firstLineStart, firstVector, secondLineStart, secondVector)); firstLineStart = new Coordinate(2, 4); firstVector = new CoordinateVector(-3, 1); secondLineStart = new Coordinate(5, 3); secondVector = new CoordinateVector(-3, 1); Assert.IsTrue(LineAlgorithms.IsParallel(firstLineStart, firstVector, secondLineStart, secondVector)); // non parallel infinite lines firstLineStart = new Coordinate(1, 1); firstVector = new CoordinateVector(0, 1); secondLineStart = new Coordinate(1, 1); secondVector = new CoordinateVector(1, 0); Assert.IsTrue(LineAlgorithms.Intersects(firstLineStart, firstVector, secondLineStart, secondVector)); Assert.IsFalse(LineAlgorithms.IsParallel(firstLineStart, firstVector, secondLineStart, secondVector)); firstLineStart = new Coordinate(2, 5); firstVector = new CoordinateVector(0, 1); secondLineStart = new Coordinate(2, 4); secondVector = new CoordinateVector(4, 0); Assert.IsTrue(LineAlgorithms.Intersects(firstLineStart, firstVector, secondLineStart, secondVector)); Assert.IsFalse(LineAlgorithms.IsParallel(firstLineStart, firstVector, secondLineStart, secondVector)); }
public void LineAlgorithmsIntersectsTest() { // coinciding lines Coordinate firstLineStart = new Coordinate(1, 1); Coordinate firstLineEnd = new Coordinate(4, 1); Coordinate secondLineStart = new Coordinate(1, 1); Coordinate secondLineEnd = new Coordinate(4, 1); Assert.IsTrue(LineAlgorithms.Intersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); // parallel lines firstLineStart = new Coordinate(1.3, 1.3); firstLineEnd = new Coordinate(1.3, 4.3); secondLineStart = new Coordinate(2.3, 1.3); secondLineEnd = new Coordinate(2.3, 4.3); Assert.IsFalse(LineAlgorithms.Intersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); // intersecting lines firstLineStart = new Coordinate(1, 1); firstLineEnd = new Coordinate(1, 4); secondLineStart = new Coordinate(4, 2); secondLineEnd = new Coordinate(0, 2); Assert.IsTrue(LineAlgorithms.Intersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); firstLineStart = new Coordinate(1, 1); firstLineEnd = new Coordinate(4, 4); secondLineStart = new Coordinate(1, 4); secondLineEnd = new Coordinate(4, 1); Assert.IsTrue(LineAlgorithms.Intersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); firstLineStart = new Coordinate(2, 2); firstLineEnd = new Coordinate(2, 8); secondLineStart = new Coordinate(2, 5); secondLineEnd = new Coordinate(2, 10); Assert.IsTrue(LineAlgorithms.Intersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); firstLineStart = new Coordinate(2, 2); firstLineEnd = new Coordinate(2, 8); secondLineStart = new Coordinate(2, 1); secondLineEnd = new Coordinate(2, 8); Assert.IsTrue(LineAlgorithms.Intersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); firstLineStart = new Coordinate(7, 7); firstLineEnd = new Coordinate(7, 7); secondLineStart = new Coordinate(7, 7); secondLineEnd = new Coordinate(7, 7); Assert.IsTrue(LineAlgorithms.Intersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); // not intersecting lines firstLineStart = new Coordinate(2, 2); firstLineEnd = new Coordinate(2, 8); secondLineStart = new Coordinate(2.1, 1); secondLineEnd = new Coordinate(10, 8); Assert.IsFalse(LineAlgorithms.Intersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); firstLineStart = new Coordinate(2, 2); firstLineEnd = new Coordinate(2, 8); secondLineStart = new Coordinate(1, 8.001); secondLineEnd = new Coordinate(10, 8.001); Assert.IsFalse(LineAlgorithms.Intersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); // internally intersecting lines firstLineStart = new Coordinate(1, 4); firstLineEnd = new Coordinate(4, 1); secondLineStart = new Coordinate(3, 2); secondLineEnd = new Coordinate(2, 3); Assert.IsTrue(LineAlgorithms.InternalIntersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); firstLineStart = new Coordinate(1, 4); firstLineEnd = new Coordinate(4, 1); secondLineStart = new Coordinate(3, 2); secondLineEnd = new Coordinate(3, 2); Assert.IsTrue(LineAlgorithms.InternalIntersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); firstLineStart = new Coordinate(1, 4); firstLineEnd = new Coordinate(4, 1); secondLineStart = new Coordinate(1, 2); secondLineEnd = new Coordinate(200, 3); Assert.IsTrue(LineAlgorithms.InternalIntersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); firstLineStart = new Coordinate(1, 4); firstLineEnd = new Coordinate(4, 1); secondLineStart = new Coordinate(1, 2); secondLineEnd = new Coordinate(200, 3); Assert.IsTrue(LineAlgorithms.InternalIntersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); // not internally intersecting lines firstLineStart = new Coordinate(0, 0); firstLineEnd = new Coordinate(0, 1000.33); secondLineStart = new Coordinate(1000.33, 200); secondLineEnd = new Coordinate(0, 0); Assert.IsFalse(LineAlgorithms.InternalIntersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); firstLineStart = new Coordinate(1, 1); firstLineEnd = new Coordinate(100, 1); secondLineStart = new Coordinate(1, 4); secondLineEnd = new Coordinate(1, 1); Assert.IsFalse(LineAlgorithms.InternalIntersects(firstLineStart, firstLineEnd, secondLineStart, secondLineEnd)); // intersecting infinite lines firstLineStart = new Coordinate(2, 5); CoordinateVector firstVector = new CoordinateVector(0, 1); secondLineStart = new Coordinate(2, 4); CoordinateVector secondVector = new CoordinateVector(4, 0); Assert.IsTrue(LineAlgorithms.Intersects(firstLineStart, firstVector, secondLineStart, secondVector)); firstLineStart = new Coordinate(2, 4); firstVector = new CoordinateVector(-3, 1); secondLineStart = new Coordinate(5, 3); secondVector = new CoordinateVector(-3, 1); Assert.IsTrue(LineAlgorithms.Intersects(firstLineStart, firstVector, secondLineStart, secondVector)); firstLineStart = new Coordinate(1, 1); firstVector = new CoordinateVector(0, 1); secondLineStart = new Coordinate(1, 1); secondVector = new CoordinateVector(1, 0); Assert.IsTrue(LineAlgorithms.Intersects(firstLineStart, firstVector, secondLineStart, secondVector)); firstLineStart = new Coordinate(1, 1); firstVector = new CoordinateVector(0, 1); secondLineStart = new Coordinate(2, 2); secondVector = new CoordinateVector(0, 1); Assert.IsTrue(LineAlgorithms.IsParallel(firstLineStart, firstVector, secondLineStart, secondVector)); Assert.IsTrue(LineAlgorithms.Intersects(firstLineStart, firstVector, secondLineStart, secondVector)); }
/// <summary> /// Computes whether one or more line strings specified by coordinates intersects with each other. /// </summary> public void Compute() { // source: http://geomalgorithms.com/a09-_intersect-3.html EndPointEvent e = _eventQueue.Next(); SweepLineSegment segment; while (e != null) { switch (e.Type) { case EventType.Left: segment = _sweepLine.Add(e); if (segment.Above != null && _sweepLine.IsAdjacent(segment.Edge, segment.Above.Edge) && segment.LeftCoordinate == segment.Above.LeftCoordinate) { _sweepLine.Intersect(segment, segment.Above); } else if (segment.Below != null && _sweepLine.IsAdjacent(segment.Edge, segment.Below.Edge) && segment.LeftCoordinate == segment.Below.LeftCoordinate) { _sweepLine.Intersect(segment, segment.Below); } if (segment.Above != null && !_sweepLine.IsAdjacent(segment.Edge, segment.Above.Edge) && LineAlgorithms.Intersects(segment.LeftCoordinate, segment.RightCoordinate, segment.Above.LeftCoordinate, segment.Above.RightCoordinate, PrecisionModel)) { _hasResult = true; _result = true; return; } if (segment.Below != null && !_sweepLine.IsAdjacent(segment.Edge, segment.Below.Edge) && LineAlgorithms.Intersects(segment.LeftCoordinate, segment.RightCoordinate, segment.Below.LeftCoordinate, segment.Below.RightCoordinate, PrecisionModel)) { _hasResult = true; _result = true; return; } break; case EventType.Right: segment = _sweepLine.Search(e); if (segment != null) { if (segment.Above != null && segment.Below != null && !_sweepLine.IsAdjacent(segment.Below.Edge, segment.Above.Edge) && LineAlgorithms.Intersects(segment.Above.LeftCoordinate, segment.Above.RightCoordinate, segment.Below.LeftCoordinate, segment.Below.RightCoordinate, PrecisionModel)) { _hasResult = true; _result = true; return; } _sweepLine.Remove(segment); } break; } e = _eventQueue.Next(); } _hasResult = true; _result = false; }
/// <summary> /// Computes the result of the algorithm. /// </summary> public void Compute() { Double minDistance = Double.MaxValue; Coordinate firstPrevious = null, secondPrevious = null; foreach (Coordinate first in this.First) { if (first == null) { continue; } foreach (Coordinate second in this.Second) { if (second == null) { continue; } // distance between a vertex of first, and a vertex of second Double distance = Coordinate.Distance(first, second); if (distance < minDistance) { minDistance = distance; } // distance between a vertex of first, and a segment of second if (secondPrevious != null) { distance = DistanceFromLine(first, secondPrevious, second); if (distance < minDistance) { minDistance = distance; } } // distance between a vertex of second, and a segment of first if (firstPrevious != null) { distance = DistanceFromLine(second, firstPrevious, first); if (distance < minDistance) { minDistance = distance; } } if (firstPrevious != null && secondPrevious != null && LineAlgorithms.Intersects(firstPrevious, first, secondPrevious, second)) { this.result = 0; this.hasResult = true; return; } secondPrevious = second; } firstPrevious = first; } this.result = this.precisionModel.MakePrecise(minDistance); this.hasResult = true; }
/// <summary> /// Computes whether one or more line strings specified by coordinates intersects with each other. /// </summary> public void Compute() { EndPointEvent e = this.eventQueue.Next(); SweepLineSegment segment; while (e != null) { switch (e.Type) { case EventType.Left: segment = this.sweepLine.Add(e); if (segment.Above != null && this.sweepLine.IsAdjacent(segment.Edge, segment.Above.Edge) && segment.LeftCoordinate == segment.Above.LeftCoordinate) { this.sweepLine.Intersect(segment, segment.Above); } else if (segment.Below != null && this.sweepLine.IsAdjacent(segment.Edge, segment.Below.Edge) && segment.LeftCoordinate == segment.Below.LeftCoordinate) { this.sweepLine.Intersect(segment, segment.Below); } if (segment.Above != null && !this.sweepLine.IsAdjacent(segment.Edge, segment.Above.Edge) && LineAlgorithms.Intersects(segment.LeftCoordinate, segment.RightCoordinate, segment.Above.LeftCoordinate, segment.Above.RightCoordinate, this.PrecisionModel)) { this.hasResult = true; this.result = true; return; } if (segment.Below != null && !this.sweepLine.IsAdjacent(segment.Edge, segment.Below.Edge) && LineAlgorithms.Intersects(segment.LeftCoordinate, segment.RightCoordinate, segment.Below.LeftCoordinate, segment.Below.RightCoordinate, this.PrecisionModel)) { this.hasResult = true; this.result = true; return; } break; case EventType.Right: segment = this.sweepLine.Search(e); if (segment != null) { if (segment.Above != null && segment.Below != null && !this.sweepLine.IsAdjacent(segment.Below.Edge, segment.Above.Edge) && LineAlgorithms.Intersects(segment.Above.LeftCoordinate, segment.Above.RightCoordinate, segment.Below.LeftCoordinate, segment.Below.RightCoordinate, this.PrecisionModel)) { this.hasResult = true; this.result = true; return; } this.sweepLine.Remove(segment); } break; } e = this.eventQueue.Next(); } this.hasResult = true; this.result = false; }