get Corner Shape - Java java.lang

Java examples for java.lang:Math Geometry Shape

Description

get Corner Shape

Demo Code


import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.List;

public class Main{
    /**//from   w  w  w. j  a  va 2s.  c  o m
     * @return
     */

    public static Shape getCornerShape(Point2D ptA, Point2D ptO,
            Point2D ptB, double cornerSize) {
        if (ptA == null || ptO == null || ptB == null || ptA.equals(ptO)
                || ptB.equals(ptO)) {
            return null;
        }

        Point2D ptI1 = GeomUtil.getCollinearPointWithLength(ptO, ptA,
                cornerSize);
        Point2D ptI2 = GeomUtil.getCollinearPointWithLength(ptO, ptB,
                cornerSize);

        double rotSignum = Math.signum(GeomUtil
                .getSmallestRotationAngleDeg(GeomUtil.getAngleDeg(ptB, ptO,
                        ptA)));
        Point2D ptI3 = GeomUtil.getPerpendicularPointFromLine(ptO, ptA,
                ptI1, rotSignum * cornerSize);

        Path2D path = new Path2D.Double(Path2D.WIND_NON_ZERO, 3);
        path.moveTo(ptI1.getX(), ptI1.getY());
        path.lineTo(ptI3.getX(), ptI3.getY());
        path.lineTo(ptI2.getX(), ptI2.getY());

        return path;
    }
    /**
     * @param ptA
     *            the first point of line segment
     * @param ptB
     *            the last point of line segment
     * @param newLength
     *            represents length from ptA to the return point ptC along AB line segment.<br>
     *            If >AB , ptC point will be located on extension of AB<br>
     *            If <0 , ptC point will be located on extension of BA <br>
     *            If >0 && < AB , ptC point will be interior of AB<br>
     * @return New point ptC coordinates or null if any argument is invalid
     */
    public static Point2D.Double getCollinearPointWithLength(Point2D ptA,
            Point2D ptB, double newLength) {
        if (ptA != null && ptB != null) {
            return getCollinearPointWithRatio(ptA, ptB,
                    newLength / ptA.distance(ptB));
        }
        return null;
    }
    /**
     * @param angle
     *            in Degree
     * @return angle in the range of [ -180 ; 180 ]
     */
    public static double getSmallestRotationAngleDeg(double angle) {
        angle = angle % 360.0;
        if (Math.abs(angle) > 180.0) {
            angle -= Math.signum(angle) * 360.0;
        }
        return angle;
    }
    /**
     * @return angle between BA & BC line segment in Radiant<br>
     *         0 is returned if any argument is invalid
     */
    public static double getAngleDeg(Point2D ptA, Point2D ptB, Point2D ptC) {
        if (ptA != null && ptB != null && ptC != null) {
            return Math.toDegrees(getAngleRad(ptA, ptB, ptC));
        }
        return 0;
    }
    /**
     * @return angle of AB line segment in radiant<br>
     *         0 is returned if any argument is invalid
     */
    public static double getAngleDeg(Point2D ptA, Point2D ptB) {
        return (ptA != null && ptB != null) ? Math.toDegrees(getAngleRad(
                ptA, ptB)) : null;
    }
    /**
     * Find a point at a given perpendicular distance from a line
     * 
     * @param ptA
     *            Start of line segment
     * @param ptB
     *            End of line segment
     * @param ptP
     *            Point of AB line
     * @param distPC
     *            Distance from line to return Point ptC <br>
     *            If >0 angle between AB and PC is +90? <br>
     *            If <0 angle between AB and PC is -+90?
     * @return ptC point
     */
    public static Point2D getPerpendicularPointFromLine(Point2D ptA,
            Point2D ptB, Point2D ptP, double distPC) {
        if (ptA == null || ptB == null || ptA.equals(ptB) || ptP == null) {
            return null;
        }

        double distAB = ptA.distance(ptB);
        double ux = -(ptB.getY() - ptA.getY()) / distAB;
        double uy = (ptB.getX() - ptA.getX()) / distAB;

        return new Point2D.Double(ptP.getX() + distPC * ux, ptP.getY()
                + distPC * uy);
    }
    public static Point2D getPerpendicularPointFromLine(Point2D ptA,
            Point2D ptB, double distAP, double distPC) {
        return getPerpendicularPointFromLine(ptA, ptB,
                getCollinearPointWithLength(ptA, ptB, distAP), distPC);
    }
    /**
     * @param ptA
     *            first point of line segment
     * @param ptB
     *            last point of line segment
     * @param k
     *            represents ratio between AB and AC, ptC being the returned point along AB line segment.<br>
     *            If >1 , ptC point will be located on extension of AB<br>
     *            If <0 , ptC point will be located on extension of BA <br>
     *            If >0 && <AB , ptC point will be interior of AB<br>
     * @return New point ptC coordinates or null if any argument is invalid
     */
    public static Point2D.Double getCollinearPointWithRatio(Point2D ptA,
            Point2D ptB, double k) {
        if (ptA != null && ptB != null) {
            return new Point2D.Double(
                    ptB.getX() * k + ptA.getX() * (1 - k), ptB.getY() * k
                            + ptA.getY() * (1 - k));
        }
        return null;
    }
    /**
     * @return angle between BA & BC line segment in Degree <br>
     *         0 is returned if any argument is invalid
     */
    public static double getAngleRad(Point2D ptA, Point2D ptB, Point2D ptC) {
        if (ptA != null && ptB != null && ptC != null) {
            return getAngleRad(ptB, ptC) - getAngleRad(ptB, ptA);
        }
        return 0;
    }
    /**
     * Compute angle into image system basis where positive angle are defined in a ClockWise orientation<br>
     * Note : angle should be computed with "Math.atan2(ptB.getY() - ptA.getY(), ptB.getX() - ptA.getX())" in an
     * ortho-normal basis system where positive angle is defined in a CounterClockWise orientation.
     * 
     * @return angle of AB line segment in radiant<br>
     *         0 is returned if any argument is invalid
     */

    public static double getAngleRad(Point2D ptA, Point2D ptB) {
        return (ptA != null && ptB != null) ? Math.atan2(
                ptA.getY() - ptB.getY(), ptB.getX() - ptA.getX()) : null;
    }
}

Related Tutorials