Java Code Examples for java.awt.geom.Area#getPathIterator()

The following examples show how to use java.awt.geom.Area#getPathIterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommandLayer.java    From rcrs-server with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void drawArea(Area area) {
	PathIterator iter = area.getPathIterator(null);
	Path2D.Float poly = new Path2D.Float();
	double[] firstPoint = null;
	while (!iter.isDone()) {
		double point[] = new double[2]; // x, y
		int type = iter.currentSegment(point);
        point[0] = t.xToScreen(point[0]);
        point[1] = t.yToScreen(point[1]);
		if (type == PathIterator.SEG_MOVETO) {
			firstPoint = point;
			poly.moveTo(point[0], point[1]);
		} else if (type == PathIterator.SEG_CLOSE) {
			poly.lineTo(firstPoint[0], firstPoint[1]);
			g.draw(poly);
			poly.reset();
		} else {
			poly.lineTo(point[0], point[1]);
		}
		iter.next();
	}
}
 
Example 2
Source File: Geometry.java    From rcrs-server with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static List<int[]> getAreas(Area area) {
	PathIterator iter = area.getPathIterator(null);
	List<int[]> areas = new ArrayList<int[]>();
	ArrayList<Integer> list = new ArrayList<Integer>();
	while (!iter.isDone()) {
		double point[] = new double[2]; // x, y
		int type = iter.currentSegment(point);
		if (type == PathIterator.SEG_CLOSE) {
			if (list.size() > 0) {
				int[] newArea = new int[list.size()];
				for (int i = 0; i < list.size(); i++)
					newArea[i] = list.get(i);
				areas.add(newArea);
				list = new ArrayList<Integer>();
			}
		} else {
			list.add((int) point[0]);
			list.add((int) point[1]);
		}
		iter.next();
	}
	return areas;
}
 
Example 3
Source File: SpriteImage.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param polygon
 * @return
 */
private static Polygon filterPolygon(Polygon polygon) {
	Area area = new Area(polygon);
	Polygon newPoly = new Polygon();
	PathIterator it = area.getPathIterator(AffineTransform.getTranslateInstance(0, 0), 0);
	float[] coords = new float[6];
	LinkedHashSet<String> set = new LinkedHashSet<String>();
	while (!it.isDone()) {
		it.currentSegment(coords);
		Point v = new Point((int) coords[0], (int) coords[1]);
		if (!set.contains(v.toString())) {
			newPoly.addPoint(v.x, v.y);
			set.add(v.toString());
		}
		it.next();
	}
	return newPoly;
}
 
Example 4
Source File: NestWorldMapPane.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
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;
    }
 
Example 5
Source File: IfcTools2D.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 6
Source File: Geometry.java    From rcrs-server with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static double surface(Area area) {
	PathIterator iter = area.getPathIterator(null);

	double sum_all = 0;
	while (!iter.isDone()) {
		List<double[]> points = new ArrayList<double[]>();
		while (!iter.isDone()) {
			double point[] = new double[2];
			int type = iter.currentSegment(point);
			iter.next();
			if (type == PathIterator.SEG_CLOSE) {
				if (points.size() > 0)
					points.add(points.get(0));
				break;
			}
			points.add(point);
		}

		double sum = 0;
		for (int i = 0; i < points.size() - 1; i++)
			sum += points.get(i)[0] * points.get(i + 1)[1]
					- points.get(i)[1] * points.get(i + 1)[0];

		sum_all += Math.abs(sum) / 2;
	}

	return sum_all;
}
 
Example 7
Source File: IfcTools2D.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Area findSmallest(Area area) {
	if (area.isSingular()) {
		System.out.println("Is singular");
		return null;
	}
	PathIterator pathIterator = area.getPathIterator(null);
	Path2D.Double tmp = new Path2D.Double();
	Path2D.Double smallest = null;
	Path2D smallestPath = null;
	while (!pathIterator.isDone()) {
		double[] coords = new double[6];
		int type = pathIterator.currentSegment(coords);
		if (type == 0) {
			tmp.moveTo(coords[0], coords[1]);
		} else if (type == 4) {
			tmp.closePath();
			
			if (smallestPath == null || IfcTools2D.containsAllPoints(new Area(smallestPath), new Area(new Path2D.Double(tmp)))) {
				smallestPath = new Path2D.Double(tmp);
				enlargeSlightlyInPlace(smallestPath);
				smallest = tmp;
			}
			tmp = new Path2D.Double();
		} else if (type == 1) {
			tmp.lineTo(coords[0], coords[1]);
		}
		pathIterator.next();
	}
	if (smallest != null) {
		return new Area(smallest);
	}
	return null;
}
 
