0

I have two points (touch positions) and I want to know angle between them relative to image center.

Ilustration

After googling for a while, I found Law of cosines.

So I came up with the following code:

final float x = e.getX() - v.getWidth() / 2;
final float y = e.getY() - v.getHeight() / 2;

final float a = PointF.length(x, y);
final float b = PointF.length(x_, y_);
final double c = Math.sqrt(Math.pow(x - x_, 2) + Math.pow(y - y_, 2));

final double alpha = Math.toDegrees(Math.acos((Math.pow(a , 2) + Math.pow(b, 2) - Math.pow(c, 2)) / (2 * a * b)));

It seems to work. But it has one big issue. The angle is always positive. But I need to be able to recognize clockwise and counterclockwise movement. So from point 1 to point 2 in illustration would be +45 but from 2 to 1 would be -45. Any idea how to do that?

Pitel
  • 5,334
  • 7
  • 45
  • 72

1 Answers1

-1

you'll need to distinguish between the first and second click. and see how they are relative to each other. Above/Left, Below/Right etc then use that according to your reference point. the angle doesn't care about the order of clicked. clock/countercheck do care

Basically if you are above the center point you'll need to check which point is to the left of the other point. and if you are under the center you'll need to check which is to the right. it all really depends on how yo define the clockwise.

Roee N
  • 502
  • 2
  • 4
  • 17