Skip to content

Instantly share code, notes, and snippets.

View DavidBoyes's full-sized avatar

David Boyes DavidBoyes

View GitHub Profile
@DavidBoyes
DavidBoyes / gist:5290376
Created April 2, 2013 06:48
Returns a rectangle which is the largest that can fit inside a rotated rectangle
- (CGRect)cropRectForRectSize:(CGSize)rectSize rotation:(float)radians {
int quadrant = ((int)floor(radians / (M_PI / 2))) & 3;
float sign_alpha = ((quadrant & 1) == 0) ? radians : M_PI - radians;
float alpha = fmod((fmod(sign_alpha, M_PI) + M_PI), M_PI);
CGSize bb = CGSizeMake(rectSize.width * cos(alpha) + rectSize.height * sin(alpha), rectSize.width * sin(alpha) + rectSize.height * cos(alpha));
float gamma = rectSize.width < rectSize.height ? atan2(bb.width, bb.height) : atan2(bb.height, bb.width);
float delta = M_PI - alpha - gamma;
float length = rectSize.width < rectSize.height ? rectSize.height : rectSize.width;