I don't know how to formulate what the algorithm I need does in fewer words; it is highly possible that such question is already answered here, but I don't know how to search for it.
What I need is the following.
I'm writing kind of 2D graph plotting program. Obviously, drawing only main coordinate lines is not very good, since the graph can be located far from the origin, and the user will not have any reference for its size if only main lines are drawn.
I decided to draw reference lines, that is, horizontal and vertical lines which appear periodically with constant interval, corresponding to e.g. whole numbers. However, using constant intervals does not work so great because of possibility of scaling, that is, if user zooms in to see, for example, internals of unit square, he will again see nearly empty box without any reference to sizes.
So, I need to somehow rescale the interval which I use to draw the reference lines. In other words, I need a function of type like double -> double
which accepts current scale coefficient (the one I use specifies how many pixels are in one unit of length) or a value of some function applied to it (e.g. maximum number of unit lengths which can fit inside the screen along some axis) and returns a value of step (in pixels on in logical units, does not matter), that is, the length of the interval between reference lines.
I believe that the set of values of such function would be countably infinite, that is, this function would map intervals of scale coefficient value to fixed values of the step. I can give rough description of the algorithm, or, more correctly, the mapping I need. It looks similarly to this (input value is the maximum number of units inside the screen):
- ...
- [5..9] -> 0.25
- [9..15] -> 0.5
- [15..30] -> 1
- ...
The values here are purely empirical and used only for illustration.
However, I don't know how to extrapolate this set of mappings to wider range of scale coefficient values, let alone how to express such dependency analytically. Yet I'm sure that this is possible because I saw such thing in many graphing programs.
I found this question which looks very close to what I need, but I couldn't manage to adapt the answer to it to my task since it mentions label width and I don't have any labels.