Calculating the angle between three points in android
Asked Answered
K

2

1

I have trouble understanding what's wrong with my code.

Point A = new Point((int)CENTER_X, (int)CENTER_Y);
Point B = new Point((int)me.getX(), (int)me.getY());
Point C = new Point((int)CENTER_X, (int)B.y);
double AB;
double AC;
double BC;
AB = Math.sqrt(Math.pow(B.x - A.x, 2) + Math.pow(B.y - A.y, 2));
AC = Math.sqrt(Math.pow(C.x - A.x, 2) + Math.pow(C.y - A.y, 2));
BC = Math.sqrt(Math.pow(C.x - B.x, 2) + Math.pow(C.y - B.y, 2));
degre = (AB * AB - AC * AC - BC * BC) /( 2 * AC * AB);
degre = Math.acos(degre*(180/Math.PI));

I am always getting degre = 0 or NaN. Why is that ?

Krishnakrishnah answered 20/4, 2012 at 9:35 Comment(0)
G
2

You are confused about when to do the radian to degree translation, you need to calculate the ratio, then do the arccos (which will return an angle in radians), then convert to degrees like so:

double float ratio = (AB * AB + AC * AC - BC * BC) /( 2 * AC * AB);
degre = Math.acos(ratio)*(180/Math.PI);
Gloss answered 20/4, 2012 at 9:54 Comment(2)
the first line is sero... (AB * AB - AC * AC - BC * BC) /( 2 * AC * AB) am i using the wrong eq?Krishnakrishnah
I didn't check your trig but believe you are using the cosine rule correctly.Gloss
S
1

I know this is an old question but it helped me when looking at this so I am sharing a working Kotlin example and also explicitly showing the angle being calculated as I found this can cause confusion.

If you consider the three points as making a triangle then you can get the 'internal' angles of the triangle using the approach discussed in the question and answers here, but you do need to be careful you are focusing on the correct equation for the angle you want.

I found that the exact angle you are referring to can be confusing when looking at this, so its useful to be able to check your code against some known or working examples that allow you visualise the angle you want.

At the time of writing the link below is to an excellent online tool that allows you enter the three points in a triangle and compute, amongst other things, the internal angles. It also includes an explanation of how the calculations are done, with your example values plugged in, which can be very useful when debugging the code:

Below is tested Kotlin code to calculate angle at vertex 'b' in a triangle like this (does not have to equilateral) :

enter image description here

The Kotlin code is below - the logging is obviously not needed but is useful when using a tool like the above site to test results:

    private fun angleBetweenThreePoints(a: CirclePoint, b: CirclePoint, c:CirclePoint): Double {
        var ab:Double = Math.sqrt(Math.pow((a.x - b.x).toDouble(), 2.0) + Math.pow((a.y - b.y).toDouble(), 2.0))
        var ac:Double = Math.sqrt(Math.pow((a.x - c.x).toDouble(), 2.0) + Math.pow((a.y - c.y).toDouble(), 2.0))
        var bc:Double = Math.sqrt(Math.pow((b.x - c.x).toDouble(), 2.0) + Math.pow((b.y - c.y).toDouble(), 2.0))

        var cosValue = (ab * ab + bc * bc - ac * ac) /( 2 * bc * ab)
        val angle = acos(cosValue) *(180/Math.PI)

        //Optional logging to help test and debug
        Log.d(TAG,"ab: " + ab)
        Log.d(TAG,"ac: " + ac)
        Log.d(TAG,"bc: " + bc)
        Log.d(TAG,"a: " + a.x +"," + a.y)
        Log.d(TAG,"b: " + b.x +"," + b.y)
        Log.d(TAG,"c: " + c.x +"," + c.y)
        Log.d(TAG,"angle: " + angle)

        return angle
    }

Note that this does not test for edge cases like non-intersecting lines etc.

Spelter answered 17/9, 2020 at 11:38 Comment(1)
You saved so much of my time:)Finegrain

© 2022 - 2024 — McMap. All rights reserved.