Java Geometry Algorithm computeAngleDEG(@Nonnull Point2D p1, @Nonnull Point2D p2)

Here you can find the source of computeAngleDEG(@Nonnull Point2D p1, @Nonnull Point2D p2)

Description

compute the angle (direction in degrees) from point 1 to point 2

Note: Goes CCW from south to east to north to west, etc.

License

Open Source License

Parameter

Parameter Description
p1 the first Point2D
p2 the second Point2D

Return

the angle in degrees

Declaration

@CheckReturnValue
public static double computeAngleDEG(@Nonnull Point2D p1, @Nonnull Point2D p2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.geom.Point2D;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;

public class Main {
    /**/*from www  .ja  va 2  s  .c  o m*/
     * compute the angle (direction in degrees) for a vector
     *
     * @param p the vector (point relative to zeroPoint2D)
     * @return the angle in degrees
     */
    @CheckReturnValue
    public static double computeAngleDEG(@Nonnull Point2D p) {
        return Math.toDegrees(computeAngleRAD(p));
    }

    /**
     * compute the angle (direction in degrees) from point 1 to point 2
     * <p>
     * Note: Goes CCW from south to east to north to west, etc.
     * For JMRI subtract from 90.0 to get east, south, west, north
     *
     * @param p1 the first Point2D
     * @param p2 the second Point2D
     * @return the angle in degrees
     */
    @CheckReturnValue
    public static double computeAngleDEG(@Nonnull Point2D p1, @Nonnull Point2D p2) {
        return Math.toDegrees(computeAngleRAD(subtract(p1, p2)));
    }

    /**
     * compute the angle (direction in radians) for a vector
     *
     * @param p the vector (point relative to zeroPoint2D)
     * @return the angle in radians
     */
    @CheckReturnValue
    public static double computeAngleRAD(@Nonnull Point2D p) {
        return Math.atan2(p.getX(), p.getY());
    }

    /**
     * compute the angle (direction in radians) from point 1 to point 2
     * <p>
     * Note: Goes CCW from south to east to north to west, etc.
     * For JMRI subtract from PI/2 to get east, south, west, north
     *
     * @param p1 the first Point2D
     * @param p2 the second Point2D
     * @return the angle in radians
     */
    @CheckReturnValue
    public static double computeAngleRAD(@Nonnull Point2D p1, @Nonnull Point2D p2) {
        return computeAngleRAD(subtract(p1, p2));
    }

    /**
     * subtract two points
     *
     * @param pA the first point
     * @param pB the second point
     * @return the difference of the two points
     */
    @CheckReturnValue
    public static Point2D subtract(@Nonnull Point2D pA, @Nonnull Point2D pB) {
        return new Point2D.Double(pA.getX() - pB.getX(), pA.getY() - pB.getY());
    }
}

Related

  1. checkWinPossibility(int[][] board, Point p1, Point p2, int playerID)
  2. circleLineIntersection(double theta, double r, double h, double k, Point2D[] result)
  3. clacGradient(Point2D p1, Point2D p2, Point2D p3)
  4. cleanList(List blueList, Polygon borders)
  5. colinearPoint(Line2D line, Point2D point, double distance)
  6. computeAngleRAD(@Nonnull Point2D p)
  7. computeBarycenter(Map values)
  8. computeEuclideanDistance(Point2D point1, Point2D point2)
  9. computePolygonArea(Point2D[] pts)