问题
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 ?
回答1:
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);
来源:https://stackoverflow.com/questions/10243663/calculating-the-angle-between-three-points-in-android