Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing an equation in Quartz

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.

like image 412
Ilya Suzdalnitski Avatar asked Dec 29 '22 07:12

Ilya Suzdalnitski


1 Answers

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);
like image 199
Thomas Zoechling Avatar answered Jan 09 '23 11:01

Thomas Zoechling