• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Drawing rounded rect
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Drawing rounded rect (From: Youngjin Kim <email@hidden>)

  • Prev by Date: Re: SCM?
  • Next by Date: Re: Notifications
  • Previous by thread: Re: Drawing rounded rect
  • Next by thread: SCM?
  • Index(es):
    • Date
    • Thread