views:

62

answers:

2

I have a graph, generated by a function, and it zooms itself in and out automatically, depending on the value of the function. I already have the graphing tools and I can display any x,y,width,height at high resolution.

I tried just snapping to the right spot:

x = target_x
y = target_y
width = target_width
height = target_height

But it's too jumpy. It's hard to tell what part was zoomed in/out.

I also tried doing this:

orig_x = x //ditto for y, width, height, etc
for i=1 to 10
    x = i/10*new_x + i/10*orig_x
    wait 25ms

It's smoother but the first step is still too jumpy. If orig_x is 10 and new_x is 1 million then the first jump is too big, near 1,000,000%. Last jump is just 10%, however. A geometric progression is even better but if I have to switch directions mid-zoom the steps are jumpy.

What would be the best effect to use?

A: 

I may try

xDiff = new_x - orig_x
stepCount = 10

for i=1 to stepCount
    x = orig_x + i/stepCount * xDiff
    wait 25ms
tafa
This zoom this algorithm creates will run in different amounts of time in different environments.
NickLarsen
@NickLarsen, may I ask why?
tafa
That's identical to what I wrote.
Eyal
@tafa because different environments will perform the non wait operations in different amounts of time. If the system were to eat up cycles in a background process half way through the rendering, the result would still be that the first part takes shorter amount of time than the last part of the zoom animation. The change from frame to frame would be consistent however. If the OP wants smooth motion based on time (which his metric was unstated), then the animation needs to calculate next frame position based on the elapsed time, and length of animation.
NickLarsen
considering the OP is using graphing tools which scale the original image at each step, the algorithm that process uses may well take different amounts of time depending on the resolution being scaled to. Regardless of the zoom stepping method used, if it does not take into account time elapsed and animation duration, it will have differing amounts of jumpyness depending on the time the rendering takes place.
NickLarsen
Could you imagine what playing video games would be like if developers assumed each frame would take exactly 1/30 of a second to render and paused that much time after each frame?
NickLarsen
That's a good point. In practice, I'm using VB.Net and a timer launches the redraw once every 25ms. So I think that I'm okay. And close enough is good enough for me.
Eyal
A: 

You want to zoom/shift a fixed percentage of the last window for each step. That means your zoom will be an exponential curve, and your shift will be tied to your zoom. Something like the following should be an improvement:

width_ratio = new_width / old_width
width_ratio_log = log(width_ratio)

x_diff = new_x - old_x
x_factor = x_diff / (width_ratio - 1)
-- warning: need to handle width_ratio near 1 the old way!

for i=1 to steps
    width_factor = exp(width_ratio_log * i / steps)
    width = old_width * width_factor
    x = old_x + x_factor * (width_factor - 1)

    -- similarly for y and height...
comingstorm