圆几何上的点

时间:2014-04-17 07:18:47

标签: ios math

enter image description here

我想实现以下内容:

我在圆圈上有两个可触摸的物体。

如果我触摸并移动红色框中的物体,我希望左边的物体沿着蓝色圆圈相应地移动 - 好像它是在第一个物体之后用透明绳索拉动的。

2 个答案:

答案 0 :(得分:2)

这应该是诀窍,按钮将位于按钮Finger的芝麻轴上。如果您想要另一个,请向touchAngle添加偏移量:

float    centerX; //Center of your Circle on X
float    centerY; //Center of your Circle on Y
float  touchAngle; //Angle of the touch
int    touchHash;
XxX    button; //Your "button" to be kept in the bluecircle
XxX    buttonFinger; //Your "button" that follow your finger

int    maxRadius; //The maximum radius: from center to the end of the blue circle
int    minRadius; //The minimum radius: from center to the beginning of the blue circle
CGRect CGRectPantone; //The CGRect of your available touch zone

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     NSSet *allTouches = [event allTouches];
     for (UITouch *touch in allTouches)
     {
         CGPoint touchLocation = [touch locationInView:self.view];
         if (CGRectContainsPoint(CGRectPantone, touchLocation))
         {
            touchHash = [touch hash];
            buttonFinger.center = touchLocation;
         }
      }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSSet *allTouches = [event allTouches];
    for (UITouch *touch in allTouches)
    {           
    if ([touch hash] == touchHash)
    {
        CGPoint touchLocation = [touch locationInView:self.view];
        if (CGRectContainsPoint(CGRectPantone, touchLocation))
        {
            buttonFinger.center = touchLocation;
            float dx = centerX - (float)touchLocation.x;
            float dy = centerY - (float)touchLocation.y;
            touchAngle = -atan2f(dx, dy)+(M_PI/2.0);

            button.center = CGPointMake(centerX-((minRadius+(maxRadius-minRadius)/2.0)*cosf(touchAngle)), centerY-((minRadius+(maxRadius-minRadius)/2.0)*sinf(touchAngle)));
        }
    }
}

答案 1 :(得分:0)

基本上这取决于角度。例如,在图片中,第一个按钮在圆圈中的角度为0度,而第二个按钮的角度为270度。您必须在移动后测量第一个按钮的角度alpha,并将第二个按钮旋转到位置270 + alpha mod 360