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

  • Home
  • SEARCH
  • 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 6690063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:35:34+00:00 2026-05-26T05:35:34+00:00

I’m trying to break up a square or rectangle into a large number of

  • 0

I’m trying to break up a square or rectangle into a large number of randomly sized squares or rectangles so that none are overlapping.

Ok of course others have asked this the best thread I found is
How to fill a square with smaller squares/rectangles?

The solution seems to be either through bin packing or some sort of tree map.

But what I’m looking for is the actual algorithm in Java, Javacript , actionscript or even C.

  • 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-26T05:35:35+00:00Added an answer on May 26, 2026 at 5:35 am

    The provided code creates a k-d tree. You can use this to draw lines on your rectangle that will divide it into smaller rectangles. After you’ve got your tree you can use it as follows to divide your region up into these rectangles:

    1. Pick the node at the root of the tree.
    2. Draw a vertical line through this point.
    3. Choose it’s left child, draw a horizontal line through this point on the left side of the line you just drew through it’s parent (this line stops at the line you just drew).
    4. Choose it’s right child, draw a horizontal line through this point on the right side of the line you just drew through it’s parent (this line also stops at the line you drew through the parent).
    5. Do this recursively, switching between vertical and horizontal lines at each level of the tree.

    Code:

    int MAX_HEIGHT = 100;
    int MAX_WIDTH = 100;
    int NUM_POINTS = 6;
    
    
    // Generate random list of points
    List<Point> pointList = new List<Point>();
    
    Random rand = new Random();
    
    for(int i = 0; i < NUM_POINTS ; i++)
    {
        pointList.add(new Point(rand.nextInt(MAX_HEIGHT), rand.nextInt(MAX_WIDTH));
    }
    
    BinaryTree tree = CreateKDTree(pointList, 0);
    
    
    // Recursive function for creating a K-D Tree from a list of points
    // This tree can be used to draw lines that divide the space up
    // into rectangles.
    public BinaryTree CreateKDTree(List<Point> pointList, int depth)
    {
        // Have to create the PointComparator class that just selects the
        // specified coordinate and sorts based on that
        Coordinate coord= depth % 2 == 0 ? X_COORDINATE : Y_COORDINATE
        Collections.sort(pointList, new PointComparator(coord));
    
        int median = pointList.size() / 2;
    
         // unfortunately Java doesn't have a BinaryTree structure so
         // you have to create this too
        BinaryTree node = new BinaryTree(pointList[median]);
    
        if(pointList.size() == 1) return node;
    
        if(median > 0)
            node.left(CreateKDTree(pointList.subList(0, median), depth + 1);
    
        if(median + 1 < subList.size())
            node.right(CreateKDTree(pointList.subList(median + 1, subList.size()), depth + 1);
    
        return node; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from

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.