我试图旋转我的矩形,但它不能正常工作。
这是我的代码的一部分,我在另一篇文章中找到了它:
#define DEGREES_TO_RADIANS(x) (M_PI * x / 180.0)
-(void)drawRect:(NSRect)rect
{
CGContextRef cRef = [[NSGraphicsContext currentContext] graphicsPort];
// points[] - this is a CGPoints array, length_one is a double value
CGRect rect_one = CGRectMake(points[0].x, points[0].y, (CGFloat)length_one, 40);
// I've print out the origin of old one and new one
NSLog(@"old rect -- %f, %f", rect_one.origin.x, rect_one.origin.y);
float centerX = rect_one.origin.x + (rect_one.size.width / 2.0);
float centerY = rect_one.origin.y + (rect_one.size.height / 2.0);
CGAffineTransform rotation = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(10));
CGAffineTransform moveAnchor = CGAffineTransformMakeTranslation(centerX, centerY);
CGAffineTransform centeredRotation = CGAffineTransformConcat(moveAnchor, rotation);
CGRect rotatedRect = CGRectApplyAffineTransform(rect_one, centeredRotation);
CGContextAddRect(cRef, rotatedRect);
// new one
NSLog(@"new rect -- %f, %f", rotatedRect.origin.x, rotatedRect.origin.y);
}
即使我无法从视图中找到我的新矩形,原点也发生了很大变化。旧原点是 (x = 263.3, y = 502.8),新原点是 (x=506.1, y=1132.0) 这个系统是如何工作的,尤其是如何分配旋转角度?如果可能的话,你们能帮我简要解释一下吗?非常感谢!!!!