【问题标题】:determine angle between two points on a circle with respect to center确定圆上两点相对于中心的角度
【发布时间】:2013-05-09 11:07:06
【问题描述】:

我有一个 Ring(width 25px) 作为UIView。当用户选择环上的任何位置时,我想计算在考虑圆心的圆上固定点上选择的点之间的角度。 我发现了几个例子,但他们没有考虑到中心。

最佳方法是什么?

【问题讨论】:

    标签: iphone ios math trigonometry


    【解决方案1】:

    您必须自己处理代码(我是一名 Java 开发人员),但获取圆上两点之间的角度(相对于圆心测量)的最简单方法是调用情况的几何形状。圆周上任意两点和圆心所组成的三角形必然是isosceles

    回想一下,等腰三角形具有(至少)两条长度相同的边——两个点的径向线段。对角进行二等分得到径向线段,该径向线段垂直于并二等分连接两点的线。这形成了一对直角三角形,半径为斜边,两点之间距离的一半为“对”边。

    将因数 2 移到分母并识别半径的两倍,只需计算两点之间的距离(在圆周上),然后将其除以直径。你得到的值是半角的正弦(你想要整个角)。取反正弦,你会得到一半的角度:

    θ/2 = sin-1(d/D)

    d 为两点之间的距离,D 为圆的直径。由于给出了直径,并且两点之间的距离为simple to calculate,因此到达该点应该很容易,然后只需将计算出的值加倍即可得到两点之间的整个角度。

    【讨论】:

      【解决方案2】:

      这可能对你有帮助,我在我的一个项目中使用了同样的东西。

       -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
      {
           beginPoint = [[[event allTouches] anyObject] locationInView:self];
           currentAngle = 0.0f;
           centerPoint = self.center;
      }
      
      -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
      {
         endPoint = [[[event allTouches] anyObject] locationInView:self];
      
         float fromAngle = atan2(beginPoint.y - self.center.y, beginPoint.x - self.center.x);
         float toAngle = atan2(endPoint.y - self.center.y, endPoint.x - self.center.x);
         float newAngle = [self wrapd:currentAngle + (toAngle - fromAngle) min:0 max:2 * 3.14];
      
         currentAngle = newAngle;
      
         CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);
         [self setTransform:cgaRotate]; 
      }
      
      
      -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
      {
         endPoint = [[[event allTouches] anyObject] locationInView:self];
      
         float fromAngle = atan2(beginPoint.y - self.center.y, beginPoint.x - self.center.x);
         float toAngle = atan2(endPoint.y - self.center.y, endPoint.x - self.center.x);
         float newAngle = [self wrapd:currentAngle + (toAngle - fromAngle) min:0 max:2 * 3.14];
      
         currentAngle = newAngle;
      }
      
      - (double) wrapd:(double)_val min:(double)_min max:(double)_max {
           if(_val < _min) return _max - (_min - _val);
           if(_val > _max) return _val - _max; /*_min - (_max - _val)*/;
           return _val;
      }
      

      【讨论】:

      • 感谢帕万的回复。您能否也添加包装方法
      • [self wrapd:currentAngle + (toAngle - fromAngle) min:0 max:2 * 3.14];缺少此方法
      • 编辑的答案有那个方法
      【解决方案3】:

      众所周知 ((center.x + radius*cos(theta)) ,(center.y + radius*sin(theta))) ~ (X,Y)

      其中(X,Y)属于任意圆周点

      因此您可以计算任何圆周点的角度,即 theta:

      X= center.x + 半径*cos(theta)

      cos(theta) = (X - center.x)/radius .......... eqn-1

      类似

      Y= center.y + 半径*sin(theta)

      sin(theta) = (Y - center.y)/radius .......... eqn-2

      将 eqn-2 除以 eqn-1 我们有

      tan(theta) = (Y - center.y)/(X - center.x) -----------------最终方程

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多