Rectangle size calculation












-3












$begingroup$


I am trying to calculate rectangle size. I have the rectangle width and height and the ratio between the width and height that has to be met. Resulting width and height of the rectangle have to be an integer(whole number).



ratio is a constant number

width, height are the given numbers

width_r, height_r are the result

width_0, height_0 are the numbers subtracted from the original width and height



Formulas:



width_r/height_r = ratio

width = width_r - width_0

height = height_r - height_0

width > width_r

height > height_r



Ex:



width = 1829

height = 3856

ratio = 16/9

9*width_r = 16*height_r

9(width - width_0) = 16(height - height_0)



I can calculate that the result is width_r = 1824, height_r = 1026. width_0 + height_0 should be the lowest possible while being whole numbers. I can calculate it but i need to create a function that a program can execute.










share|cite|improve this question











$endgroup$












  • $begingroup$
    Closest according to which criteria ? Perimeter, area, min max differences ?
    $endgroup$
    – Damien
    Jan 24 at 21:11










  • $begingroup$
    width_0 + height_0 has to be the smallest possible while being an integer.
    $endgroup$
    – Orion
    Jan 24 at 21:15










  • $begingroup$
    You need more information. Are you enlarging or shrinking? Should the result stay inside the same box or can it grow? To what end do you need it to be "close"? Is any rounding up to the closest pixel ok, or do you need special good approximations (which will strongly depend on the prime factors of both dimensions).
    $endgroup$
    – orion
    Jan 24 at 21:17










  • $begingroup$
    It cannot grow beyond the given width and height, it is shrinking. There should be no decimal numbers. I calculated it by taking the lower number between width and height and then subtracting it one by one until it fit the criteria.
    $endgroup$
    – Orion
    Jan 24 at 21:23










  • $begingroup$
    With Bézout coefficients, you can generate all possible values that satisfy the ratio. It can help finding the minimum
    $endgroup$
    – Damien
    Jan 24 at 21:35
















-3












$begingroup$


I am trying to calculate rectangle size. I have the rectangle width and height and the ratio between the width and height that has to be met. Resulting width and height of the rectangle have to be an integer(whole number).



ratio is a constant number

width, height are the given numbers

width_r, height_r are the result

width_0, height_0 are the numbers subtracted from the original width and height



Formulas:



width_r/height_r = ratio

width = width_r - width_0

height = height_r - height_0

width > width_r

height > height_r



Ex:



width = 1829

height = 3856

ratio = 16/9

9*width_r = 16*height_r

9(width - width_0) = 16(height - height_0)



I can calculate that the result is width_r = 1824, height_r = 1026. width_0 + height_0 should be the lowest possible while being whole numbers. I can calculate it but i need to create a function that a program can execute.










share|cite|improve this question











$endgroup$












  • $begingroup$
    Closest according to which criteria ? Perimeter, area, min max differences ?
    $endgroup$
    – Damien
    Jan 24 at 21:11










  • $begingroup$
    width_0 + height_0 has to be the smallest possible while being an integer.
    $endgroup$
    – Orion
    Jan 24 at 21:15










  • $begingroup$
    You need more information. Are you enlarging or shrinking? Should the result stay inside the same box or can it grow? To what end do you need it to be "close"? Is any rounding up to the closest pixel ok, or do you need special good approximations (which will strongly depend on the prime factors of both dimensions).
    $endgroup$
    – orion
    Jan 24 at 21:17










  • $begingroup$
    It cannot grow beyond the given width and height, it is shrinking. There should be no decimal numbers. I calculated it by taking the lower number between width and height and then subtracting it one by one until it fit the criteria.
    $endgroup$
    – Orion
    Jan 24 at 21:23










  • $begingroup$
    With Bézout coefficients, you can generate all possible values that satisfy the ratio. It can help finding the minimum
    $endgroup$
    – Damien
    Jan 24 at 21:35














-3












-3








-3





$begingroup$


I am trying to calculate rectangle size. I have the rectangle width and height and the ratio between the width and height that has to be met. Resulting width and height of the rectangle have to be an integer(whole number).



ratio is a constant number

width, height are the given numbers

width_r, height_r are the result

width_0, height_0 are the numbers subtracted from the original width and height



Formulas:



width_r/height_r = ratio

width = width_r - width_0

height = height_r - height_0

width > width_r

height > height_r



Ex:



width = 1829

height = 3856

ratio = 16/9

9*width_r = 16*height_r

9(width - width_0) = 16(height - height_0)



I can calculate that the result is width_r = 1824, height_r = 1026. width_0 + height_0 should be the lowest possible while being whole numbers. I can calculate it but i need to create a function that a program can execute.










share|cite|improve this question











$endgroup$




I am trying to calculate rectangle size. I have the rectangle width and height and the ratio between the width and height that has to be met. Resulting width and height of the rectangle have to be an integer(whole number).



ratio is a constant number

width, height are the given numbers

width_r, height_r are the result

width_0, height_0 are the numbers subtracted from the original width and height



