Calculating angle between two points - java
Asked Answered
J

3

10

I need to calculate the angle in degrees between two points, with a fixed point that is connected with the given two points by a line.

Here is an image that illustrates what I need:

enter image description here

Here is what I have tried so far:

public static float GetAngleOfLineBetweenTwoPoints(float x1, float x2, float y1, float y2) {
        float xDiff = x2 - x1;
        float yDiff = y2 - y1;
        return (float) (Math.atan2(yDiff, xDiff) * (180 / Math.PI));
}

It's pointless to say that it doesn't provide the correct answer.

Jurgen answered 27/9, 2014 at 16:18 Comment(4)
You do not even consider the coordinates of the "origin" point currently, right?Centaury
what is your fixed point?you also need that pointEquanimity
add third point (as @getlost mentioned) and use vector angle formula: vitutor.com/geometry/vec/angle_vectors.htmlClever
From @RomeoKienzler post (because it's not an answer), you should review this: How to calculate the angle between a line and the horizontal axis?Anethole
F
18

You can have the following method that calculates the angle in radians using the Math.atan2 method:

public static double angleBetweenTwoPointsWithFixedPoint(double point1X, double point1Y, 
        double point2X, double point2Y, 
        double fixedX, double fixedY) {

    double angle1 = Math.atan2(point1Y - fixedY, point1X - fixedX);
    double angle2 = Math.atan2(point2Y - fixedY, point2X - fixedX);

    return angle1 - angle2; 
}

And call it with three points (using Math.toDregrees to transform resulting angle from radians to degrees):

System.out.println(Math.toDegrees(
            angleBetweenTwoPointsWithFixedPoint(0, 0, // point 1's x and y
                                                1, 1, // point 2
                                                1, 0  // fixed point
                                               )));

Output: 90.0

Feel free to use Java's standard Point or Line2D classes in your solution though. This was just to demonstrate it works.

Fastigiate answered 27/9, 2014 at 16:50 Comment(1)
To make this work properly I had to add a correction of the result (after converting it to degrees): if (result < 0) result += 360; Otherwise my result was switching between ~270 and ~90 degrees (with a fixed point in the bottom right corner).Willemstad
A
7

Here is a code snippet from my Android Gesture library. It works and is fully tested.

public double getAngleFromPoint(Point firstPoint, Point secondPoint) {

    if((secondPoint.x > firstPoint.x)) {//above 0 to 180 degrees

        return (Math.atan2((secondPoint.x - firstPoint.x), (firstPoint.y - secondPoint.y)) * 180 / Math.PI);

    }
    else if((secondPoint.x < firstPoint.x)) {//above 180 degrees to 360/0

        return 360 - (Math.atan2((firstPoint.x - secondPoint.x), (firstPoint.y - secondPoint.y)) * 180 / Math.PI);

    }//End if((secondPoint.x > firstPoint.x) && (secondPoint.y <= firstPoint.y))

    return Math.atan2(0 ,0);

}//End public float getAngleFromPoint(Point firstPoint, Point secondPoint)
Aram answered 11/5, 2015 at 7:55 Comment(2)
man, this is actually the only valid answer I found in Stack Overflow. All others are not working! Thanks a lot!Bactria
How can i get the value of xSouthwick
S
1

I don't know @user2288580 but even for simple, test-cases your code is failing.

firstPoint = (0,0) secondPoint = (0, 5), (5,5), (5,0), (5, -5) (0, -5) (-5, -5), (-5, 0)

Please see if this works for you @David -

public double angleBetween2CartesianPoints(double firstX, double firstY, double secondX, double secondY) {
    double angle = Math.atan2((secondX - firstX), (secondY - firstY)) * 180 / Math.PI;
    if (angle < 0) {
        return (360 + angle);
    } else {
        return (angle);
    }
}
Shortcake answered 29/9, 2015 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.