Skip navigation.
 
mlRe: NSBezierPath geometry question...
FROM : Serge Meynard
DATE : Mon Apr 11 16:02:46 2005

On Apr 11, 2005, at 07:17, Keith Blount wrote:
> 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.
>
> [snip]
>
> // 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];


Robert Clair is right, I had forgotten that 
appendBezierPathWithArcFromPoint:toPoint: creates a straight line 
segment from your current point to the start point of the arc. This 
actually simplifies the code a lot.
But there's a simple error in your code. Here currPoint and nextPoint 
are the second and third points forming the current corner; drawing an 
arc between them won't produce what you want. The radius calculation 
you're making is probably fine, but the two points you provide to the 
arc method must lie on the circle, and you want them to be at 90 
degrees from each other. What you need for your arc points is a set of 
points at distance "radius" from currPoint; the first interpolated 
between currPoint and [path currentPoint], and the second interpolated 
between currPoint and nextPoint.
There's also a minor error in that your first radius calculation will 
use only half of the top edge, since startPoint is the halfway point 
between the first and last points. I think you could make your 
algorithm simpler and cleaner by adding the last point as the first 
entry in corners, and also adding the first point as the last entry. 
You can then loop from 1 to the number of points (not corners), and 
always use i-1, i and i+1 to make calculations (instead of [path 
currentPoint], i and i+1). You'd still start at the same startPoint.

Hope this helps.

Serge

Related mailsAuthorDate
mlNSBezierPath geometry question... Keith Blount Apr 10, 23:40
mlRe: NSBezierPath geometry question... Serge Meynard Apr 11, 00:37
mlRe: NSBezierPath geometry question... Robert Clair Apr 11, 13:01
mlRe: NSBezierPath geometry question... Keith Blount Apr 11, 13:17
mlRe: NSBezierPath geometry question... Serge Meynard Apr 11, 16:02
mlRe: NSBezierPath geometry question... Nicko van Someren Apr 11, 17:48
mlRe: NSBezierPath geometry question... Serge Meynard Apr 11, 18:41
mlRe: NSBezierPath geometry question... David Phillip Oste… Apr 11, 18:46
mlRe: NSBezierPath geometry question... Nicko van Someren Apr 11, 19:48
mlRe: NSBezierPath geometry question... Heinrich Giesen Apr 11, 20:49
mlRe: NSBezierPath geometry question... Robert Clair Apr 11, 21:02
mlRe: NSBezierPath geometry question... Keith Blount Apr 11, 21:19
mlRe: NSBezierPath geometry question... Scott Thompson Apr 11, 23:25
mlRe: NSBezierPath geometry question... Robert Clair Apr 11, 23:54
mlRe: NSBezierPath geometry question... Keith Blount Apr 12, 00:24
mlRe: NSBezierPath geometry question... Nicko van Someren Apr 12, 10:04
mlRe: NSBezierPath geometry question... Keith Blount Apr 12, 18:08
mlRe: NSBezierPath geometry question... Jonathon Mah Apr 13, 17:11