public static float getArea(Area area) { float sum = 0; PathIterator pathIterator = area.getPathIterator(null); float[] coords = new float[6]; float[] last = null; float[] first = null; while (!pathIterator.isDone()) { int currentSegment = pathIterator.currentSegment(coords); if (currentSegment == PathIterator.SEG_CLOSE) { } else if (currentSegment == PathIterator.SEG_MOVETO) { last = new float[]{coords[0], coords[1]}; if (first == null) { first = new float[]{coords[0], coords[1]}; } } else if (currentSegment == PathIterator.SEG_LINETO) { if (last != null) { sum = sum + last[0] * coords[1] - last[1] * coords[0]; } last = new float[]{coords[0], coords[1]}; } pathIterator.next(); } if (last != null && first != null) { sum = sum + last[0] * first[1] - last[1] * first[0]; } return sum / 2f; }
public static boolean containsAllPoints(Area areaOutside, Area areaInside) { Area clone = (Area) areaOutside.clone(); IfcTools2D.enlargeSlightlyInPlace(clone); PathIterator iterator = areaInside.getPathIterator(null); double[] coords = new double[6]; boolean allInside = true; while (!iterator.isDone()) { iterator.currentSegment(coords); if (!clone.contains(new Point2D.Double(coords[0], coords[1]))) { allInside = false; break; } iterator.next(); } return allInside; }
return null; PathIterator pathIterator = area.getPathIterator(null); Path2D.Double tmp = new Path2D.Double(); Path2D.Double smallest = null;
for (IfcRepresentationItem ifcRepresentationItem : ifcShapeRepresentation.getItems()) { Area area = getArea(multiplierMillimeters, productMatrix, ifcRepresentationItem); if (area != null && area.getPathIterator(null).isDone()) { return area;
private static GeneralPath areaToPath(final Area negativeArea, final double deltaX, final GeneralPath pixelPath) { final float[] floats = new float[6]; // move to correct rectangle final AffineTransform transform = AffineTransform.getTranslateInstance(deltaX, 0.0); final PathIterator iterator = negativeArea.getPathIterator(transform); while (!iterator.isDone()) { final int segmentType = iterator.currentSegment(floats); if (segmentType == PathIterator.SEG_LINETO) { pixelPath.lineTo(floats[0], floats[1]); } else if (segmentType == PathIterator.SEG_MOVETO) { pixelPath.moveTo(floats[0], floats[1]); } else if (segmentType == PathIterator.SEG_CLOSE) { pixelPath.closePath(); } iterator.next(); } return pixelPath; } }
public static void main(String[] args) { Area a = new Area(new Rectangle(1, 1, 5, 5)); PathIterator iterator = a.getPathIterator(null); float[] floats = new float[6]; Polygon polygon = new Polygon(); while (!iterator.isDone()) { int type = iterator.currentSegment(floats); int x = (int) floats[0]; int y = (int) floats[1]; if(type != PathIterator.SEG_CLOSE) { polygon.addPoint(x, y); System.out.println("adding x = " + x + ", y = " + y); } iterator.next(); } }
public static GeneralPath areaToPath(Area negativeArea, double deltaX) { final GeneralPath pixelPath = new GeneralPath(GeneralPath.WIND_NON_ZERO); final float[] floats = new float[6]; // move to correct rectangle final AffineTransform transform = AffineTransform.getTranslateInstance(deltaX, 0.0); final PathIterator iterator = negativeArea.getPathIterator(transform); while (!iterator.isDone()) { final int segmentType = iterator.currentSegment(floats); if (segmentType == PathIterator.SEG_LINETO) { pixelPath.lineTo(floats[0], floats[1]); } else if (segmentType == PathIterator.SEG_MOVETO) { pixelPath.moveTo(floats[0], floats[1]); } else if (segmentType == PathIterator.SEG_CLOSE) { pixelPath.closePath(); } else { throw new IllegalStateException("unhandled segment type in path iterator: " + segmentType); } iterator.next(); } return pixelPath; }
private int computeHashCode() { int hash = width * 1313 + height * 71; // Area.hashCode() is not implemented, so we have to do it ourselves.. PathIterator pi = area.getPathIterator(null); double[] arg = new double[6]; while (!pi.isDone()) { for (int i = 0; i < 6; i++) { arg[i] = 0; } int val = pi.getWindingRule(); val += 3 * pi.currentSegment(arg); for (int i = 0; i < 6; i++) { val = (val * 5) + (int) Math.floor(arg[i] * 1000); } hash = hash * 7 + val; pi.next(); } return hash; }
final double[] coords = new double[6]; for (final PathIterator pi = area.getPathIterator(null); !pi.isDone(); pi.next()) {
static public final Collection<Polygon> getPolygonsByRounding(final Area area) { final ArrayList<Polygon> pols = new ArrayList<Polygon>(); Polygon pol = new Polygon(); final float[] coords = new float[6]; for (final PathIterator pit = area.getPathIterator(null); !pit.isDone(); ) { final int seg_type = pit.currentSegment(coords); switch (seg_type) { case PathIterator.SEG_MOVETO: case PathIterator.SEG_LINETO: pol.addPoint(Math.round(coords[0]), Math.round(coords[1])); break; case PathIterator.SEG_CLOSE: pols.add(pol); pol = new Polygon(); break; default: Utils.log2("WARNING: unhandled seg type."); break; } pit.next(); if (pit.isDone()) { break; } } return pols; }
@SuppressWarnings("null") static public final Collection<Polygon> getPolygons(final Area area) { final ArrayList<Polygon> pols = new ArrayList<Polygon>(); Polygon pol = null; final float[] coords = new float[6]; for (final PathIterator pit = area.getPathIterator(null); !pit.isDone(); ) { final int seg_type = pit.currentSegment(coords); switch (seg_type) { case PathIterator.SEG_MOVETO: pol = new Polygon(); //$FALL-THROUGH$ case PathIterator.SEG_LINETO: pol.addPoint((int)coords[0], (int)coords[1]); break; case PathIterator.SEG_CLOSE: pols.add(pol); break; default: Utils.log2("WARNING: unhandled seg type."); break; } pit.next(); if (pit.isDone()) { break; } } return pols; } static public final Collection<Polygon> getPolygonsByRounding(final Area area) {
/** Return a new Area resulting from applying @param ict to @param a; * assumes areas consists of paths with moveTo, lineTo and close operations. */ static public final Area transform(final mpicbg.models.CoordinateTransform ict, final Area a) { final GeneralPath path = new GeneralPath(); final float[] coords = new float[6]; final double[] fp = new double[2]; for (final PathIterator pit = a.getPathIterator(null); !pit.isDone(); ) { final int seg_type = pit.currentSegment(coords); fp[0] = coords[0]; fp[1] = coords[1]; ict.applyInPlace(fp); switch (seg_type) { case PathIterator.SEG_MOVETO: path.moveTo(fp[0], fp[1]); break; case PathIterator.SEG_LINETO: case PathIterator.SEG_CLOSE: path.lineTo(fp[0], fp[1]); break; default: Utils.log2("WARNING: unhandled seg type."); break; } pit.next(); if (pit.isDone()) { break; } } return new Area(path); }
public void fillHoles() { Polygon pol = new Polygon(); for (PathIterator pit = area.getPathIterator(null); !pit.isDone(); ) { float[] coords = new float[6]; int seg_type = pit.currentSegment(coords); switch (seg_type) { case PathIterator.SEG_MOVETO: case PathIterator.SEG_LINETO: pol.addPoint((int)coords[0], (int)coords[1]); break; case PathIterator.SEG_CLOSE: area.add(new Area(pol)); // prepare next: pol = new Polygon(); break; default: Utils.log2("WARNING: unhandled seg type."); break; } pit.next(); if (pit.isDone()) { break; } } } }
/** Detect if a point is not in the area, but lays inside one of its path, which is returned as a Polygon. Otherwise returns null. The given x,y must be already in the Area's coordinate system. */ static public final Polygon findPath(final Area area, final int x, final int y) { Polygon pol = new Polygon(); for (final PathIterator pit = area.getPathIterator(null); !pit.isDone(); ) { final float[] coords = new float[6]; final int seg_type = pit.currentSegment(coords); switch (seg_type) { case PathIterator.SEG_MOVETO: case PathIterator.SEG_LINETO: pol.addPoint((int)coords[0], (int)coords[1]); break; case PathIterator.SEG_CLOSE: if (pol.contains(x, y)) return pol; // else check next pol = new Polygon(); break; default: Utils.log2("WARNING: unhandled seg type."); break; } pit.next(); if (pit.isDone()) { break; } } return null; }
public static Polygon getPolygonOutline(BufferedImage image) { Area a = getOutline(image, new Color(0, 0, 0, 0), false, 10); // 10 or whatever color tolerance you want Polygon p = new Polygon(); FlatteningPathIterator fpi = new FlatteningPathIterator(a.getPathIterator(null), 0.1); // 0.1 or how sloppy you want it double[] pts = new double[6]; while (!fpi.isDone()) { switch (fpi.currentSegment(pts)) { case FlatteningPathIterator.SEG_MOVETO: case FlatteningPathIterator.SEG_LINETO: p.addPoint((int) pts[0], (int) pts[1]); break; } fpi.next(); } return p; }
/** * Get all the corners of the plot (supports non-rectangular shapes). * @return A list of the plot corners */ public List<Location> getAllCorners() { Area area = new Area(); for (RegionWrapper region : this.getRegions()) { Rectangle2D rect = new Rectangle2D.Double(region.minX - 0.6, region.minZ - 0.6, region.maxX - region.minX + 1.2, region.maxZ - region.minZ + 1.2); Area rectArea = new Area(rect); area.add(rectArea); } List<Location> locs = new ArrayList<>(); double[] coords = new double[6]; for (PathIterator pi = area.getPathIterator(null); !pi.isDone(); pi.next()) { int type = pi.currentSegment(coords); int x = (int) MathMan.inverseRound(coords[0]); int z = (int) MathMan.inverseRound(coords[1]); if (type != 4) { locs.add(new Location(this.getWorldName(), x, 0, z)); } } return locs; }
import java.awt.*; import java.awt.geom.*; import javax.swing.*; import java.util.*; public class Board extends JPanel { Area area; void go( Area area ) { this.area = area; JFrame frame = new JFrame("Circle Test"); frame.getContentPane().add(this); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); repaint(); frame.setPreferredSize(new Dimension(800,800)); frame.pack(); frame.setVisible(true); } public void paintComponent(Graphics g) { AffineTransform at = new AffineTransform(); at.translate( 100, 100 ); at.scale( 50, 50 ); PathIterator pit = area.getPathIterator( at ); Path2D path = new Path2D.Double(); path.append( pit, true ); Graphics2D g2d = (Graphics2D)g; g2d.draw( path ); } }
public static BezierPath op(final BezierPath path1, final BezierPath path2, final OP op) { final Area area1 = new Area(path1.toGeneralPath()); final Area area2 = new Area(path2.toGeneralPath()); switch (op) { case ADD: area1.add(area2); break; case XOR: area1.exclusiveOr(area2); break; case INTERSECT: area1.intersect(area2); break; case SUBTRACT: area1.subtract(area2); break; } return toBezierPath(area1.getPathIterator(new AffineTransform())); }
@Override public ResultsTable measureAreas(ResultsTable rt) { if (0 == ht_areas.size()) return rt; if (null == rt) rt = Utils.createResultsTable("Area results", new String[]{"id", "name-id", "layer index", "area"}); final double nameId = getNameId(); final Calibration cal = layer_set.getCalibration(); final String units = cal.getUnit(); // Sort by Layer final TreeMap<Layer,Area> sm = new TreeMap<Layer, Area>(Layer.COMPARATOR); for (final Map.Entry<Long,Area> e : ht_areas.entrySet()) { sm.put(layer_set.getLayer(e.getKey()), e.getValue()); } for (final Map.Entry<Layer,Area> e : sm.entrySet()) { final Area area = e.getValue(); if (area.isEmpty()) continue; rt.incrementCounter(); rt.addLabel("units", units); rt.addValue(0, this.id); rt.addValue(1, nameId); rt.addValue(2, layer_set.indexOf(e.getKey()) + 1); // 1-based // measure surface final double pixel_area = Math.abs(AreaCalculations.area(area.createTransformedArea(this.at).getPathIterator(null))); final double surface = pixel_area * cal.pixelWidth * cal.pixelHeight; rt.addValue(3, surface); } return rt; }
PathIterator itr = a.getPathIterator(tx);