Java Angle from Point getAngle(Point2D.Double vertPt, Point2D.Double edgePt)

Here you can find the source of getAngle(Point2D.Double vertPt, Point2D.Double edgePt)

Description

get Angle

License

Apache License

Declaration

public static double getAngle(Point2D.Double vertPt, Point2D.Double edgePt) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.geom.Point2D;

public class Main {
    public static double getAngle(Point2D.Double vertPt, Point2D.Double edgePt) {
        // if loop is 1st and 4th qudrant, else loop is 2nd and 3rd quadrant
        double dx = Math.abs(edgePt.x - vertPt.x), dy = Math.abs(edgePt.y - vertPt.y);
        if (dx == 0.0D) {
            if (edgePt.y > vertPt.y)
                return Math.PI / 2.0D;
            else//from   ww w  . ja  v a  2  s. c o m
                return 3 * Math.PI / 2.0D;
        } else if (dy == 0.0D) {
            if (edgePt.x > vertPt.x)
                return 0.0D;
            else
                return Math.PI;
        }
        if (edgePt.x > vertPt.x) {
            // If loop is 1st Quadrant and else is 4th Quadrant
            if (edgePt.y > vertPt.y)
                return Math.atan(dy / dx);
            else
                return (3 * Math.PI / 2.0D + Math.atan(dy / dx));
        } else {
            // If loop is 2nd Quadrant and else is 3rd Quadrant
            if (edgePt.y > vertPt.y)
                return (Math.PI / 2.0D + Math.atan(dx / dy));
            else
                return (Math.PI + Math.atan(dy / dx));
        }
    }

    public static double getAngle(java.awt.Point a, java.awt.Point b) {
        double dx = b.getX() - a.getX();
        double dy = b.getY() - a.getY();
        System.out.println("dx: " + dx + " dy: " + dy);
        double angle = 0.0d;

        if (dx == 0.0) {
            if (dy == 0.0)
                angle = 0.0;
            else if (dy > 0.0)
                angle = Math.PI / 2.0;
            else
                angle = (Math.PI * 3.0) / 2.0;
        } else if (dy == 0.0) {
            if (dx > 0.0)
                angle = 0.0;
            else
                angle = Math.PI;
        } else {
            if (dx < 0.0)
                angle = Math.atan(dy / dx) + Math.PI;
            else if (dy < 0.0)
                angle = Math.atan(dy / dx) + (2 * Math.PI);
            else
                angle = Math.atan(dy / dx);
        }
        System.out.println("angle: " + angle);
        angle = (angle * 180) / Math.PI;
        System.out.println("Recalc Angle: " + angle);
        return angle;
    }
}

Related

  1. getAngle(double originX, double originY, double x, double y)
  2. getAngle(Point2D center, Point2D point)
  3. getAngle(Point2D origin, Point2D target)
  4. getAngle(Point2D p1, Point2D p2)
  5. getAngle(Point2D startPosition, Point2D endPosition)
  6. getAngleForPoint(Arc2D arc, int x, int y)