Skip navigation.
 
mlRe: NSBezierPath geometry question...
FROM : Keith Blount
DATE : Tue Apr 12 00:24:20 2005

In a possibly vain attempt to return to my original
question, if anyone could help, I would be very
grateful. :)

To recap, I am trying to achieve something similar to
this:

http://www.rumramruf.com/bezierPic.tiff

but am getting certain drawing glitches. If anybody
actually knows how to generate a bezier path that
draws around a rectArray returned from
NSLayoutManager's rectArrayForCharacterRange:... so as
to round the corners, similar to the above picture, I
would be very grateful.

Many thanks again,
Keith

PS. Here is a re-post of the code I am currently using
to generate the above bezier paths, called in my text
view's drawViewBackgroundInRect: method. The top
corners are more rounded than the bottom, and I'm not
sure why, and there are other minor glitches too:

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!?
Make Yahoo! your home page
http://www.yahoo.com/r/hs

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