设置CPView框架以使其包含绘制的rect

时间:2013-12-17 18:43:39

标签: objective-c cocoa cappuccino objective-j

我在卡布奇诺的目标是调整CPView的框架,以便绘制矩形的每个点都被包裹起来。矩形是可旋转的,这使它变得棘手: here http://www.asawicki.info/files/aabb_of_rotated_rectangle.png

我明白在Cocoa中我会使用CGContextGetClipBoundingBox(context)。 我怎么会在卡布奇诺处理这样的问题?

编辑:根据亚历山大的提示,这就是我的问题的答案:

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

1 个答案:

答案 0 :(得分:1)

如果myPath是描述旋转的矩形的CGPath,则可以使用以下方法获取边界矩形:

CGPathGetBoundingBox(myPath)