图形计算器:如何找到图形中适当的部分来显示

发布于 2024-11-09 22:04:34 字数 447 浏览 0 评论 0 原文

我正在开发一个图形计算器(你知道,你输入一个公式,比方说 x^2 ,你就可以得到该函数的图形)。我遇到的问题是如何偏移和缩放图形视图以显示函数的有趣部分。

我已经用尽了我所有的“简单”想法。让我举一些例子: - sin(x) =>有趣的部分是在 y = [-1,1] 和 offset (0,0) 之间 - x^2 =>有趣的部分是在 y = [0, 100] 之间,偏移量是 (0,0)。 (任意选取100个) - 100x^2 - 10000 => y = [-10000, 100*] 和偏移量是 (-10000, 0)

我想我可以为每种类型的函数分配一个“范围”和“偏移量”,并创建一些数学来将这些范围加/乘/等等以同样的方式计算结果。然而,这需要“创建”一些数学知识,并且隐藏逻辑缺陷的可能性太高了。

一定有一种不太困难的方法可以做到这一点,但我就是找不到。是否有一些特定的术语可供搜索?有算法的指针吗?

I am working on a graphing calculator (you know, one where you type in a formula, let's say x^2 and you get the graph of that function). The problem I am having is how to offset and scale the view of graph as to show the interesting section of the function.

I have exhausted all the 'simple' ideas I have had. Let me show some example:
- sin(x) => interesting section is between y = [-1,1] and offset (0,0)
- x^2 => interesting section is between y = [0, 100] and offset is (0,0). (100 has been picked arbitrarily)
- 100x^2 - 10000 => y = [-10000, 100*] and offset is (-10000, 0)

I figured I could assign a 'range' and 'offset' for each type of function and create some math to add/multiply/etc these range together the same way a result would be calculated. However, that requires 'creating' some math and the potential for well hidden logical flaws is way too high.

There must be a non-too-difficult way to do this, but I just can't find it. Are there some specific terms to search for? Any pointers to an algorithm?

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

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

发布评论

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

评论(3

止于盛夏 2024-11-16 22:04:34

多么有趣的问题啊。我从来没有想过这个,但我首先要找到:

  1. 最接近原点的方程的两个根(您可以使用 牛顿-拉夫森算法)。
  2. 最大值和最小值。为此,您需要找到函数导数为 0 的位置。您可以进行数值微分 并粗略地找到导数与 0 交叉的点,或者如果您雄心勃勃,可以使用 自动微分。一旦找到导数的 0 交叉点,请返回并在这些点处计算原始方程。
  3. 每个 x 轴点处的函数值。

然后取每个轴上相距最远的点,加上 10%,并将它们用作边界框坐标。

存在明显的边缘情况:函数可能没有根、有一个根或有无穷多个根。该函数可能没有最大值或最小值。我不太确定如何检测这些情况,但您可能希望对步骤 (1) 和 (2) 建立限制,例如查找前 N 个根或前 N 个极值,从 0 开始计数。另一个限制可能要确保一个轴上的偏移永远不会超过另一轴上偏移的 N 倍。

What an interesting problem. I've never thought about this, but I'd start by finding:

  1. The two roots of the equation nearest the origin (you can use the Newton-Raphson algorithm).
  2. The maxima and minima. For this you need to find the the places where the derivative of the function is 0. You could do numerical differentiation and find, roughly, the spots where the derivative crosses 0, or if you're feeling ambitious you could use automatic differentiation. Once you've found the 0-crossings of the derivative, go back and evaluate the original equation at those spots.
  3. The value of the function at each of these x-axis points.

Then take the furthest apart points on each axis, add 10% to them, and use them as the bounding box coordinates.

There are obvious edge cases: the function may have no, one, or infinitely many roots. The function may have no maximum or minimum. I'm not really sure how you can detect these cases, but you might want to build in limits to steps (1) and (2), like find the first N roots or the first N extrema, counting out from 0. Another limit might be making sure your excursion on one axis is never more than N times the excursion on the other axis.

吹泡泡o 2024-11-16 22:04:34

最常见图形的两个有趣点是坐标系的原点(用于方向)和函数的 y 截距,这很容易计算。因此,我选择的比例使得原点 (0,0) 和 y 截距 (0,y0) 都可见,加上一些填充,即区间 [-y0 - y0/5; y0 + y0/5]。如果原点和 y 轴截距碰巧接近甚至相同,我会选择一个可见的区间,例如 [-5; 5]。

这背后的基本原理是,一个精心制定的函数应该在原点附近或至少在 y 截距附近有其有趣的部分。如果没有,你根本无法告诉用户想要看到什么,所以他应该自己处理这个问题。

Two interesting points of most common graphs are the origin of the coordinate system (for orientation) and the y-intercept of the function, which is easy to compute. Hence, I'd choose the scale such that both the origin (0,0) and the y-intercept (0,y0) are visible, plus some padding, i.e. the interval [-y0 - y0/5; y0 + y0/5]. If origin and y-intercept happen to be close or even the same, I'd choose a visible interval of, say, [-5; 5].

The rationale behind this is that a well-formulated function is supposed to have its interesting part somewhere near the origin, or at least near the y-intercept. If it doesn't, you simply can't tell what the user wants to see, so he shall take care of that himself.

半葬歌 2024-11-16 22:04:34

感兴趣区域的可能定义之一是以下点的密度:

  1. f(x)=0(穿过 x-zxis)
  2. f'(x)=0(最小/最大)
  3. f''(x)=0(改变曲率)
  4. f'''(x)=0(最大曲率,可能最小曲率可能不是很有趣)

One of possible definitions of interesting zone is density of the following points:

  1. f(x)=0 (crossing x-zxis)
  2. f'(x)=0 (min/max)
  3. f''(x)=0 (changing direction of curvature)
  4. f'''(x)=0 (max. curvature, probably min. curvature may not be very interesting)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文