ShapeHelper { public static List<Shape> splitLine(Shape line, double firstLineLength) { ArrayList<Shape> shapes = new ArrayList<Shape>(); if (ShapeHelper.getLineLength(line) < firstLineLength) { shapes.add(line); return shapes; } PathIterator it = line.getPathIterator(null, FLATNESS); double coords[] = new double[6]; Path2D.Double segment = new Path2D.Double(); double p = 0.0; double p1; it.currentSegment(coords); double x1 = coords[0]; double y1 = coords[1]; segment.moveTo(x1, y1); double xFirst = x1; double yFirst = y1; it.next(); double x2; double y2; boolean first = true; while (!it.isDone()) { int type = it.currentSegment(coords); if (type == PathIterator.SEG_CLOSE) { x2 = xFirst; y2 = yFirst; } else { x2 = coords[0]; y2 = coords[1]; } double xx, yy; xx = x2 - x1; yy = y2 - y1; p1 = Math.sqrt(xx * xx + yy * yy); p += p1; if (first && p > firstLineLength) { first = false; double delta = (p - firstLineLength); Point2D.Double pt = getPointAt(x1, y1, x2, y2, p1 - delta); x1 = pt.x; y1 = pt.y; segment.lineTo(x1, y1); p = 0; shapes.add(segment); segment = new Path2D.Double(); segment.moveTo(x1, y1); } else { segment.lineTo(x2, y2); x1 = x2; y1 = y2; it.next(); } } shapes.add(segment); return shapes; } private  ShapeHelper(); static double getAreaPerimeterLength(Shape area); static double getLineLength(Shape line); static void printvertices(Shape shp); static List<Shape> splitLine(Shape line, double firstLineLength); static List<Shape> splitLineInSeg(Shape line, double segLength); static List<Shape> splitLine(Shape line, int nbPart); static Point2D.Double getPointAt(Shape shp, double distance); static List<Shape> perpendicularOffset(Shape shp, double offset); static Line2D.Double intersection(Line2D.Double line, Rectangle2D.Double bounds); static Geometry clipToExtent(Geometry theGeom, Envelope extent); static final double ONE_DEG_IN_RAD; }
@Test public void testSplitLine_Shape_double() { System.out.println("splitLine"); Path2D.Double line = new Path2D.Double() {}; line.moveTo(0,0); line.lineTo(10, 10); line.lineTo(0, 20); line.lineTo(10, 30); line.lineTo(0, 40); double coords[] = new double[6]; List<Shape> result1 = ShapeHelper.splitLine(line, 28.28); for (Shape shp : result1){ PathIterator it = shp.getPathIterator(null); System.out.println ("Shape: "); while (!it.isDone()){ it.currentSegment(coords); System.out.println ("(" + coords[0] + ";" + " " + coords[1] + ")"); it.next(); } }; result1 = ShapeHelper.splitLine(line, 35.0); for (Shape shp : result1){ PathIterator it = shp.getPathIterator(null); System.out.println ("Shape: "); while (!it.isDone()){ it.currentSegment(coords); System.out.println ("(" + coords[0] + ";" + " " + coords[1] + ")"); it.next(); } } result1 = ShapeHelper.splitLine(line, 70.0); for (Shape shp : result1){ PathIterator it = shp.getPathIterator(null); System.out.println ("Shape: "); while (!it.isDone()){ it.currentSegment(coords); System.out.println ("(" + coords[0] + ";" + " " + coords[1] + ")"); it.next(); } } }