Here you can find the source of computeAngleDEG(@Nonnull Point2D p1, @Nonnull Point2D p2)
Note: Goes CCW from south to east to north to west, etc.
Parameter | Description |
---|---|
p1 | the first Point2D |
p2 | the second Point2D |
@CheckReturnValue public static double computeAngleDEG(@Nonnull Point2D p1, @Nonnull Point2D p2)
//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()); } }