SOBezier – Getting started.

stompy » 30 July 2008 » In SOBezier »

The first part of this demonstration will create an application that draws a predefined curve in a window that is not re-sizeable. The idea is to get a simple working version up and running in the first place which can then be built upon.

Create the Project

In Xcode, create a new project of type “Cocoa Document-based application”. Save it wherever you like and call it SOBezier. The only project setting that needs to be set is to make sure that Garbage collection is turned on.


MVC Classes

Cocoa makes extensive use of the Model-View-Controller (MVC) paradigm. Model objects are responsible for the data or state of the application. View objects are responsible for displaying the data and the Controller objects keep the two in sync. For this basic application, the model class is SOBezierCurve, a subclass of NSObject, which contains the points of the curve and also knows how to draw itself. The view class is SOBezierView, a subclass of NSView, in which the curve is drawn. These are co-ordinated by the MyDocument class, a subclass of NSDocument, which is the controller.

SOBezierCurve – the model

From the Xcode File menu, choose New File and then select a Cocoa Objective-C class. Call this SOBezier curve. This will create the header and the implementation files. Edit them to look like these code listings.

//  SOBezierCurve.h

#import <Cocoa/Cocoa.h>

@interface SOBezierCurve : NSObject {
    NSPoint pt1;
    NSPoint pt2;
    NSPoint ControlPt1;
    NSPoint ControlPt2;

@property (readwrite) NSPoint pt1;
@property (readwrite) NSPoint pt2;
@property (readwrite) NSPoint ControlPt1;
@property (readwrite) NSPoint ControlPt2;

- (id)initWithPt1:(NSPoint)point1
- (void)drawCurve;


Here, the four points that define the curve are declared and I am using the Objective-C 2.0 @property syntax to make these properties. Two methods are declared. First, an initialiser that creates the curve based on the four points being passed in, and a method that draws the curve.

The implementation is quite simple:

//  SOBezierCurve.m

#import "SOBezierCurve.h"

@implementation SOBezierCurve
@synthesize pt1, pt2, ControlPt1, ControlPt2;

- (id)initWithPt1:(NSPoint)point1
    // Designated initialiser.
    if (![super init]) {
        return nil;
    pt1 = point1;
    pt2 = point2;
    ControlPt1 = cPoint1;
    ControlPt2 = cPoint2;
    return self;

- (void)drawCurve
    NSBezierPath *path = [NSBezierPath bezierPath];
    // Move to the first control point
    [path moveToPoint:ControlPt1];
    // Add a line from the control point to the first point of the curve
    [path lineToPoint:pt1];
    // Create the curve
    [path curveToPoint:pt2 controlPoint1:ControlPt1 controlPoint2:ControlPt2];
    // Draw a line from the end of the curve to the second control point
    [path lineToPoint:ControlPt2];
    // Now draw the curve
    [[NSColor blackColor] set];
    [path stroke];


The initialiser just sets the points of the curve to the points that are passed in. the @sythesize directive lets Objective-C 2.0 create accessors for the properties that are Key Value Coding (KVC) compliant.

The DrawCurve: method is responsible for drawing the curve represented by these points. This is where the terminology becomes confusing with the term ‘bezier’ appearing everywhere. The NSBezierPath class is used extensively in Cocoa graphics, (which is why it is so easy to draw bezier curves), so to draw the curve a series of bezier paths are linked together as a single path and then drawn on the view by stroking this single path.

SOBezierView – the view

This class needs to be creates as we did with the SOBezierCurve class. From the Xcode File menu, choose New File and then select a Cocoa Objective-C NSView subclass. Call this file SOBezierView. This will create the header and the implementation files. Edit them to look like these code listings.

//  SOBezierView.h

#import <Cocoa/Cocoa.h>
@class MyDocument;

@interface SOBezierView : NSView {
    IBOutlet MyDocument *doc;


This class doesn’t do much at the moment so it is quite small. The interface file just declares an outlet called doc to the controller, which is the MyDocument object in this case.

The implementation file only has two methods, both of which come from the superclass so they don’t need to be declared in the header file.

//  SOBezierView.m

#import "SOBezierView.h"
#import "MyDocument.h"
#import "SOBezierCurve.h"

@implementation SOBezierView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    return self;

- (void)drawRect:(NSRect)rect {
    [[NSColor yellowColor] set];
    [NSBezierPath fillRect:[self bounds]];
    [[doc curve] drawCurve];


The initWithFrame: method is where the initialisation code will go. But the important method is the drawRect: method which is invoked whenever the view needs to draw itself.

The first thing that the drawRect: method does is set the colour to be used and then calls the NSBezierPath fillRect: method to fill the entire view window with this colour. The rect paramater that is passed into the drawRect: method is the rectangle that is ‘dirty’ and needs to be redrawn, but to keep things simple, this implementation just fills in the whole view. If the view was drawing a number of curves, this dirty rectangle could be used to determine which curves are within this rectangle and the drawing code could be optimised to only draw those curves. The method then uses the outlet to the MyDocument object outlet doc to access curve object which is then asked to draw itself.

MyDocument – the controller

The MyDocument class was created by Xcode as part of the project.

Edit the header file so that it looks like this listing.

//  MyDocument.h
//  SOBezier

#import <Cocoa/Cocoa.h>
@class SOBezierCurve;                          // 1

@interface MyDocument : NSDocument
    SOBezierCurve *curve;                      // 2
@property (readwrite) SOBezierCurve *curve;    // 3
  1. Here SOBezierCurve is declared to be a class.
  2. The curve attribute is declared as a pointer to an SOBezierCurve object.
  3. This attribute is declared as a property.

The MyDocument.m file contains a lot of generated code. For now the only changes need to be made to the beginning of the file. Edit the code so it looks like this listing.

//  MyDocument.m
//  SOBezier

#import "MyDocument.h"
#import "SOBezierCurve.h"                                   
// 1

@implementation MyDocument
@synthesize curve;                                          // 2

- (id)init                                                  // 3
    self = [super init];
    if (self) {
        NSPoint p1 = NSMakePoint(100, 150);
        NSPoint p2 = NSMakePoint(300, 150);
        NSPoint cP1 = NSMakePoint(100, 225);
        NSPoint cP2 = NSMakePoint(300, 75);
        curve = [[SOBezierCurve alloc] initWithPt1:p1
    return self;



  1. Import the header file for the SOBezierCurve class.
  2. This creates KVC compliant accessors for the curve attribute.
  3. The init method, called when the object is created, sets up the points to be used for the default curve and then creates an SOBezierCurve object with these points.

Create the User Interface

Here’s a short screencast of how the UI is created. (If a picture is worth a thousand words, how many more is a video worth?).

Once that is done, you can click build and go to see a simple bezier curve drawn on a yellow background. Not very interesting so far, but it’s a good starting point.

Next Steps

The next step will be to add handles to the points so they can be moved around the view causing the curve to change.

Finally, thanks for your attention and I’d appreciate any comments you have, particularly about things you don’t like, so that I can improve this demonstration.


You can get the source code up to this point from the project’s git repository here. A checkout of the Stage1_1 tag will get the code up to this point.

Alternatively, you can download a zip file of the completed version up to this point here: (60 kb)

Tags: , , , , ,

Trackback URL

2 Comments on "SOBezier – Getting started."

  1. stompy
    youtube unlock
    07/11/2013 at 1:06 pm Permalink

    Terrific defeat! I need to beginner as you fix your web site, how may possibly i sign up to for your site site? Your bill solved the problem the suitable option. I actually ended up small bit common of the the transmitted provided outstanding see-through thought

  2. stompy
    27/01/2015 at 12:26 pm Permalink

    Hi there very cool website!! Man .. Excellent
    .. Wonderful .. I’ll bookmark your web site and take the feeds also?
    I am satisfied to seek out so many helpful information right
    here in the publish, we’d like work out extra techniques in this regard, thanks for sharing.
    . . . . .

Hi Stranger, leave a comment:


<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to Comments