Take the tour ×

I have got experience in iPhone programming but not really in graphics programming.

I decided to make a 2d game and choose Quartz 2D for this purpose (I'm familiar with Open GL, but quartz seems to be easier for a 2d game).

Is there a built-in way to draw a graph of a math formula in Quartz? For beginning it will be only a parabola (y=ax^2+bx+c) but the equation itself will become more complex in the future.

It may be pretty slow if I will draw the parabola using my own algorithm (from separate lines or dots).

Thank you in advance.

share|improve this question
add comment

5 Answers

up vote 4 down vote accepted

You could calculate some discrete samples of the equations you want to render.
Your graph will look smooth or polygonal depending on how many samples you provide.

This blog post describes how to render waveforms for a Mac OS X application.
As the author is using Quartz and you can probably adapt his approach for your problem.
The blog post uses the convenient CGPathAddLines() method to create a path from a point array.
If you'd like to construct your path piece by piece, you could use some of the following methods:

CGMutablePathRef path = CGPathCreateMutable(); 
CGPathMoveToPoint(path, ...);
CGPathAddLineToPoint(path, ...);
CGPathAddArc(path, ...);
share|improve this answer
add comment

What about using something like the CorePlot framework?

share|improve this answer
add comment

For plotting an arbitrary math expression, you can use Graham Cox's GCMathParser to take in an NSString containing the expression and evaluate that expression for various values. You can then use Core Plot (as suggested by Jasarien) to create a graph of these values. In fact, I have a sample application that does just this, which I will be adding to the project soon.

share|improve this answer
add comment

There's nothing built in to Quartz for this; you'll have to plot each point individually using math.

share|improve this answer
add comment

Quartz uses Bezier curves

share|improve this answer
add comment

Your Answer


