验证圈子的部门

时间:2013-08-19 11:48:12

标签: ios

我使用以下代码制作了圆圈

//Color Declaration
    UIColor *color = [UIColor colorWithRed:0 green:0.429 blue:0 alpha:1];

    //Drawing Circle
    CGRect circleRect = CGRectMake(20, 20, 170, 170);
    UIBezierPath *circlePath = [UIBezierPath bezierPath];
    [circlePath addArcWithCenter:CGPointMake(CGRectGetMidX(circleRect), CGRectGetMidY(circleRect))
                          radius:CGRectGetWidth(circleRect)/2 startAngle:0 * M_PI/180
                        endAngle:289 * M_PI/180
                       clockwise:YES];
    [circlePath addLineToPoint:CGPointMake(CGRectGetMidX(circleRect), CGRectGetMidY(circleRect))];
    [circlePath closePath];


    [color setFill];
    [circlePath fill];

我已使用以下代码验证带有圆形区域的触摸代码,

- (BOOL)validatePoint:(CGPoint)myPoint
{
    // calculate how far from centre we are with Pythagorean
    // √ a2 + b2
    CGFloat a = abs(myPoint.x - (self.view.bounds.size.width/2));
    CGFloat b = abs(myPoint.y - (self.view.bounds.size.height/2));
    CGFloat distanceFromCentre = sqrt(pow(a,2) + pow(b,2));

    if((distanceFromCentre > self.minRadiusSize) && (distanceFromCentre < self.maxRadiusSize)){
        return YES;
    }else{
        // not inside doughnut
        return NO;
    }
}

我已经使用上面的代码验证了圆圈。当上面的代码为真时,我们可以为Circle添加触摸。

同样,我必须验证Circle的扇区(部分) 我的要求是,我必须验证Circle of Sector。我是否必须检测绘制圆圈的扇区(部分)。 我有公式                 [Area of Sector = ½ × (θ × π/180) × r2 (when θ is in degrees)]

1 个答案:

答案 0 :(得分:0)

如果您要做的只是检查点是否在圆圈区域内,那么您应该使用相同的路径来测试点

BOOL isInsideSector = [circlePath containsPoint:myPoint];