使用约束从用户点击计算点坐标

时间:2014-12-18 14:20:26

标签: ios objective-c iphone geometry coordinates

我正在尝试计算与点击位置对应的圆圈的坐标。坐标应位于最靠近抽头位置的圆的边界上(例如,距离半径较远的边界)。为了方便这一点,我只检测距圆心80%半径的水龙头。

enter image description here

输入:

  • P(GPPoint) - 圆心
  • P1(GPPoint)显示图像的当前位置
  • r(浮动)圆的半径
  • P3(CGPoint)用户点击坐标

期望的输出:

P2(CGPoint) - 对应于P3但沿着圆的图像的新坐标。对不起的解释感到抱歉,我试着解释一下:一旦用户点击屏幕,我想在P2中移动图像。 P2应该通过将P2移动到圆的边界来导出。应该可以通过使用半径信息来实现。

我们的想法是从P3坐标创建一个名为P2的新坐标,如上所述 - 关键是与中心的P2距离应与半径完全对应,角度应与tapPoint。

任何人都可以建议一个公式来计算给定水龙头的相应坐标吗?我只需要使用我的输入来计算P3。

到目前为止

代码:

-(void)tapInImageView:(UITapGestureRecognizer *)tap
{
    CGPoint tapPoint = [tap locationInView:tap.view];

    if ([self isInOuternCircle:tapPoint]) {

       // then create from tapPoint coordinates a new coordinate P2 as described above - but have no idea how.. the key is that P2   distance from the centre should correspond exactly to the radius and the ANGLE should be the same as tapPoint.
    }
}

-(BOOL)isInOuternCircle:(CGPoint)point
{
    double distanceToCenter = sqrt((point.x - _timerView.center.x)*(point.x - _timerView.center.x) + (point.y - _timerView.center.y)*(point.y - _timerView.center.y));

    if (distanceToCenter < _innerCircleRadius) {
        return false;
    } 
    return true;
}

1 个答案:

答案 0 :(得分:1)

之前我曾经做过这个,但数学通常取决于你如何设置你的坐标系,所以我只是简要介绍一下我做了什么。您需要一些几何体和一些公式来确定沿圆的新坐标。

  1. 使用以下方法计算通过中心(P)和您的分接点(P3)的线的公式:http://en.wikipedia.org/wiki/Linear_equation#Two-point_form
  2. 确定圈子的等式:http://en.wikipedia.org/wiki/Circle#Equations
  3. 使用上述两个等式,您将拥有一个线性和二次方程的系统:http://www.mathsisfun.com/algebra/systems-linear-quadratic-equations.html
  4. 如果你有上面的等式,你需要解决它。结果将产生两个可能的点(线将在两个位置与圆相交),并且您要寻找的点是更靠近分接点的点。在这种情况下,只需比较两个解决方案之间的距离P3,较短的距离将显示您所需的解决方案 - P2。