Alastair’s Place

Software development, Cocoa, Objective-C, life. Stuff like that.

Arrow Demo

Bézier Path Arrow Demo

Some other operating systems provide a built-in way to add arrows to the ends of lines. Mac OS X, in contrast, leaves it up to you to figure out how to do this, and in some cases the results haven’t been particularly pretty.

This program is a demo that I wrote way back in 2004, showing one way to add arrows to arbitrary NSBezierPaths. In particular, drawing an arrowhead is as simple as

NSBezierPath *startArrow = [ourPath bezierPathWithArrowHeadForStartOfLength:10.0

[startArrow fill];

There’s a similar method for obtaining an arrowhead path for the end of your path.

The code works by chopping off a short segment of the path, rotating it through the specified angle in either direction and joining the ends together. By default, you get an open path with just the two sides, but obviously if you want to close it you can just send it a -closePath message.

You can grab the code from the mercurial repository.