문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top