在 Quartz 中绘制方程

发布于 2024-08-16 07:36:10 字数 240 浏览 2 评论 0原文

我有 iPhone 编程的经验,但在图形编程方面却没有。

我决定制作一个 2d 游戏并为此选择 Quartz 2D(我熟悉 Open GL,但 Quartz 似乎更适合 2d 游戏)。

有没有内置的方法可以在 Quartz 中绘制数学公式的图表?一开始它只是一个抛物线(y=ax^2+bx+c),但方程本身将来会变得更加复杂。

如果我使用自己的算法(从单独的线或点)绘制抛物线,可能会很慢。

先感谢您。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

意中人 2024-08-23 07:36:10

您可以计算要渲染的方程的一些离散样本。
您的图表将看起来平滑或多边形,具体取决于您提供的样本数量。

这篇博文介绍了如何在 Mac OS 上渲染波形X 应用程序。
由于作者使用的是 Quartz,您可能可以根据您的问题调整他的方法。
该博客文章使用方便的 CGPathAddLines() 方法从点数组创建路径。
如果您想逐步构建路径,可以使用以下一些方法:

CGMutablePathRef path = CGPathCreateMutable(); 
CGPathMoveToPoint(path, ...);
CGPathAddLineToPoint(path, ...);
CGPathAddArc(path, ...);
CGPathCloseSubpath(path);

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);
寄意 2024-08-23 07:36:10

使用 CorePlot 框架怎么样?

What about using something like the CorePlot framework?

笛声青案梦长安 2024-08-23 07:36:10

要绘制任意数学表达式,您可以使用 Graham Cox 的 GCMathParser 来获取包含以下内容的 NSString表达式并评估该表达式的各种值。然后,您可以使用核心图(按照 Jasarien 的建议)创建以下图表:这些值。事实上,我有一个示例应用程序就是这样做的,我将很快将其添加到项目中。

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.

情话已封尘 2024-08-23 07:36:10

Quartz 中没有为此内置任何东西;您必须使用数学单独绘制每个点。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文