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?
What an interesting problem. I've never thought about this, but I'd start by finding:
- The two roots of the equation nearest the origin (you can use the Newton-Raphson algorithm).
- 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.
- 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.
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:
- f(x)=0 (crossing x-zxis)
- f'(x)=0 (min/max)
- f''(x)=0 (changing direction of curvature)
- f'''(x)=0 (max. curvature, probably min. curvature may not be very interesting)
精彩评论