Re: NSBezierPath geometry question...
Re: NSBezierPath geometry question...
- Subject: Re: NSBezierPath geometry question...
- From: Keith Blount <email@hidden>
- Date: Mon, 11 Apr 2005 04:17:32 -0700 (PDT)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
Many thanks to you both for your replies.
This is in fact what I have been trying - going down
the right sides, then back up the left sides of the
rects, and then trying to round the corners using
appendBezierPathWithArcFromPoint... I have pasted the
code I have been trying at the bottom of this post. It
gets all the points correctly and rounds the corners,
but there are still some drawing glitches - in
particular, my radius calculation seems to result in
the top corners being more rounded than the bottom
ones, though I am not sure why.
Many thanks again,
Keith
The (not quite working properly) code:
NSLayoutManager *layoutManager = [self layoutManager];
NSTextContainer *textContainer = [self
textContainer];
unsigned rectCount;
NSRectArray rectArray = [layoutManager
rectArrayForCharacterRange:charRange
withinSelectedCharacterRange:NSMakeRange(NSNotFound,0)
inTextContainer:textContainer
rectCount:&rectCount];
int i;
NSBezierPath *path = [NSBezierPath bezierPath];
float radius = 6;
NSMutableArray *corners = [NSMutableArray array];
NSPoint textContainerOrigin = [self
textContainerOrigin];
// Get right sides
for (i=0; i<rectCount; i++)
{
// Get current rect and account for text container
NSRect currRect =
NSOffsetRect(rectArray[i],textContainerOrigin.x,textContainerOrigin.y);
NSPoint topPoint =
NSMakePoint(NSMaxX(currRect),currRect.origin.y);
NSPoint bottomPoint =
NSMakePoint(NSMaxX(currRect),NSMaxY(currRect));
// Make sure all points lie on the centre of a pixel
topPoint.x = (int)topPoint.x;
topPoint.x += 0.5;
topPoint.y = (int)topPoint.y;
topPoint.y += 0.5;
bottomPoint.x = (int)bottomPoint.x;
bottomPoint.x += 0.5;
bottomPoint.y = (int)bottomPoint.y;
bottomPoint.y += 0.5;
[corners addObject:[NSValue
valueWithPoint:topPoint]];
[corners addObject:[NSValue
valueWithPoint:bottomPoint]];
}
// Get left sides
for (i=rectCount-1; i>=0; i--)
{
NSRect currRect =
NSOffsetRect(rectArray[i],textContainerOrigin.x,textContainerOrigin.y);
NSPoint topPoint = currRect.origin;
NSPoint bottomPoint =
NSMakePoint(currRect.origin.x,NSMaxY(currRect));
topPoint.x = (int)topPoint.x;
topPoint.x += 0.5;
topPoint.y = (int)topPoint.y;
topPoint.y += 0.5;
bottomPoint.x = (int)bottomPoint.x;
bottomPoint.x += 0.5;
bottomPoint.y = (int)bottomPoint.y;
bottomPoint.y += 0.5;
[corners addObject:[NSValue
valueWithPoint:bottomPoint]];
[corners addObject:[NSValue
valueWithPoint:topPoint]];
}
// Now try to draw the curve...
NSPoint firstPoint = [[corners objectAtIndex:0]
pointValue];
NSPoint lastPoint = [[corners objectAtIndex:[corners
count]-1] pointValue];
NSPoint startPoint =
NSMakePoint((firstPoint.x+lastPoint.x)/2.0,(firstPoint.y+lastPoint.y)/2.0);
[path moveToPoint:startPoint];
for (i = 0; i < [corners count]; i++)
{
NSPoint currPoint = [[corners objectAtIndex:i]
pointValue];
if (i < [corners count]-1)
{
NSPoint nextPoint = [[corners objectAtIndex:i+1]
pointValue];
radius = MIN(6, 0.5 * (MIN(
KBDistanceBetweenPoints([path
currentPoint],currPoint),
KBDistanceBetweenPoints(currPoint,nextPoint))));
[path appendBezierPathWithArcFromPoint:currPoint
toPoint:nextPoint radius:radius];
}
else
{
radius = MIN(6, 0.5 * (MIN(
KBDistanceBetweenPoints([path
currentPoint],currPoint),
KBDistanceBetweenPoints(currPoint,startPoint))));
[path appendBezierPathWithArcFromPoint:currPoint
toPoint:startPoint radius:radius];
}
}
[path closePath];
And this is the function used in radius calculation
above:
float KBDistanceBetweenPoints (NSPoint a, NSPoint b)
{
float
dX = a.x - b.x,
dY = a.y - b.y;
return (sqrt(dX*dX + dY*dY));
}
__________________________________
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/
_______________________________________________
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