Given a ration x:y, make it to ratio of 1:1 or 3:4 or 4:3 by only adding some values to x or y or both.

70 Views Asked by At

This problem is related to programing, for which I am searching an optimal solution.

Problem: I have some images in different height to width ratios. I want to make them in ratios of 1:1 or 3:4 or 4:3, by adding white space. I want to add as little white space as possible, to make it in any of the 3 given ratio. I never want to reduce.

Currently my solution is to create 3 new images for 3 given ratios and then checking for which the program needed to add the least no. of pixel. I then go with that. However currently I am adding pixel to only one side, which I have found is not good and in some cases adding pixel to both the side reduces total no. of white pixel added.

I also have tried searching google, but I don't know what this problem is called i.e what to search for. Hence I am here seeking an insight from you.

I hope the solution to this problem will be with in the limits of high school Maths as anything beyond it, will make the solution harder to implement.

2

There are 2 best solutions below

1
On BEST ANSWER

I am afraid, that the solution does boil down essentially to creating the three images - or at least their sizes, and test.

If the original is $x\times y$, then the smallest fitting $1:1$ is clearly $$\max\{x,y\}\times\max\{x,y\}.$$

For the smallest fitting $4:3$ ratio, let $x'=\cdot\lceil x/4\rceil$, $y'=\cdot \lceil y/3\rceil$ and take $$4\max\{x',y'\}\times 3\max\{x',y'\}.$$

Similarly, for the smallest fitting $3:4$ ratio, let $x'=\cdot\lceil x/3\rceil$, $y'=\cdot \lceil y/4\rceil$ and take $$3\max\{x',y'\}\times 4\max\{x',y'\}.$$

Pick the format that leads to the smallest product.

1
On

If I understand right, so you have you given $$\frac{x}{y}=t$$ and you want to have $$\frac{x+\alpha}{y+\beta}=\frac{4}{3}$$ from the last equation we get $$3x+3\alpha=4y+4\beta$$b with $x=ty$ we obtain $$y(3t-4)=4\beta-3\alpha$$ so $$y=\frac{4\beta-3\alpha}{3t-4}$$ and $$x=\frac{t(4\beta-3\alpha}{3t-4}$$