Keeping a constant resolution scale of an image

145 Views Asked by At

Intro

I'm writing a program that zooms in and out on an image and the only way to do it with the data I have is to change the limits of my graph. The aspect ratio (scale of x to y) always remains 1:1 and the ratio of the x limits to the y limits must be 1280/720. The zoom is based off of just one variable, we call $d =$ depth

Question

I am having trouble scaling it when the parameter $d$ changes from the default value, which is just a scale factor, and the default value is $1$. I can get it to scale correctly at the default value, but the current equations I have slowly deviate from what I want as I increasee/decrease $d$. The hardest part for me is that I need the image to always be centered within the limits of the plot, as well as keeping the proper scale between $x$ and $y$. The variables used are:

$R_f = d \cdot R_i \rightarrow$ final resolution (resolution of my depth-scaled image)

$R_i = 250\times250 $ resolution $ \rightarrow$ initial resolution (resolution of image before being scaled by depth)

$d = $ depth

The default position of my image has it's top left corner at the origin $(0,0)$ and propagates in the $+x,-y$ direction, so the bottom right corner is at $(R_f, -R_f)$. Now I need equations to define the limits of $(x_i,y_i)$ and $(x_f,y_f)$ based on those parameters so that the image is always centered, no matter how $d$ changes (well, it can't be negative).

My Attempt

This is what I tried. I think my error comes from forming my last term for the $x$ limits. I just can't figure out how to make a correct combination of the 2nd and 3rd terms I discuss...anyways. First I defined $$y_i = \frac{R_f}{2} - \frac{R_i}{2}$$ $$y_f = \frac{R_f}{2} + \frac{R_i}{2}$$

which seems to do the job for centering the image for all $d$. The limits become larger in comparison to the resolution of my image as $d$ increases and vice-versa, so I think I'm good here. Now the trouble comes with $x_i, x_f$. At all values, we know that the limits of $x$ must be $1280/720 = 1.778$ times larger than the $y$ limits. Taking note of this at the default value, where $d=1$, we know that we must add in a term for the $x$ limits (using a simple scale factor on the y-limits here doesn't work, since the image needs to be centered). I came up with $\frac{1}{2} \cdot R_f(\frac{1280}{720} - 1)$. Now I just subtract this term for $x_i$ and add it for $x_f$ to the respective y limits, and this works for the default value.

Now we face more problems when we aren't at the default value though. This problem can be illustrated if we draw a small square box (our image) in a very large rectangle and look at our given equations. Clearly, the added term in the previous paragraph becomes negligible in comparison to the total growth of the $x$ limits. But the $y$ limits and $x$ limits can't be increasing at the same rate, otherwise our resolution will be compromised. So one more term is neccessary. I thought the final term would be $\frac{1280}{720}(\frac{R_f}{2} - \frac{R_i}{2})$, giving my $x$ limits as

$$x_i = \left(1 + \frac{1280}{720}\right) \left(\frac{R_f}{2} - \frac{R_i}{2}\right) - \frac{1}{2} \cdot R_f\left(\frac{1280}{720} - 1\right)$$ $$x_f = \left(\frac{R_f}{2} + \frac{R_i}{2}\right) - \frac{1}{2} \cdot R_f\left(\frac{1280}{720} - 1\right) - \frac{1280}{720} \left(\frac{R_f}{2} - \frac{R_i}{2}\right)$$

But this doesn't quite work. If my $d \le 1$ it actually works pretty well (but not perfect) but as $d$ gets bigger and bigger, my error blows up more and more. Anyways, if someone can figure out where I'm going wrong, that would be great! I honestly didn't think this task would be too difficult!