Example 8
Source File: IfcTools2D.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
		float[] lastMoveTo = null;
		while (!pathIterator.isDone()) {
			int currentSegment = pathIterator.currentSegment(coords);
			if (currentSegment == PathIterator.SEG_CLOSE) {
//				sum += lastMoveTo[0] * coords[1] - lastMoveTo[1] * coords[0];
				last = new float[]{coords[0], coords[1]};
			} else if (currentSegment == PathIterator.SEG_MOVETO) {
				lastMoveTo = new float[]{coords[0], coords[1]};
				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 += last[0] * coords[1] - last[1] * coords[0];
				}

				last = new float[]{coords[0], coords[1]};
			} else {
				LOGGER.info("Unimplemented segment: " + currentSegment);
			}
			pathIterator.next();
		}
		if (last != null && first != null) {
			sum += last[0] * first[1] - last[1] * first[0];
		}
		return Math.abs(sum / 2f);
	}
 
Example 9
Source File: ShapeInformation.java    From han3_ji7_tsoo1_kian3 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 建立一個相關資訊物件
 * 
 * @param area
 *            要計算資訊的area
 */
public ShapeInformation(Area area)
{
	approximativeCircumference = 0.0;
	approximativeRegion = 0.0;
	double[] controlPoint = new double[6];
	SimplePolygon simplePolygon = new SimplePolygon();
	for (PathIterator pathIterator = area.getPathIterator(null); !pathIterator.isDone(); pathIterator
			.next())
	{
		int type = pathIterator.currentSegment(controlPoint);
		switch (type)
		{
		case PathIterator.SEG_CUBICTO:
			simplePolygon.addPoint(controlPoint[0], controlPoint[1]);
			simplePolygon.addPoint(controlPoint[2], controlPoint[3]);
			simplePolygon.addPoint(controlPoint[4], controlPoint[5]);
			break;
		case PathIterator.SEG_QUADTO:
			simplePolygon.addPoint(controlPoint[0], controlPoint[1]);
			simplePolygon.addPoint(controlPoint[2], controlPoint[3]);
			break;
		case PathIterator.SEG_LINETO:
			simplePolygon.addPoint(controlPoint[0], controlPoint[1]);
			break;
		case PathIterator.SEG_MOVETO:
			simplePolygon.addPoint(controlPoint[0], controlPoint[1]);
			break;
		case PathIterator.SEG_CLOSE:
			simplePolygon.finish();
			approximativeCircumference += simplePolygon.getCircumference();
			approximativeRegion += simplePolygon.getRegionSize();
			simplePolygon.clear();
			break;
		}
	}
}
 
Example 10
Source File: AreaList.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
/** Exports the given area as a list of SVG path elements with integers only. Only reads SEG_MOVETO, SEG_LINETO and SEG_CLOSE elements, all others ignored (but could be just as easily saved in the SVG path). */
static final void exportArea(final StringBuilder sb, final String indent, final Area area) {
	// I could add detectors for straight lines and thus avoid saving so many points.
	final float[] coords = new float[6];
	final float precision = 0.0001f;
	for (final PathIterator pit = area.getPathIterator(null); !pit.isDone(); ) {
		switch (pit.currentSegment(coords)) {
			case PathIterator.SEG_MOVETO:
				//Utils.log2("SEG_MOVETO: " + coords[0] + "," + coords[1]); // one point
				sb.append(indent).append("<t2_path d=\"M ");
				M.appendShortest(coords[0], precision, sb);
				sb.append(' ');
				M.appendShortest(coords[1], precision, sb);
				break;
			case PathIterator.SEG_LINETO:
				//Utils.log2("SEG_LINETO: " + coords[0] + "," + coords[1]); // one point
				sb.append(" L ");
				M.appendShortest(coords[0], precision, sb);
				sb.append(' ');
				M.appendShortest(coords[1], precision, sb);
				break;
			case PathIterator.SEG_CLOSE:
				//Utils.log2("SEG_CLOSE");
				// make a line to the first point
				//sb.append(" L ").append(x0).append(" ").append(y0);
				sb.append(" z\" />\n");
				break;
			default:
				Utils.log2("WARNING: AreaList.exportArea unhandled seg type.");
				break;
		}
		pit.next();
		if (pit.isDone()) {
			//Utils.log2("finishing");
			return;
		}
	}
}
 
