Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8131531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:07:08+00:00 2026-06-06T09:07:08+00:00

Given a known number of images with the same width to height ratio and

  • 0

Given a known number of images with the same width to height ratio and dimensions, is there an algorithm that determines the best way to present them in a screen whose resolution may vary? Aka arrange them single page or double page.

For example, to determine if you can present two images on the screen or, given the width/height, to “detect” that only one would look better. To see if it’s better to have them fit on width, with some blank space above and/or below them, or to fit on height, with blank space on the left/right.

I have made some attempts to determine such an algorithm of my own, but I’m not completely satisfied and I was hoping there might be a better solution or maybe some advices.

Unfortunately, it’s not as simple as “width > height => two images otherwise one image”, as I found out.

Summed up, right now I do all my calculations based on the image height first and then check if the screen width’s is larger than 1.5 x my image width. Larger means I decrease the height to have two images fit, smaller means I only present one image. Still, I keep getting some undesired results for certain combinations of image sizes / screen resolutions.

If you stumbled upon the same problem and have some code or tips to spare, it would be greatly appreciated.

P.S. As you may figure out, this is about presenting a magazine.

[edit] I forgot to mention that I’m using javascript (vanilla, no plugins/frameworks) for the coding

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:07:09+00:00Added an answer on June 6, 2026 at 9:07 am

    To answer my own question, it seems there isn’t such an algorithm or at least I wasn’t able to find one. The kd-tree suggested is a waaaaaaaaaaaaay too complicated overkill solution to be practical.

    In the end, I settled for the one I came up with myself:

    • first I check the window height, to see if the largest image fits. If it doesn’t, I check if a certain percentage (75%) does. If it’s still to large, I downsize the heights
    • once I determine a height that fits, I check if two images fit in the total width. If they do, this means I have a double-page mode, otherwise is single page mode

    Here is a vanilla javascript implementation

    var selCase = 0; //an "ok" var
    //pageH = array with page heights
    //pageW = array with page widths
    //currentWH = window height
    //currentWW = windowWidth
    
     for(i = pageH.length - 1; i >= 0; i--) {
        if(!selCase) {
            if(pageH[i] <= currentWH) { //if the page height is lower than the total height
                if(pageW[i] * 2 <= currentWW) { //if we have room for double pages
                    currentMode = 'L';
                    currentPW = pageW[i];
                    currentPH = pageH[i];
                    selCase = 11;
                }else {
                    if(currentWW > currentWH) { //if the width is bigger, double-page mode is prefered
                        if((pageW[i] * 1.5) >= (currentWW - 20)) { //there wouldn't be much space left for double-pages 
                            currentMode = 'P';
                            currentPW = pageW[i];
                            currentPH = pageH[i];
                            selCase = 12;
                        } // else: the height fits, but not the width so a smaller height is required
                    }else { //standard portrait mode
                        if(pageW[i] <= (currentWW - 20)) {
                            currentMode = 'P';
                            currentPW = pageW[i];
                            currentPH = pageH[i];
                            selCase = 13;
                        }
                    }
                }   
            }else { //we check to see if maybe we can shrink the page a little to fit in the total height
                var sPerc; //scale percentage
                sPerc = currentWH * 100 / pageH[i];
                if(sPerc >= perc) {
                    var newW = pageW[i] * sPerc / 100; 
                    if(newW * 2 <= (currentWW - 20)) { //if we have room for two also-scaled pages
                        currentMode = 'L';
                        currentPW = Math.floor(newW);
                        currentPH = currentWH;
                        selCase = 21; 
                    }else { 
                        if(currentWW > currentWH) { //same as before
                            if((newW * 1.5) >= (currentWW - 20)) { //only one scaled page fits
                                currentMode = 'P';
                                currentPW = Math.floor(newW);
                                currentPH = currentWH;
                                selCase = 22;
                            }
                        }else {
                            if(newW < (currentWW - 20)) {
                                currentMode = 'P';
                                currentPW = Math.floor(newW);
                                currentPH = currentWH;
                                selCase = 23;
                            }
                        }
                    }
                }
            }
        }       
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a list of x,y coordinates and a known width & height how can
Given a node x in an undirected graph that is known to be part
Let E be a given directed edge set. Suppose it is known that the
What is the best practise for storing a large number of files that are
Given the known weaknesses of MD5 and the recent (May 2009) weaknesses discussed in
I have an Excel Workbook which has some VSTO-based c# code. Given a known
Given a date and time structured as: 2011-10-21 14:44:19 which is known to be
Given that I don't know at deployment time what kinds of system my code
Given int a; , I know that the following returns the largest value that
I have a folder that contains 1000+ sub folders. In each subfolder there are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.