In my Cocoa hobby project (on OSX), I have a view with some points identified. Something like this:
NSPoint pt1 = NSMakePoint(20,100);
NSPoint pt2 = NSMakePoint(100,30);
I would like to create a meandering line (that never crosses itself) between those two point. The points may vary, of course. I'm familiar with NSBezierPath
but I'm not graphics whiz.
There are two variations on this. Given NSBezierPath *p ...
and setup of [p moveToPoint:pt1]
- Use
[p lineToPoint:ptx]
where I create a meandering line that is jagged and - Use
[p curveToPoint:ptx controlPoint1:cpt1 controlPoint2:cpt2]
with a meandering line that is smooth.
The 2nd case seems more difficult, since sensible control points also must be calculated.
Finally, I would like to be able to adjust the amount with which the line meanders. If I set a variable like int numOfIntermediatePoints
to 1, then there will be a smooth curve between pt1
and pt2
. If I set numberOfIntermediatePoints
to 10, there will be much more movement in the line. I don't want the last intermediate points to be very far from the final point (leaving a large change at the end of the line).
I've looked into using Perlin noise, but it seems like it would be hard to guide the line towards its end point. It seems like it would make sense to calculate an array of NSPoint
items (and possibly an array of control points, for case two) and then loop through them to create the line.
What's the best approach to this?
Update
Following Tommy's advice led me to port Raymond Hill's Javascript-Voronoi library to Obj-C. You can find it here: https://github.com/ccheaton/objcvoronoi
Another Update
One more update -- I played with Dijkstra's algorithm and found it to be overkill for what I was trying to achieve. I ended up implementing a simplified variation of it that allows me to specify guide nodes for the random line. In this image, the line start is on the mid left, the line end is on the mid right, and there are guide points at (xMax * 0.33, 0) and (xMax * 0.66, yMax).
Final Update
To make it slightly less jagged, I added an optional relaxation algorithm. Performance isn't great now, but that doesn't matter for the use I have in mind.