5 // Created by Pieter de Bie on 24-09-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "RoundedRectangle.h"
12 @implementation NSBezierPath (RoundedRectangle)
13 + (NSBezierPath *)bezierPathWithRoundedRect: (NSRect) aRect cornerRadius: (double) cRadius
15 double left = aRect.origin.x, bottom = aRect.origin.y, width = aRect.size.width, height = aRect.size.height;
17 //now, crop the radius so we don't get weird effects
18 double lesserDim = width < height ? width : height;
19 if ( cRadius > lesserDim / 2 )
21 cRadius = lesserDim / 2;
24 //these points describe the rectangle as start and stop points of the
25 //arcs making up its corners --points c, e, & g are implicit endpoints of arcs
27 NSPoint a = NSMakePoint( 0, cRadius ), b = NSMakePoint( 0, height - cRadius ),
28 d = NSMakePoint( width - cRadius, height ), f = NSMakePoint( width, cRadius ),
29 h = NSMakePoint( cRadius, 0 );
31 //these points describe the center points of the corner arcs
32 NSPoint cA = NSMakePoint( cRadius, height - cRadius ),
33 cB = NSMakePoint( width - cRadius, height - cRadius ),
34 cC = NSMakePoint( width - cRadius, cRadius ),
35 cD = NSMakePoint( cRadius, cRadius );
38 NSBezierPath *bp = [NSBezierPath bezierPath];
41 [bp appendBezierPathWithArcWithCenter: cA radius: cRadius startAngle:180 endAngle:90 clockwise: YES];
43 [bp appendBezierPathWithArcWithCenter: cB radius: cRadius startAngle:90 endAngle:0 clockwise: YES];
45 [bp appendBezierPathWithArcWithCenter: cC radius: cRadius startAngle:0 endAngle:270 clockwise: YES];
47 [bp appendBezierPathWithArcWithCenter: cD radius: cRadius startAngle:270 endAngle:180 clockwise: YES];
50 //Transform path to rectangle's origin
51 NSAffineTransform *transform = [NSAffineTransform transform];
52 [transform translateXBy: left yBy: bottom];
53 [bp transformUsingAffineTransform: transform];
55 return bp; //it's already been autoreleased