Re: Drawing rounded rect
Re: Drawing rounded rect
- Subject: Re: Drawing rounded rect
- From: "John C. Randolph" <email@hidden>
- Date: Tue, 28 Aug 2001 11:33:16 -0700
On Tuesday, August 28, 2001, at 06:47 AM, Youngjin Kim wrote:
Hi,
Is there any convenient call for drawing good old rounded rectangles?
I'm doing it by creating a bezierpath from rect then drawing it with
rounded line join style. but I'm wondering if there's better(simpler)
alternatives.
I did it like this, in a category of NSBezierPath:
+ (NSBezierPath *) bezierPathWithRoundRectInRect:(NSRect)rect
radius:(float) radius;
/*"This method adds the traditional Macintosh rounded-rectangle to
NSBezierPath's repertoire. Take care that radius is smaller than half
the height or width of rect, or some peculiar artifacts may result."*/
{
NSRect
innerRect = NSInsetRect(rect, radius, radius);
NSBezierPath
*path = [self bezierPath];
[path moveToPoint:rect.origin];
[path appendBezierPathWithArcWithCenter: bottomLeftOfRect(innerRect)
radius: radius startAngle: 180.0 endAngle:270.0];
[path relativeLineToPoint:NSMakePoint(NSWidth(innerRect), 0.0)];
[path appendBezierPathWithArcWithCenter: bottomRightOfRect(innerRect)
radius: radius startAngle: 270.0 endAngle: 360.0];
[path relativeLineToPoint:NSMakePoint(0.0, NSHeight(innerRect))];
[path appendBezierPathWithArcWithCenter: topRightOfRect(innerRect)
radius: radius startAngle: 0.0 endAngle: 90.0];
[path relativeLineToPoint:NSMakePoint( - NSWidth(innerRect), 0.0)];
[path appendBezierPathWithArcWithCenter:topLeftOfRect(innerRect)
radius: radius startAngle: 90.0 endAngle: 180.0];
[path closePath];
return path;
}
And it depends on these functions:
NSPoint centerOfRect(NSRect rect) { return NSMakePoint(NSMidX(rect),
NSMidY(rect)); }
NSPoint topCenterOfRect(NSRect rect) { return NSMakePoint(NSMidX(rect),
NSMaxY(rect)); }
NSPoint topLeftOfRect(NSRect rect) { return NSMakePoint(NSMaxX(rect),
NSMaxY(rect)); }
NSPoint topRightOfRect(NSRect rect) { return NSMakePoint(NSMinX(rect),
NSMaxY(rect)); }
NSPoint leftCenterOfRect(NSRect rect) { return
NSMakePoint(NSMinX(rect), NSMidY(rect)); }
NSPoint bottomCenterOfRect(NSRect rect) { return
NSMakePoint(NSMidX(rect), NSMinY(rect)); }
NSPoint bottomLeftOfRect(NSRect rect) { return rect.origin; }
NSPoint bottomRightOfRect(NSRect rect) { return
NSMakePoint(NSMidX(rect), NSMinY(rect)); }
NSPoint rightCenterOfRect(NSRect rect) { return
NSMakePoint(NSMidX(rect), NSMidY(rect)); }
-jcr
"Scientology is both immoral and socially obnoxious... it is
corrupt, sinister and dangerous." - Mr. Justice Latey, London 1984