Skip to content

Commit

Permalink
implement line intersect
Browse files Browse the repository at this point in the history
  • Loading branch information
OttyLab committed Jan 27, 2022
1 parent b5ccab2 commit 8b07285
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
38 changes: 38 additions & 0 deletions services-turf/src/main/java/com/mapbox/turf/TurfMisc.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,44 @@ private TurfMisc() {
throw new AssertionError("No Instances.");
}

/**
* Takes lines {@link LineString} and returns intersect points. The time complexity is O(nm)
* that is not as efficient as Turf.js implementation.
*
* @param line1 LineString 1
* @param line2 LineString 2
* @return Intersect points
* @see <a href="https://turfjs.org/docs/#lineIntersect">Turf Line intersect documentation</a>
* @since 6.2.0
*/
@NonNull
public static List<Point> lineIntersect(@NonNull LineString line1, @NonNull LineString line2) {
List<Point> result = new ArrayList<>();
Point[] line1Points = line1.coordinates().toArray(new Point[line1.coordinates().size()]);
Point[] line2Points = line2.coordinates().toArray(new Point[line2.coordinates().size()]);

for (int i = 0; i < line1Points.length - 1; i++) {
for (int j = 0; j < line2Points.length - 1; j++) {
LineIntersectsResult intersects = lineIntersects(
line1Points[i].longitude(),
line1Points[i].latitude(),
line1Points[i + 1].longitude(),
line1Points[i + 1].latitude(),
line2Points[j].longitude(),
line2Points[j].latitude(),
line2Points[j + 1].longitude(),
line2Points[j + 1].latitude());

if (intersects != null) {
result.add(Point.fromLngLat(
intersects.horizontalIntersection(),
intersects.verticalIntersection()));
}
}
}
return result;
}

/**
* Takes a line, a start {@link Point}, and a stop point and returns the line in between those
* points.
Expand Down
72 changes: 72 additions & 0 deletions services-turf/src/test/java/com/mapbox/turf/TurfMiscTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,78 @@ public class TurfMiscTest extends TestUtils {
@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void lineIntersect_intersectingEdge() {
List<Point> line1Coords = new ArrayList<>();
line1Coords.add(Point.fromLngLat(1.0, 2.0));
line1Coords.add(Point.fromLngLat(2.0, 3.0));
LineString line1 = LineString.fromLngLats(line1Coords);

List<Point> line2Coords = new ArrayList<>();
line2Coords.add(Point.fromLngLat(2.0, 3.0));
line2Coords.add(Point.fromLngLat(4.0, 2.0));
LineString line2 = LineString.fromLngLats(line2Coords);

List<Point> result1 = TurfMisc.lineIntersect(line1, line2);
assertEquals(1, result1.size());
assertEquals(Point.fromLngLat(2.0, 3.0), result1.get(0));

List<Point> line3Coords = new ArrayList<>();
line3Coords.add(Point.fromLngLat(0.0, 3.0));
line3Coords.add(Point.fromLngLat(1.0, 2.0));
LineString line3 = LineString.fromLngLats(line3Coords);

List<Point> result2 = TurfMisc.lineIntersect(line1, line3);
assertEquals(1, result2.size());
assertEquals(Point.fromLngLat(1.0, 2.0), result2.get(0));
}

@Test
public void lineIntersect_intersecting() {
List<Point> line1Coords = new ArrayList<>();
line1Coords.add(Point.fromLngLat(2.0, 1.0));
line1Coords.add(Point.fromLngLat(2.0, 5.0));
line1Coords.add(Point.fromLngLat(2.0, 9.0));
LineString line1 = LineString.fromLngLats(line1Coords);

List<Point> line2Coords = new ArrayList<>();
line2Coords.add(Point.fromLngLat(4.0, 1.0));
line2Coords.add(Point.fromLngLat(0.0, 5.0));
line2Coords.add(Point.fromLngLat(2.0, 9.0));
LineString line2 = LineString.fromLngLats(line2Coords);

List<Point> expected = new ArrayList<>();
expected.add(Point.fromLngLat(2.0, 3.0));
expected.add(Point.fromLngLat(2.0, 9.0));

List<Point> result = TurfMisc.lineIntersect(line1, line2);
for(int i = 0; i < result.size(); i++) {
assertEquals(expected.get(i), result.get(i));
}
}

@Test
public void lineIntersect_nonintersecting() {
List<Point> line1Coords = new ArrayList<>();
line1Coords.add(Point.fromLngLat(2.0, 1.0));
line1Coords.add(Point.fromLngLat(2.0, 5.0));
line1Coords.add(Point.fromLngLat(2.0, 9.0));
LineString line1 = LineString.fromLngLats(line1Coords);

List<Point> line2Coords = new ArrayList<>();
line2Coords.add(Point.fromLngLat(1.0, 1.0));
line2Coords.add(Point.fromLngLat(1.0, 5.0));
line2Coords.add(Point.fromLngLat(1.0, 9.0));
LineString line2 = LineString.fromLngLats(line2Coords);

List<Point> expected = new ArrayList<>();
expected.add(Point.fromLngLat(2.0, 3.0));
expected.add(Point.fromLngLat(2.0, 9.0));

List<Point> result = TurfMisc.lineIntersect(line1, line2);
assertEquals(0, result.size());
}

@Test
public void lineSlice_throwsStartStopPointException() throws Exception {
thrown.expect(TurfException.class);
Expand Down

0 comments on commit 8b07285

Please sign in to comment.