Skip to content

Commit c3114f1

Browse files
committed
Add diagonals and diagonalPartition functions
1 parent c814ed2 commit c3114f1

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

modules/app/src/main/java/org/locationtech/jtstest/function/CreateShapeFunctions.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.locationtech.jts.geom.Envelope;
2626
import org.locationtech.jts.geom.Geometry;
2727
import org.locationtech.jts.geom.GeometryFactory;
28+
import org.locationtech.jts.geom.LineString;
2829
import org.locationtech.jts.geom.util.AffineTransformation;
2930
import org.locationtech.jts.geom.util.AffineTransformationFactory;
3031
import org.locationtech.jts.geom.util.SineStarFactory;
@@ -396,4 +397,33 @@ public static Geometry nGon(Geometry g,
396397
pts.closeRing();
397398
return FunctionsUtil.getFactoryOrDefault(g).createPolygon(pts.toCoordinateArray());
398399
}
400+
401+
public static Geometry diagonals(Geometry g) {
402+
Coordinate[] pts = g.getCoordinates();
403+
int n = pts.length - 1;
404+
405+
List<Geometry> chords = new ArrayList<Geometry>();
406+
407+
for (int i = 0; i < n; i++) {
408+
for (int j = i + 2; j < n; j++) {
409+
if (i == j) continue;
410+
if (i == 0 && j == n-1) continue;
411+
412+
Coordinate[] chordPts = new Coordinate[] {
413+
pts[i].copy(), pts[j].copy() };
414+
LineString chord = g.getFactory().createLineString(chordPts);
415+
//-- only keep internal chords
416+
if (g.covers(chord)) {
417+
chords.add(chord);
418+
}
419+
}
420+
}
421+
return g.getFactory().buildGeometry(chords);
422+
}
423+
424+
public static Geometry diagonalPartition(Geometry g) {
425+
Geometry chords = diagonals(g);
426+
Geometry noded = NodingFunctions.MCIndexNoding(g, chords);
427+
return PolygonizeFunctions.polygonize(noded);
428+
}
399429
}

0 commit comments

Comments
 (0)