Java examples for java.lang:Math Geometry Line
Returns the line fragments of the specified Shape.
//package com.java2s; import java.awt.Shape; import java.awt.geom.FlatteningPathIterator; import java.awt.geom.Line2D; import java.awt.geom.PathIterator; import java.awt.geom.Point2D; import java.util.ArrayDeque; import java.util.Deque; public class Main { /**/* w w w . jav a 2 s. com*/ * Returns the line fragments of the specified Shape. * * @param path Shape to be divided. * @param swapped Invert segment direction. * @return Array of lines. */ public static Line2D[] shapeToLines(Shape path, boolean swapped) { Deque<Line2D> lines = new ArrayDeque<Line2D>(); PathIterator i = new FlatteningPathIterator( path.getPathIterator(null), 0.5); double[] coords = new double[6]; double[] coordsPrev = new double[6]; while (!i.isDone()) { int segment = i.currentSegment(coords); if (segment == PathIterator.SEG_LINETO || segment == PathIterator.SEG_CLOSE) { Line2D line; if (!swapped) { line = new Line2D.Double(coordsPrev[0], coordsPrev[1], coords[0], coords[1]); lines.addLast(line); } else { line = new Line2D.Double(coords[0], coords[1], coordsPrev[0], coordsPrev[1]); lines.addFirst(line); } } if (segment == PathIterator.SEG_CLOSE && !lines.isEmpty()) { Point2D firstPoint = lines.getFirst().getP1(); Point2D lastPoint = lines.getLast().getP2(); if (!firstPoint.equals(lastPoint)) { Line2D line; if (!swapped) { line = new Line2D.Double(coords[0], coords[1], firstPoint.getX(), firstPoint.getY()); lines.addLast(line); } else { line = new Line2D.Double(firstPoint.getX(), firstPoint.getY(), coords[0], coords[1]); lines.addFirst(line); } } } System.arraycopy(coords, 0, coordsPrev, 0, 6); i.next(); } Line2D[] linesArray = new Line2D[lines.size()]; lines.toArray(linesArray); return linesArray; } }