Re: Round Rect
Re: Round Rect
- Subject: Re: Round Rect
- From: Andy Lee <email@hidden>
- Date: Wed, 29 Mar 2006 11:59:39 -0500
Yet another example implementation, again as a category on
NSBezierPath, can be found in the Reducer example. Besides doing the
drawing, it checks for a radius that is too big.
I found this by searching for "rounded rectangle" in Xcode's
Documentation window.
--Andy
==========
<http://developer.apple.com/samplecode/Reducer/listing21.html>
@implementation NSBezierPath (RoundRect)
+ (NSBezierPath *)bezierPathWithRoundedRect:(NSRect)rect cornerRadius:
(float)radius {
NSBezierPath *result = [NSBezierPath bezierPath];
[result appendBezierPathWithRoundedRect:rect cornerRadius:radius];
return result;
}
- (void)appendBezierPathWithRoundedRect:(NSRect)rect cornerRadius:
(float)radius {
if (!NSIsEmptyRect(rect)) {
if (radius > 0.0) {
// Clamp radius to be no larger than half the rect's width or
height.
float clampedRadius = MIN(radius, 0.5 * MIN(rect.size.width,
rect.size.height));
NSPoint topLeft = NSMakePoint(NSMinX(rect), NSMaxY(rect));
NSPoint topRight = NSMakePoint(NSMaxX(rect), NSMaxY(rect));
NSPoint bottomRight = NSMakePoint(NSMaxX(rect), NSMinY(rect));
[self moveToPoint:NSMakePoint(NSMidX(rect), NSMaxY(rect))];
[self appendBezierPathWithArcFromPoint:topLeft
toPoint:rect.origin radius:clampedRadius];
[self appendBezierPathWithArcFromPoint:rect.origin
toPoint:bottomRight radius:clampedRadius];
[self appendBezierPathWithArcFromPoint:bottomRight
toPoint:topRight radius:clampedRadius];
[self appendBezierPathWithArcFromPoint:topRight
toPoint:topLeft radius:clampedRadius];
[self closePath];
} else {
// When radius == 0.0, this degenerates to the simple case of
a plain rectangle.
[self appendBezierPathWithRect:rect];
}
}
}
@end
--Andy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >Round Rect (From: Valerio Ferrucci <email@hidden>) |