getAngleForPoint method

double getAngleForPoint(
  1. double x,
  2. double y
)

returns the angle relative to the chart center for the given point on the chart in degrees. The angle is always between 0 and 360°, 0° is NORTH, 90° is EAST, ...

@param x @param y @return

Implementation

double getAngleForPoint(double x, double y) {
  MPPointF c = getCenterOffsets();

  double tx = x - c.x, ty = y - c.y;
  double length = sqrt(tx * tx + ty * ty);
  double r = acos(ty / length);

  double angle = r * 180.0 / pi;

  if (x > c.x) angle = 360 - angle;

  // add 90° because chart starts EAST
  angle = angle + 90;

  // neutralize overflow
  if (angle > 360) angle = angle - 360;

  MPPointF.recycleInstance(c);

  return angle;
}