views:

424

answers:

5

Hello everyone,

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.

A: 

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

Ben Gottlieb
A: 

Quartz uses Bezier curves

Azeem.Butt
+4  A: 

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, ...);
CGPathCloseSubpath(path);
weichsel
+2  A: 

What about using something like the CorePlot framework?

Jasarien
+1  A: 

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.

Brad Larson