2012-05-18 79 views
3

我要旋轉CGPoint(紅色矩形)周圍其他CGPoint(藍色矩形),但它從原點(藍色矩形)的距離改變的距離......當我在角度給予270它創建點正好在原點之上,但是當我給出角度值90時,它會降低原點,但更改距離幾乎等於三倍......我想保持距離相同並且想要圍繞另一個角度旋轉CGPoint。請指導任何方法對cgpoints的旋轉...CGPoint旋轉改變從原點

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]]; 

感謝

回答

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

請讓如果這不能解決您的問題,我知道您是否需要其他任何東西!