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 537931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:58:44+00:00 2026-05-13T09:58:44+00:00

I am trying to layout a bunch of overlapping rectangles that start out like

  • 0

I am trying to layout a bunch of overlapping rectangles that start out like this:

alt text http://img690.imageshack.us/img690/209/picture1bp.png

The 2-pass algorithm I thought up is roughly:

// Pass 1 - Move all rectangles to the right until they do not overlap any other rectangles
rects = getRectsSortedOnTopLeft(); // topmost first, all rects same size
foreach(rect in rects)
{
   while(rect.collidingRects().size() != 0)
   {
       rect.x += RECT_SIZE;
   }
}

This (probably) ends up with rectangles laid out like:
alt text http://img685.imageshack.us/img685/9963/picture2bc.png

This is not aesthetically pleasing so I thought of a second pass which would move them all left starting again from the topmost:

// Pass 2
foreach(rect in rects)
{
    while(rect.x >= LEFT_MARGIN)
    {
        assert(rect.collidingRects().size() == 0);
        rect.x -= RECT_WIDTH;
        if(rect.collidingRects().size() != 0)
        {
           rect.x += RECT_WIDTH;
           break;
        }
    }
}

I think this should end up looking like below (looks exactly correct in practice):

alt text http://img511.imageshack.us/img511/7059/picture3za.png

However, I am wary of this algorithm because I am not sure if it will lay out correctly in all cases and it may be really slow. Do you think this algorithm can work? Can you make some suggestions on a better algorithm?

  • 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-05-13T09:58:44+00:00Added an answer on May 13, 2026 at 9:58 am

    I think that this problem is of polynomial complexity. Assuming your example’s limitation of only two rectangles overlapping at any given point is not a true limitation of the problem, you would need to try every possible order of bumping the rectangles to the right in order to produce the optimal (least wide) result. This is a form of space packing problem, and those are Hard unless your data set is small enough to brute force.

    However, one small improvement to your pseudocode is possible, which would improve its performance in many cases.

    Consider this desired final result:

    A
    A C
    A C E
    A C E
    B C E
    B D E
    B D F
    B D F
      D F
        F
    

    (where all four of one character are a single rectangle)

    Your first pass would move everything except A to the right, forming a staircase. Then in the second pass your code would decline to move B to the left margin, because the first attempt to move it would overlap with E. What you need to do is start at the left margin and check for the leftmost position you can move each rectangle to in pass 2.

    Pseudocode:

    // Pass 1 - Move all rectangles to the right until they do not overlap any other rectangles
    rects = getRectsSortedOnTopLeft(); // topmost first, all rects same width
    foreach(rect in rects)
        while(rect.collidingRects())
            rect.x += RECT_WIDTH;
    // Pass 2 - Move all rectangles to the leftmost position in which they don't overlap any other rectangles
    foreach(rect in rects)
        for(i=LEFT_MARGIN; i+=RECT_WIDTH; i<rect.x)
        {
            o = rect.x;
            rect.x = i;
            if(rect.collidingRects())
                rect.x = o;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a layout similar to this: alt text http://img20.imageshack.us/img20/3533/stackn.png Here's the
I'm trying to add a bunch of buttons to a layout like this: for(
I'm trying to layout field labels and values like this: Name: Bob Age: 25
I am trying to make Layout that contains 2 layout, vertical and horizontal scrolls.
I'm trying to get this css layout to work with IE7 and I'm a
I'm trying to change the layout of a databound treeview from this: To this:
I am trying to make a chatroom layout like the following: Now my problem
I am trying to create a bunch of dynamic helper methods like these: show_admin_sidebar
I'm trying to layout a Rails app using Devise for authentication. I'd like to
I've got a form that I'm trying to layout semantically and format with CSS.

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.