我正在开发一个图形计算器(你知道,你输入一个公式,比方说 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?
发布评论
评论(3)
多么有趣的问题啊。我从来没有想过这个,但我首先要找到:
然后取每个轴上相距最远的点,加上 10%,并将它们用作边界框坐标。
存在明显的边缘情况:函数可能没有根、有一个根或有无穷多个根。该函数可能没有最大值或最小值。我不太确定如何检测这些情况,但您可能希望对步骤 (1) 和 (2) 建立限制,例如查找前 N 个根或前 N 个极值,从 0 开始计数。另一个限制可能要确保一个轴上的偏移永远不会超过另一轴上偏移的 N 倍。
What an interesting problem. I've never thought about this, but I'd start by finding:
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.
最常见图形的两个有趣点是坐标系的原点(用于方向)和函数的 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.
感兴趣区域的可能定义之一是以下点的密度:
One of possible definitions of interesting zone is density of the following points: