Subtract a specified geometric area of data points from another shape to yield gaps. - Java java.lang

Java examples for java.lang:Math Geometry Shape

Description

Subtract a specified geometric area of data points from another shape to yield gaps.

Demo Code


import java.awt.BasicStroke;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.FlatteningPathIterator;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.io.Serializable;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class Main{
    /**/*from  w  w w . jav  a 2s.co m*/
     * Precision.
     */
    public static final double EPSILON = 1e-5;
    /**
     * Subtract a specified geometric area of data points from another shape to
     * yield gaps.
     *
     * @param shapeArea  Shape from which to subtract.
     * @param gap        Size of the gap.
     * @param rounded    Gap corners will be rounded if {@code true}.
     * @param pointPos   Position of the data point
     * @param pointShape Shape of the data point
     * @return Shape with punched holes
     */
    public static Area punch(Area shapeArea, double gap, boolean rounded,
            Point2D pointPos, Shape pointShape) {
        if (gap <= 1e-10 || pointPos == null || pointShape == null) {
            return shapeArea;
        }

        AffineTransform tx = AffineTransform.getTranslateInstance(
                pointPos.getX(), pointPos.getY());

        int gapJoin = rounded ? BasicStroke.JOIN_ROUND
                : BasicStroke.JOIN_MITER;
        Area gapArea = GeometryUtils.grow(
                tx.createTransformedShape(pointShape), gap, gapJoin, 10f);

        shapeArea.subtract(gapArea);

        return shapeArea;
    }
    /**
     * Expand or shrink a shape in all directions by a defined offset.
     *
     * @param s      Shape
     * @param offset Offset
     * @return New shape that was expanded or shrunk by the specified amount
     */
    public static Area grow(final Shape s, final double offset) {
        return grow(s, offset, BasicStroke.JOIN_MITER, 10f);
    }
    /**
     * Expand or shrink a shape in all directions by a defined offset.
     *
     * @param s          Shape
     * @param offset     Offset to expand/shrink
     * @param join       Method for handling edges (see BasicStroke)
     * @param miterlimit Limit for miter joining method
     * @return New shape that is expanded or shrunk by the specified amount
     */
    public static Area grow(final Shape s, final double offset, int join,
            float miterlimit) {
        Area shape = new Area(s);

        if (MathUtils.almostEqual(offset, 0.0, EPSILON)) {
            return shape;
        }

        Stroke stroke = new BasicStroke((float) Math.abs(2.0 * offset),
                BasicStroke.CAP_SQUARE, join, miterlimit);
        Area strokeShape = new Area(stroke.createStrokedShape(s));

        if (offset > 0.0) {
            shape.add(strokeShape);
        } else {
            shape.subtract(strokeShape);
        }

        return shape;
    }
}

Related Tutorials