Calculating formula dependent on 2 variables from known points

28 Views Asked by At

I have an image 1916x971 in size embedded in an app.

I'm implementing a zoom action that allows the user to scale the image from 1 (actual size) to 6.

I am also restricting the movement so when the user slides out of bounds of the image, the movement will stop.

When the scale is 1, the translation matrix equals the original matrix, and therefore the "right" movement stops at 1916 and "down" stops at 971, as expected.

However when I zoom in and move right and down, the translated matrix stops well before the edge of the image. As the scale grows, so does the distance from the edge of the image.

I was able to gather the values in which the movement should stop, but my math is quite weak and I can't figure out what formula, dependent on Scale and Width\Height, will give me the required values for each Scale.

Scale   Right   Down
1       1916    971
1.3     1892    948
1.5     1868    938
2       1788    916
3       1652    878
4       1498    814
6       1187    727

What I'm looking for is the formula, that, for example:

input: Scale=1 Width=1916, output: 1916

input: Scale=1.3 Width=1916, output: 1892

input: Scale=4 Width=1916, output 1498

Could the formula be used for any image size?

1

There are 1 best solutions below

1
On

Well, a simple linear regression calculation using python's panda and scipy libraries provided with the following:

X:
Slope: -147.5951226678419
Intercept: 2082.2554723079184
Rvalue: -0.999185570517115
Pvalue: 3.634109826165092e-08
stderr: 2.6655948966701812
R^2: 0.9983718043296126
Predicted f for X = 1 : 1934.6603496400764
Eq
-147.5951226678419 * f + 2082.2554723079184

Y:
Slope: -48.103422946966354
Intercept: 1013.7634787718525
Rvalue: -0.9979519867296809
Pvalue: 3.6417413993094023e-07
stderr: 1.3789237083209072
R^2: 0.9959081678177172
Predicted f for X = 1 : 965.6600558248862
Eq
-48.103422946966354 * f + 1013.7634787718525

Taking 1916x971 into consideration, the following linear regression formulas:

MaxWidth = Width * 1.087 - 147.595 * ScaleX
MaxHeight = Height * 1.043 - 48.103 * ScaleY

I hope this will work for different Width and Height, otherwise it's back to the drawing board