cocoagraphicsquartz-graphicsmacrubygraph-drawing

NSBezierPath / Line Intersection / flatten


I'm currently porting my jruby/java2d Graph Drawing/Layouting application to macruby/cocoa. Therefore I need to get the intersection point of an open NSBezierPath with an closed NSBezierPath.

In java2d I used the following trick. I flattened both paths and did a simple line intersection test for each segment.

So is there a simple way to convert a NSBezierPath to a bunch of straight lines?

My current algorithm simply walks the line (in a binary search way) until I find a NSPoint for which containsPoint is true. But it only works for straight lines. The one I implemented in java2d worked for curved paths too.

def getIntersection edge, path
  out = edge.source
  ins = edge.target
  until (out.dist(ins) < 1.0)
    mid = out + ((ins - out) * 0.5)
    if (path.containsPoint (NSMakePoint(mid.x, mid.y)))
      ins = mid
    else
      out = mid
    end
  end
  return out
end

Graph


Solution

  • So is there a simple way to convert a NSBezierPath to a bunch of straight lines?

    Send the path a bezierPathByFlatteningPath message. This will return a new path, so converted.