Formulas:



width_r/height_r = ratio

width = width_r - width_0

height = height_r - height_0

width > width_r

height > height_r



Ex:



width = 1829

height = 3856

ratio = 16/9

9*width_r = 16*height_r

9(width - width_0) = 16(height - height_0)



I can calculate that the result is width_r = 1824, height_r = 1026. width_0 + height_0 should be the lowest possible while being whole numbers. I can calculate it but i need to create a function that a program can execute.







algebra-precalculus geometry






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Jan 26 at 0:02









J. W. Tanner

2,9541218




2,9541218










asked Jan 24 at 20:56









OrionOrion

12




12












  • $begingroup$
    Closest according to which criteria ? Perimeter, area, min max differences ?
    $endgroup$
    – Damien
    Jan 24 at 21:11










  • $begingroup$
    width_0 + height_0 has to be the smallest possible while being an integer.
    $endgroup$
    – Orion
    Jan 24 at 21:15










  • $begingroup$
    You need more information. Are you enlarging or shrinking? Should the result stay inside the same box or can it grow? To what end do you need it to be "close"? Is any rounding up to the closest pixel ok, or do you need special good approximations (which will strongly depend on the prime factors of both dimensions).
    $endgroup$
    – orion
    Jan 24 at 21:17










  • $begingroup$
    It cannot grow beyond the given width and height, it is shrinking. There should be no decimal numbers. I calculated it by taking the lower number between width and height and then subtracting it one by one until it fit the criteria.
    $endgroup$
    – Orion
    Jan 24 at 21:23










  • $begingroup$
    With Bézout coefficients, you can generate all possible values that satisfy the ratio. It can help finding the minimum
    $endgroup$
    – Damien
    Jan 24 at 21:35


















  • $begingroup$
    Closest according to which criteria ? Perimeter, area, min max differences ?
    $endgroup$
    – Damien
    Jan 24 at 21:11










  • $begingroup$
    width_0 + height_0 has to be the smallest possible while being an integer.
    $endgroup$
    – Orion
    Jan 24 at 21:15










  • $begingroup$
    You need more information. Are you enlarging or shrinking? Should the result stay inside the same box or can it grow? To what end do you need it to be "close"? Is any rounding up to the closest pixel ok, or do you need special good approximations (which will strongly depend on the prime factors of both dimensions).
    $endgroup$
    – orion
    Jan 24 at 21:17










  • $begingroup$
    It cannot grow beyond the given width and height, it is shrinking. There should be no decimal numbers. I calculated it by taking the lower number between width and height and then subtracting it one by one until it fit the criteria.
    $endgroup$
    – Orion
    Jan 24 at 21:23










  • $begingroup$
    With Bézout coefficients, you can generate all possible values that satisfy the ratio. It can help finding the minimum
    $endgroup$
    – Damien
    Jan 24 at 21:35
















$begingroup$
Closest according to which criteria ? Perimeter, area, min max differences ?
$endgroup$
– Damien
Jan 24 at 21:11




$begingroup$
Closest according to which criteria ? Perimeter, area, min max differences ?
$endgroup$
– Damien
Jan 24 at 21:11












$begingroup$
width_0 + height_0 has to be the smallest possible while being an integer.
$endgroup$
– Orion
Jan 24 at 21:15




$begingroup$
width_0 + height_0 has to be the smallest possible while being an integer.
$endgroup$
– Orion
Jan 24 at 21:15












$begingroup$
You need more information. Are you enlarging or shrinking? Should the result stay inside the same box or can it grow? To what end do you need it to be "close"? Is any rounding up to the closest pixel ok, or do you need special good approximations (which will strongly depend on the prime factors of both dimensions).
$endgroup$
– orion
Jan 24 at 21:17




$begingroup$
You need more information. Are you enlarging or shrinking? Should the result stay inside the same box or can it grow? To what end do you need it to be "close"? Is any rounding up to the closest pixel ok, or do you need special good approximations (which will strongly depend on the prime factors of both dimensions).
$endgroup$
– orion
Jan 24 at 21:17












$begingroup$
It cannot grow beyond the given width and height, it is shrinking. There should be no decimal numbers. I calculated it by taking the lower number between width and height and then subtracting it one by one until it fit the criteria.
$endgroup$
– Orion
Jan 24 at 21:23




$begingroup$
It cannot grow beyond the given width and height, it is shrinking. There should be no decimal numbers. I calculated it by taking the lower number between width and height and then subtracting it one by one until it fit the criteria.
$endgroup$
– Orion
Jan 24 at 21:23












$begingroup$
With Bézout coefficients, you can generate all possible values that satisfy the ratio. It can help finding the minimum
$endgroup$
– Damien
Jan 24 at 21:35




$begingroup$
With Bézout coefficients, you can generate all possible values that satisfy the ratio. It can help finding the minimum
$endgroup$
– Damien
Jan 24 at 21:35










1 Answer
1






active

oldest

votes


















0












$begingroup$

