objective-ccocoacappuccinoobjective-j

Setting CPView frame so that it encases drawn rect


my goal in Cappuccino is to adjust the CPView's frame so that every point of a drawn rect is encased. The rect is rotatable which makes it tricky: here
(source: asawicki.info)
.

I understand that in Cocoa I'd use CGContextGetClipBoundingBox(context). How would I handle such a problem in Cappuccino?

Edit: That's the answer to my problem, based on Alexander's tips:

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

Solution

  • If myPath is a CGPath describing your rotated rect, you can get the bounding rectangle using:

    CGPathGetBoundingBox(myPath)