【发布时间】: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