iOS:给定一个圆圈得出分接点的角度

mm2*_*m24 2 objective-c computational-geometry ios

我有一个UIImageView显示一个分为六个相等三角形的圆圈,对应于:

  • area1在0-60度之间
  • area2在> 60-120度之间
  • area3在> 120-180度之间
  • area4在> 180-240度之间
  • area5在> 240-300度之间
  • area6在> 300-360度之间

圆圈类似于以下内容(原谅我糟糕的绘图):

在此输入图像描述

我想从水龙头所在区域的接触点得出.例如,如果用户点击圆圈的右上角,则该区域应为区域2:"> 60-120".

我得到的输入数据是:

  • 包含圆的框架的宽度和高度(例如200像素宽,200像素高)
  • 点击坐标

考虑到上述输入数据,有关如何推断抽头点落在哪个区域的任何建议?

Lyn*_*ott 8

我刚刚注意到这个解决方案中的一些错误我在评论中提到的,但它通常是你需要的......

我建议你的点击点和你的圆圈框架中心之间的角度然后实现一个 getArea功能,以便在您的圆圈内点击特定区域,例如:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    CGPoint tapPoint = [touch locationInView:self.view];
    CGFloat angle = [self angleToPoint:tapPoint];

    int area = [self sectionForAngle:angle];
}

- (float)angleToPoint:(CGPoint)tapPoint
{
    int x = self.circle.center.x;
    int y = self.circle.center.y;
    float dx = tapPoint.x - x;
    float dy = tapPoint.y - y;
    CGFloat radians = atan2(dy,dx); // in radians
    CGFloat degrees = radians * 180 / M_PI; // in degrees

    if (degrees < 0) return fabsf(degrees);
    else return 360 - degrees;
}

- (int)sectionForAngle:(float)angle
{
    if (angle >= 0 && angle < 60) {
        return 1;
    } else if (angle >= 60 && angle < 120) {
        return 2;
    } else if (angle >= 120 && angle < 180) {
        return 3;
    } else if (angle >= 180 && angle < 240) {
        return 4;
    } else if (angle >= 240 && angle < 300) {
        return 5;
    } else {
        return 6;
    }
}
Run Code Online (Sandbox Code Playgroud)