0
我的目標是在卡布奇諾調整CPView的框架,以便繪製矩形的每個點都被包住。矩形是可旋轉的,這使得它很棘手: here http://www.asawicki.info/files/aabb_of_rotated_rectangle.png。設置CPView框架,使其包圍繪製矩形
我明白,在可可我會用CGContextGetClipBoundingBox(上下文)。 我將如何處理卡布奇諾的這種問題?
編輯:這是回答我的問題的基礎上,亞歷山大的提示:
var context = [[CPGraphicsContext currentContext] graphicsPort],
transform = CGAffineTransformRotate(CGAffineTransformMakeTranslation(CGRectGetWidth([self bounds])/2, CGRectGetHeight([self bounds])/2), rotationRadians);
CGContextBeginPath(context);
path2 = CGPathCreateMutable();
CGPathAddRect(path2, transform, CGRectMake(-newWidth/2, -newHeight/2, newWidth, newHeight));
CGContextAddPath(context, path2);
CGContextClosePath(context);
CGContextSetFillColor(context, [CPColor yellowColor]);
CGContextFillPath(context);
CGContextRestoreGState(context);
var frame = [self frame];
frame.size.width = CGRectGetWidth([self bounds]);
frame.size.height = CGRectGetHeight([self bounds]);
oldFrameWidth = frame.size.width;
oldFrameHeight = frame.size.height;
newFrameWidth = CGRectGetWidth(CGPathGetBoundingBox(path2));
newFrameHeight = CGRectGetHeight(CGPathGetBoundingBox(path2));
frame.size.width = newFrameWidth;
frame.size.height = newFrameHeight;
frame.origin.x -= (newFrameWidth - oldFrameWidth)/2;
frame.origin.y -= (newFrameHeight - oldFrameHeight)/2;
[self setFrame:frame];
Alex,我目前有正確的邊界CPView的尺寸(請參閱上面的代碼),但繪製的矩形的中心位於CPView的左上角。我怎樣才能把它帶到CPView的中心? – saeppi
那是因爲你正在用這種方式繪製你的矩形,在(-newWidth/2,newHeight/2)。如果要在中心繪製它,則可以將轉換添加到邊界寬度和高度的一半的變換中。 –
謝謝。如何將翻譯添加到變換而不覆蓋旋轉? – saeppi