我想创建一个图形计算器,但我坚持使用图形位。我想知道如何为 sin(x) cos(x) tan(x) 绘制图形。我已经做了网格。我不想使用核心情节框架。
任何帮助,将不胜感激。谢谢。
我想创建一个图形计算器,但我坚持使用图形位。我想知道如何为 sin(x) cos(x) tan(x) 绘制图形。我已经做了网格。我不想使用核心情节框架。
任何帮助,将不胜感激。谢谢。
要实际绘制函数,请像使用纸和铅笔一样:针对多个输入评估函数。然后画线以连接结果点。
并不是说我真的会这样做(我会看 Core Plot),但您可以使用 Core Image 生成器过滤器绘制这样的图,如下所示:
//wavelength and magnitude are distances in destination pixels. Think of them as the width and height of each wave.
kernel vec4 sineWave(float wavelength, float magnitude, __color color)
{
vec2 coord = destCoord();
coord.y -= magnitude;
coord /= vec2(wavelength, magnitude / 2.0);
float pi = radians(180.0);
float value = sin(coord.x * pi);
//Smaller threshold = finer wave line. For a gradient, replace the comparison with 1.0 - abs(…).
float threshold = 0.1;
float alpha = abs(coord.y - value) <= threshold;
return color * alpha;
}
这是一些可以回答您的问题的伪代码:
for i = xmin to xmax do
{
draw XY point at X=(i*x_scale_factor+x_offset) and Y=(sin(i)*y_scale_factor+Y_offset);
}
请注意:不要在 for 循环中使用浮点数
编辑以回应评论
恕我直言,最简单的方法是获取视图的边界,以获取 X 轴和 Y 轴上数据的最小值和最大值。
然后,您可以使用NSAffineTransform
实例来转换绘图的坐标。所以一切都可以在你的图形坐标中完成,这更容易。如果您愿意,您可以在坐标 (4.6, 3.2*10-7) 处写一个标签。这是让您入门的关键点。路很长。但是使用NSAffineTransform
会更容易。