CGPoint旋转改变距离原点的距离

时间:2012-05-18 23:39:10

标签: iphone objective-c ios image-processing cgpoint

我想围绕另一个CGPoint(蓝色矩形)旋转一个CGPoint(红色矩形),但它改变了与原点的距离(蓝色矩形)......当我给出270°的角度时,它会在原点正上方创建点,但是当我给出了90作为角度值它降低了原点但是改变距离几乎是三倍....我想保持距离相同并且想要围绕另一个旋转CGPoint。请指导任何旋转cgpoints的方法......

distance = 100;
angle = 270*M_PI/180;    
rotatedPoint.x = initialPoint.x+distance*cos(angle);
rotatedPoint.y = initialPoint.y+distance*sin(angle);
    //rotatedPoint.x = initialPoint.x+tan(angle);

[test setCenter:rotatedPoint];   
[test setBackgroundColor:[UIColor redColor]];

由于

1 个答案:

答案 0 :(得分:2)

CGAffineTransform在旋转,平移和缩放方面是一个方便的工具。要确保正确旋转点,必须将其平移到原点,旋转它,然后将其平移。

要完成转换,请执行以下操作:

CGPoint pointToRotate = CGPointMake(30, 30);
float angleInRadians = DEGREES_TO_RADIANS(90);
CGPoint distanceFromOrigin = CGPointMake(0 - pointToRotate.x, 0 - pointToRotate.y);

CGAffineTransform translateToOrigin = CGAffineTransformMakeTranslation(distanceFromOrigin.x, distanceFromOrigin.y);
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(angleInRadians);
CGAffineTransform translateBackFromOrigin = CGAffineTransformInvert(translateToOrigin);

CGAffineTransform totalTransform = CGAffineTransformConcat(translateToOrigin, rotationTransform);
totalTransform = CGAffineTransformConcat(totalTransform, translateBackFromOrigin);

pointToRotate = CGPointApplyAffineTransform(pointToRotate, totalTransform);

以下是CGAffineTransform的文档,如果您想进一步查看:http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CGAffineTransform/Reference/reference.html

如果您无法解决问题,请告诉我其他任何事情!