通过角度求圆上点

自定义view, 坐标的(0,0)是在左上角,等于第四象限。
需求是 onTouchEvent 的点,都需要在一个圆环上,不合格的点需要计算相同角度的且在圆上的点

 /**
   * 坐标系是倒立的 , cos 0°-180°区间
   * @param point
   * @return
   */
public static double getAngle (Point point,int radius ,Point center){
 
    getRingPoint(point,radius,center);
    double argument = Math.acos((point.y - center.y)*1.0/radius);
    double angleDegrees = Math.toDegrees(argument);
    if (point.x > center.x){
      angleDegrees = 180 - angleDegrees;
    }else {
      angleDegrees += 180;
    }
    Log.d("RingPointMapper", "getAngle: "+angleDegrees);
    return angleDegrees;

  }


/**
   * 换取  角度相同 半径121上的点
   * @param point 原始点,不应该是在圆上
   * @param radius 半径
   * @param center 圆心
   * @return
   */
  private static Point getRingPoint(Point point,int radius ,Point center){
    double multiple = Math.sqrt(Math.pow(radius, 2) / (Math.pow(point.x - center.x, 2) + Math.pow(point.y - center.y, 2)));
    point.x = (int) Math.abs(point.x * multiple  - center.x *(multiple-1));
    point.y = (int) Math.abs(point.y * multiple -  center.y *(multiple-1));
    return point;
  }

初始化时,还需要通过设置角度,求出圆上的点
 

public static Point getPointByAngle(int angle,int radius ,Point center){
    Point point = new Point();
    point.y = (int) (center.y - radius * Math.cos(Math.toRadians(angle)));
    point.x = (int) (center.x + radius * Math.sin(Math.toRadians(angle)));
    return point;
  }

如果有想要自定义view,圆环取色器的可以留言。


版权声明:本文为Sunshine_Cui001原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。