4
\$\begingroup\$

What is a good formula to use in order to relate two different scale distances to each other? For example, I have a camera that can have a minimum size of 2 and a maximum of 10. I would like the camera to zoom proportionally with the mouse distance from an object.

The problem is the distance is returned in terms of pixels (e.g. 500px) and I'd like to transform that into a relative value between 2 and 10. What's the best way to do this?

Thanks

\$\endgroup\$
4
\$\begingroup\$

Normalization

The best option would probably be to normalize the data. This can easily be done using the formula:

x = a + (X - A) * (b - a) / (B - A)

where a and b are the minimum and maximum values for your desired range, respectively; A and B are the minimum and maximum values for your original range, respectively; X is the input value (in your case, the distance returned in pixels); and x is the normalized version of X.

For your use, a would be 2, b would be 10, A would be the smallest number of pixels possible, B would be the largest number of pixels possible, and X would be 500 (as in your example distance).

You can then use x (the relative value) as you please.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.