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 = 开发者_StackOverflow中文版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?
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...
I may try
xDiff = new_x - orig_x
stepCount = 10
for i=1 to stepCount
x = orig_x + i/stepCount * xDiff
wait 25ms
精彩评论