1

円の中心までの結び目の円周を計算する方法の背後にある数学を理解するのが難しい. 皆さんが私に何か指針を与えてくれることを願っています。

現在の計算では、結び目をパイの真ん中に設定し、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 に答える