get Angle Degree - Java java.lang

Java examples for java.lang:Math Geometry

Description

get Angle Degree

Demo Code


//package com.java2s;

import java.awt.geom.Point2D;

public class Main {
    /**//from   w w w .j a v a 2s  .  co m
     * @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;
    }

    /**
     * @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