Returns absolute angle form point to target - Java 2D Graphics

Java examples for 2D Graphics:Angle

Description

Returns absolute angle form point to target

Demo Code


//package com.java2s;
import static java.lang.StrictMath.PI;
import static java.lang.StrictMath.atan2;

public class Main {
    public static void main(String[] argv) throws Exception {
        double x = 2.45678;
        double y = 2.45678;
        double targetX = 2.45678;
        double targetY = 2.45678;
        System.out.println(getAngleTo(x, y, targetX, targetY));
    }/*from  w  w  w . j  a v  a 2 s  . c  om*/

    /**
     * Returns absolute angle form point to target
     */
    public static double getAngleTo(double x, double y, double targetX,
            double targetY) {
        double absoluteAngleTo = atan2(targetY - y, targetX - x);

        while (absoluteAngleTo > PI) {
            absoluteAngleTo -= 2.0D * PI;
        }

        while (absoluteAngleTo < -PI) {
            absoluteAngleTo += 2.0D * PI;
        }

        return absoluteAngleTo;
    }
}

Related Tutorials