I think you just want this: one option is (width,width/ratio), and the other is (height*ratio,height), both rounded down to the nearest integer. Then take the smaller one. This way, it fits to the window along at least one dimension (this is "fit to window" function in image viewers, for instance).



This assumes that any rounded pair of integers is acceptable. However, if you want to put restrictions on how much deviation in ratio=width/height from the desired ratio is acceptable, you need more information. You need a trade-off condition between how much you shrink and how wrong the ratio is. Do you allow shrinking by at most $n$ pixels and pick the best result? Do you impose a minimum acceptable error in aspect ratio and pick the first acceptable choice? Do you construct a trade-off as a weighted sum of the shrinkage and ratio error, and find the minimum? You need to make a choice, there is no "best" selection.



As this is probably about image resizing, I'd say the nearest integer rounded result is what you want, all the rest is just splitting hairs.






share|cite|improve this answer









$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "69"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3086341%2frectangle-size-calculation%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0












    $begingroup$

    I think you just want this: one option is (width,width/ratio), and the other is (height*ratio,height), both rounded down to the nearest integer. Then take the smaller one. This way, it fits to the window along at least one dimension (this is "fit to window" function in image viewers, for instance).



    This assumes that any rounded pair of integers is acceptable. However, if you want to put restrictions on how much deviation in ratio=width/height from the desired ratio is acceptable, you need more information. You need a trade-off condition between how much you shrink and how wrong the ratio is. Do you allow shrinking by at most $n$ pixels and pick the best result? Do you impose a minimum acceptable error in aspect ratio and pick the first acceptable choice? Do you construct a trade-off as a weighted sum of the shrinkage and ratio error, and find the minimum? You need to make a choice, there is no "best" selection.



    As this is probably about image resizing, I'd say the nearest integer rounded result is what you want, all the rest is just splitting hairs.






    share|cite|improve this answer









    $endgroup$


















      0












      $begingroup$

      I think you just want this: one option is (width,width/ratio), and the other is (height*ratio,height), both rounded down to the nearest integer. Then take the smaller one. This way, it fits to the window along at least one dimension (this is "fit to window" function in image viewers, for instance).



      This assumes that any rounded pair of integers is acceptable. However, if you want to put restrictions on how much deviation in ratio=width/height from the desired ratio is acceptable, you need more information. You need a trade-off condition between how much you shrink and how wrong the ratio is. Do you allow shrinking by at most $n$ pixels and pick the best result? Do you impose a minimum acceptable error in aspect ratio and pick the first acceptable choice? Do you construct a trade-off as a weighted sum of the shrinkage and ratio error, and find the minimum? You need to make a choice, there is no "best" selection.



      As this is probably about image resizing, I'd say the nearest integer rounded result is what you want, all the rest is just splitting hairs.






      share|cite|improve this answer









      $endgroup$
















        0












        0








        0





        $begingroup$

        I think you just want this: one option is (width,width/ratio), and the other is (height*ratio,height), both rounded down to the nearest integer. Then take the smaller one. This way, it fits to the window along at least one dimension (this is "fit to window" function in image viewers, for instance).



        This assumes that any rounded pair of integers is acceptable. However, if you want to put restrictions on how much deviation in ratio=width/height from the desired ratio is acceptable, you need more information. You need a trade-off condition between how much you shrink and how wrong the ratio is. Do you allow shrinking by at most $n$ pixels and pick the best result? Do you impose a minimum acceptable error in aspect ratio and pick the first acceptable choice? Do you construct a trade-off as a weighted sum of the shrinkage and ratio error, and find the minimum? You need to make a choice, there is no "best" selection.



        As this is probably about image resizing, I'd say the nearest integer rounded result is what you want, all the rest is just splitting hairs.






        share|cite|improve this answer









        $endgroup$



        I think you just want this: one option is (width,width/ratio), and the other is (height*ratio,height), both rounded down to the nearest integer. Then take the smaller one. This way, it fits to the window along at least one dimension (this is "fit to window" function in image viewers, for instance).



        This assumes that any rounded pair of integers is acceptable. However, if you want to put restrictions on how much deviation in ratio=width/height from the desired ratio is acceptable, you need more information. You need a trade-off condition between how much you shrink and how wrong the ratio is. Do you allow shrinking by at most $n$ pixels and pick the best result? Do you impose a minimum acceptable error in aspect ratio and pick the first acceptable choice? Do you construct a trade-off as a weighted sum of the shrinkage and ratio error, and find the minimum? You need to make a choice, there is no "best" selection.



        As this is probably about image resizing, I'd say the nearest integer rounded result is what you want, all the rest is just splitting hairs.







        share|cite|improve this answer












        share|cite|improve this answer



        share|cite|improve this answer










        answered Jan 24 at 21:23









        orionorion

        13.7k11837




        13.7k11837






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Mathematics Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3086341%2frectangle-size-calculation%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mario Kart Wii

            What does “Dominus providebit” mean?

            Antonio Litta Visconti Arese