Basic Concept
I am currently developing a small tool designed to assist with geometric calculations for print-related items.
Project Overview
In this tool, users are asked to input the width and height of a box, represented visually as a CSS-based box, through two input fields labeled w
and h
.
The challenge lies in accurately displaying the measurements within a limited space, with the box constrained to a maximum size of 69 x 69
pixels.
To address this, my goal is to scale the dimensions of the box proportionally based on the longer of the two input values, while ensuring that it does not exceed the maximum size.
Technical Approach
Despite not being mathematically inclined, I have devised a function to achieve this purpose:
updateRectBox: function(w, h){
var max_x = 69;
var max_y = 69;
var factor, x, y;
factor = w / h;
if (w == h) {
x = max_x;
y = max_y;
} else {
if (w > h) {
x = max_x;
y = (factor > 1 ? max_y / factor : max_y * factor);
} else {
x = (factor > 1 ? max_x / factor : max_x * factor);
y = max_y;
}
}
jQuery('#rect').css({
'width': (x) + 'px',
'height': (y) + 'px'
});
}
While this function serves its purpose effectively, I believe there is room for optimization and simplification.
Seeking Refinement
Considering my limitations in mathematical expertise, I am reaching out for suggestions on refining and streamlining the code for a more elegant implementation. I have provided a working fiddle to facilitate testing and experimentation.