【问题标题】:Calculating point coordinates from user tap with constraints使用约束从用户点击计算点坐标
【发布时间】:2014-12-18 14:20:25
【问题描述】:

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

输入:

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

期望的输出:

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

这个想法是从 P3 坐标创建一个名为 P2 的新坐标,如上所述 - 关键是 P2 到中心的距离应该与半径完全对应,而 ANGLE 应该与点击点。

有人能建议一个公式来计算给定点击的相应坐标吗?我只需要使用我拥有的输入来计算 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;
}

【问题讨论】:

    标签: ios objective-c iphone geometry coordinates


    【解决方案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

    一旦你有了上面的等式,你就需要解决它。结果将产生两个可能的点(线将在两个地方与圆相交),您正在寻找的点是更靠近点击点的点。在这种情况下,只需比较两个解之间到 P3 的距离,较短的距离将显示您所需的解 - P2。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多