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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:33:14+00:00 2026-05-29T23:33:14+00:00

I am searching for a way to check a collection (Java TreeSet) of rectangles

  • 0

I am searching for a way to check a collection (Java TreeSet) of rectangles – implemented by a “comparable” Java class using google guavas Range for x and y range – for intersections and holes.
i know that an option could be to use kd-trees, but I have no idea how to build such an kd-tree (for rectangles it should be 4d, shouldn’t it?) and how to get the problem solved (intersection, holes).

the sorting prioritizes the x-axis over y-axis.

EDIT: (try to restate the problem): the use case is to create arbitrary tables (consisting of 2 or 3 blocks of rectangles “header”,”pre column”,”data”). i have to guarantee that there are no intersections and holes (i.e. provided by invalid html or other sources of table data) in each block (besides this the blocks must fit together).
Currently (just got an idea) i try to save in a 2d-array which positions (x,y) are occupied. at the end all position must be occupied exactly once.

  • 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-29T23:33:16+00:00Added an answer on May 29, 2026 at 11:33 pm

    There are quite a few of approaches to solving this type of a problem, each with different pros and cons. Here are some of them:

    Rectangle Pair Intersection + Area Sum

    Look at every pair of rectangles – if the two rectangles intersect, there is an overlap. Add up the rectangle areas and check whether the sum matches the canvas area – if the areas don’t match, there is a gap.

    Painting

    This is the approach you mentioned: create a 2D array that has the dimensions of your canvas. Then, iterate over rectangles and “paint” them into the array.

    One optimization to this approach is coordinate compression. Let’s say that you have rectangles [(10,20), (15,25)] and [(7,3), (15, 25)]. You can look at the distinct x-coordinates (7, 10, 15) and remap them to (0, 1, 2), and the distinct y-coordinates (3, 20, 25) and remap them to (0, 1, 2). Then, you are left with rectangles [(1, 1), (2, 2)] and [(0,0), (2,2)], so you only need a 3×3 array for the painting, instead of a 26×26 array.

    Sweep Line Algorithm

    Sweep a line from left to right, stopping at ‘interesting’ points, and keeping track of which areas are occupied.

    2D Range Trees

    A data structure that can efficiently perform queries over rectangle ranges.

    Which One to Pick?

    It depends on the number of rectangles you have, how they are distributed in the area, how fast your algorithm must be, how much complexity you are willing to take on, etc. The first two algorithms that I mentioned are much simpler than the latter two, so I’d recommend starting there.

    More Info

    If you want learn more about these algorithms, try searching for “rectangle union” online. The most efficient solution is the sweep line algorithm.

    Here are a couple of references on the sweep line algorithm:

    1. What is an Efficient algorithm to find Area of Overlapping Rectangles
    2. http://compgeom.cs.uiuc.edu/~jeffe/open/klee.html
    3. J. L. Bentley, Algorithms for Klee’s rectangle problems. Unpublished notes, Computer Science Department, Carnegie Mellon University, 1977

    Reference 3. is usually given as the original source of the line sweep algorithm for rectangle union, but I have to admit that I didn’t actually find the paper online, perhaps because it is “Unpublished”…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a good way to check a form input using regex to make
I am searching a way to check with C#, if there is an open
I'm searching for a simple and fast way to check if all my Listitems
I am searching the ultimate best way to check if a var is a
I'm searching the way(s) to fill an array with numbers from 0 to a
You can get underground processes by ps ux I am searching a way to
I am searching for a way to compress JavaScript code for the iPhone. Is
I'm searching for a way to auto compare an object propriety to a list
I'm searching for a way to create a menu in an iPhone app that
I was searching for a way of detecting in my C# code if the

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.