1

我很难理解如何计算结的圆周到圆心的数学。我希望你们能给我一些指导。

当前计算将结 img 设置在饼图的中间,希望将其移到更靠近外圈的位置,如 img 2

感谢您的观看和评论,任何意见表示赞赏。

在此处输入图像描述

我想怎么样。 在此处输入图像描述

/** Draw a white knob over the circle **/
-(void) drawTheHandle:(CGContextRef)ctx{

    CGContextSaveGState(ctx);
    NSLog(@"handleCenterA.x %f",handleCenterA.x);
    NSLog(@" handleCenterA.y %f", handleCenterA.y);
    [[UIColor colorWithWhite:1.0 alpha:1.0]set];
    UIImage *myImage = [UIImage imageNamed:@"clock-marker.png"];

    //this will give me the result of image 1
    [myImage drawInRect:CGRectMake(handleCenterA.x-35, handleCenterA.y-40, TB_BUTTON_WIDTH, TB_BUTTON_WIDTH)];

    //this will give me the result of image 2
    [myImage drawInRect:CGRectMake(handleCenterA.x-35, handleCenterA.y-40, TB_BUTTON_WIDTH, TB_BUTTON_WIDTH)];

    CGContextRestoreGState(ctx);
}

#pragma mark - Math -

/** Move the Handle **/
-(void)movehandle:(CGPoint)lastPoint{

    //Get the center
    CGPoint centerPoint = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);

    //Calculate the direction from a center point and a arbitrary position.
    //float currentAngle = AngleFromNorth(centerPoint, lastPoint, NO);
    //int angleInt = floor(currentAngle);


    //Calculate the direction from the center point to an arbitrary position.
    float currentAngle = AngleFromNorth(centerPoint, lastPoint, NO);
    int angleInt = 360 - floor(currentAngle);

    if (sliderLock == SliderLockedStart) {
        self.startAngle = angleInt;
    }
    //Redraw
    [self setNeedsDisplay];
}

- (CGPoint)centerPointFromAngel:(int)angleInt {
    CGPoint point = [self pointFromAngle:angleInt];
    point.x += TB_BUTTON_WIDTH/2;
    point.y += TB_BUTTON_WIDTH/2;
    return point;
}

- (CGFloat)distanceBetween:(CGPoint)p1 and:(CGPoint)p2 {
    CGFloat xDist = (p2.x - p1.x);
    CGFloat yDist = (p2.y - p1.y);
    return sqrt((xDist * xDist) + (yDist * yDist));
}

/** Given the angle, get the point position on circumference **/
-(CGPoint)pointFromAngle:(int)angleInt{

    //Circle center
    CGPoint centerPoint = CGPointMake(self.frame.size.width/2 - (TB_BUTTON_WIDTH/2), self.frame.size.height/2 - (TB_BUTTON_WIDTH/2));

    //The point position on the circumference
    CGPoint result;
    result.y = round(centerPoint.y + radius * sin(ToRad(-angleInt))) ;
    result.x = round(centerPoint.x + radius * cos(ToRad(-angleInt)));    
    return result;
}

//Sourcecode from Apple example clockControl 
//Calculate the direction in degrees from a center point to an arbitrary position.
static inline float AngleFromNorth(CGPoint p1, CGPoint p2, BOOL flipped) {
    CGPoint v = CGPointMake(p2.x-p1.x,p2.y-p1.y);
    float vmag = sqrt(SQR(v.x) + SQR(v.y)), result = 0;
    v.x /= vmag;
    v.y /= vmag;
    double radians = atan2(v.y,v.x);
    result = ToDeg(radians);
    return (result >=0  ? result : result + 360.0);
}
4

1 回答 1

0

终于解决了。

/** 给定角度,得到圆周上的点位置 **/

-(CGPoint)pointFromAngle:(int)angleInt{

    //Circle center//TB_BUTTON_WIDTH
    CGPoint centerPoint = CGPointMake(self.frame.size.width/2 - (TB_BUTTON_WIDTH/2), self.frame.size.height/2 - (TB_BUTTON_WIDTH/2));

    //The point position on the circumference radius
    CGPoint result;
    result.y = round(centerPoint.y + 120 * sin(ToRad(-angleInt))) ;
    result.x = round(centerPoint.x + 120 * cos(ToRad(-angleInt)));

        NSLog(@"centerPoint %@",NSStringFromCGPoint(centerPoint));
        NSLog(@"result %@",NSStringFromCGPoint(result));
    return result;
}
于 2013-04-26T04:27:29.980 回答