Example 11
Source File: AreaList.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
/** Returns an ArrayList of ArrayList of Point as value with all paths for the Area of the given layer_id. */
public ArrayList<ArrayList<Point>> getPaths(final long layer_id) {
	Area ob = ht_areas.get(layer_id);
	if (null == ob) return null;
	if (AreaList.UNLOADED == ob) {
		ob = loadLayer(layer_id);
		if (null == ob) return null;
	}
	final Area area = ob;
	final ArrayList<ArrayList<Point>> al_paths = new ArrayList<ArrayList<Point>>();
	ArrayList<Point> al_points = null;
	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:
				al_points = new ArrayList<Point>();
				al_points.add(new Point((int)coords[0], (int)coords[1]));
				break;
			case PathIterator.SEG_LINETO:
				al_points.add(new Point((int)coords[0], (int)coords[1]));
				break;
			case PathIterator.SEG_CLOSE:
				al_paths.add(al_points);
				al_points = null;
				break;
			default:
				Utils.log2("WARNING: AreaList.getPaths() unhandled seg type.");
				break;
		}
		pit.next();
		if (pit.isDone()) {
			break;
		}
	}
	return al_paths;
}
 
Example 12
Source File: M.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
/** 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);
}
 
Example 13
Source File: ShapeAnalyst.java    From han3_ji7_tsoo1_kian3 with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * 建立一個相關資訊物件
 * 
 * @param area
 *            要計算資訊的area
 */
public ShapeAnalyst(Area area)
{
	approximativeCircumference = 0.0;
	approximativeRegion = 0.0;
	double[] controlPoint = new double[6];
	SimplePolygon simplePolygon = new SimplePolygon();
	int i=0;
	for (PathIterator pathIterator = area.getPathIterator(null); !pathIterator
			.isDone(); pathIterator.next())
	{
		int type = pathIterator.currentSegment(controlPoint);
		System.out.println(i+" main=" + type + " " + controlPoint[0] + " "
				+ controlPoint[1] + " " + controlPoint[2] + " "
				+ controlPoint[3] + " " + controlPoint[4] + " "
				+ controlPoint[5]);
		switch (type)
		{
		case PathIterator.SEG_CUBICTO:
			simplePolygon.addPoint(controlPoint[0], controlPoint[1]);
			simplePolygon.addPoint(controlPoint[2], controlPoint[3]);
			simplePolygon.addPoint(controlPoint[4], controlPoint[5]);
			break;
		case PathIterator.SEG_QUADTO:
			simplePolygon.addPoint(controlPoint[0], controlPoint[1]);
			simplePolygon.addPoint(controlPoint[2], controlPoint[3]);
			break;
		case PathIterator.SEG_LINETO:
			simplePolygon.addPoint(controlPoint[0], controlPoint[1]);
			break;
		case PathIterator.SEG_MOVETO:
			simplePolygon.addPoint(controlPoint[0], controlPoint[1]);
			break;
		case PathIterator.SEG_CLOSE:
			simplePolygon.finish();
			approximativeCircumference += simplePolygon.getCircumference();
			approximativeRegion += simplePolygon.getRegionSize();
			simplePolygon.clear();
			System.out.println("closer");
			break;
		}
		i++;
	}
}
 
Example 14
Source File: AbstractAffineTile2D.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Add a virtual {@linkplain PointMatch connection} between two
 * {@linkplain AbstractAffineTile2D Tiles}.  The
 * {@linkplain PointMatch connection} is placed in the center of the
 * intersection area of both tiles.
 *
 * TODO Not yet tested---Do we need these virtual connections?
 *
 * @param t
 */
final public void makeVirtualConnection( final AbstractAffineTile2D< ? > t )
{
	final Area a = new Area( patch.getPerimeter() );
	final Area b = new Area( t.patch.getPerimeter() );
	a.intersect( b );

	final double[] fa = new double[ 2 ];
	int i = 0;

	final double[] coords = new double[ 6 ];

	final PathIterator p = a.getPathIterator( null );
	while ( !p.isDone() )
	{
		p.currentSegment( coords );
		fa[ 0 ] += coords[ 0 ];
		fa[ 1 ] += coords[ 1 ];
		++i;
		p.next();
	}

	if ( i > 0 )
	{
		fa[ 0 ] /= i;
		fa[ 1 ] /= i;

		final double[] fb = fa. clone();
		try
		{
			model.applyInverseInPlace( fa );
			t.model.applyInverseInPlace( fb );
		}
		catch ( final NoninvertibleModelException e ) { return; }

		final Point pa = new Point( fa );
		final Point pb = new Point( fb );
		final PointMatch ma = new PointMatch( pa, pb, 0.1f );
		final PointMatch mb = new PointMatch( pb, pa, 0.1f );

		addVirtualMatch( ma );
		addConnectedTile( t );
		t.addVirtualMatch( mb );
		t.addConnectedTile( this );
	}
}