Re: Drawing elliptical curve using NSBezierPath
Re: Drawing elliptical curve using NSBezierPath
- Subject: Re: Drawing elliptical curve using NSBezierPath
- From: Gregory Weston <email@hidden>
- Date: Wed, 20 Dec 2006 20:28:20 -0500
On Dec 20, 2006, at 2:38 PM, Shawn Erickson wrote:
On 12/20/06, Vinay Prabhu <email@hidden> wrote:
I am trying to draw elliptical arc using NSBezierPath.
The arc must vary from Ellipse to Rect. Please see the attached
image. The
blue curve is the elliptical curve and the bounding rectangle is
stroked
with red color.
On windows, the arc is drawn using the addArc method.
Is there any direct replacement for the addArc method on Mac to draw
elliptical curve?
Review the following section in the Cocoa drawing guide...
<http://developer.apple.com/documentation/Cocoa/Conceptual/
CocoaDrawingGuide/Paths/chapter_6_section_1.html>
...in particular for your quesiton...
<http://developer.apple.com/documentation/Cocoa/Conceptual/
CocoaDrawingGuide/Paths/chapter_6_section_5.html#//apple_ref/doc/
uid/TP40003290-CH206-BBCBGFBH>
But those are circular arcs and the OP asked about ellipses. I didn't
see the sample image because it got scrubbed from the message in
digest mode, but on the off chance the OP was looking to do the
traditional Mac OS roundrect shape, I've got a category on
NSBezierPath to do it.
@interface NSBezierPath (RoundRect)
+ (NSBezierPath*)bezierPathWithRoundRect:(NSRect)inRect
xRadius:(float)inRadiusX
yRadius:(float)inRadiusY;
@end
@implementation NSBezierPath (RoundRect)
+ (NSBezierPath*)bezierPathWithRoundRect:(NSRect)inRect
xRadius:(float)inRadiusX
yRadius:(float)inRadiusY
{
const float kEllipseFactor = 0.55228474983079;
float theMaxRadiusX = NSWidth(inRect) / 2.0;
float theMaxRadiusY = NSHeight(inRect) / 2.0;
float theRadiusX = (inRadiusX < theMaxRadiusX) ? inRadiusX :
theMaxRadiusX;
float theRadiusY = (inRadiusY < theMaxRadiusY) ? inRadiusY :
theMaxRadiusY;
float theControlX = theRadiusX * kEllipseFactor;
float theControlY = theRadiusY * kEllipseFactor;
NSRect theEdges = NSInsetRect(inRect, theRadiusX, theRadiusY);
NSBezierPath* theResult = [NSBezierPath bezierPath];
// Lower edge and lower-right corner
[theResult moveToPoint:NSMakePoint(theEdges.origin.x,
inRect.origin.y)];
[theResult lineToPoint:NSMakePoint(NSMaxX(theEdges),
inRect.origin.y)];
[theResult curveToPoint:NSMakePoint(NSMaxX(inRect),
theEdges.origin.y)
controlPoint1:NSMakePoint(NSMaxX(theEdges) +
theControlX, inRect.origin.y)
controlPoint2:NSMakePoint(NSMaxX(inRect),
theEdges.origin.y - theControlY)];
// Right edge and upper-right corner
[theResult lineToPoint:NSMakePoint(NSMaxX(inRect), NSMaxY
(theEdges))];
[theResult curveToPoint:NSMakePoint(NSMaxX(theEdges), NSMaxY
(inRect))
controlPoint1:NSMakePoint(NSMaxX(inRect), NSMaxY
(theEdges) + theControlY)
controlPoint2:NSMakePoint(NSMaxX(theEdges) +
theControlX, NSMaxY(inRect))];
// Top edge and upper-left corner
[theResult lineToPoint:NSMakePoint(theEdges.origin.x, NSMaxY
(inRect))];
[theResult curveToPoint:NSMakePoint(inRect.origin.x, NSMaxY
(theEdges))
controlPoint1:NSMakePoint(theEdges.origin.x -
theControlX, NSMaxY(inRect))
controlPoint2:NSMakePoint(inRect.origin.x, NSMaxY
(theEdges) + theControlY)];
// Left edge and lower-left corner
[theResult lineToPoint:NSMakePoint(inRect.origin.x,
theEdges.origin.y)];
[theResult curveToPoint:NSMakePoint(theEdges.origin.x,
inRect.origin.y)
controlPoint1:NSMakePoint(inRect.origin.x,
theEdges.origin.y - theControlY)
controlPoint2:NSMakePoint(theEdges.origin.x -
theControlX, inRect.origin.y)];
// Finish up and return
[theResult closePath];
return theResult;
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden