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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:26:00+00:00 2026-05-26T03:26:00+00:00

let’s say I have a huge set of non-overlapping rectangle with integer coordinates, who

  • 0

let’s say I have a huge set of non-overlapping rectangle with integer coordinates, who are fixed once and for all

I have another rectangle A with integer coordinates whose coordinates are moving (but you can assume that its size is constant)

What is the most efficient way to find which rectangles are intersecting (or inside) A?
I cannot simply loop through my set as it is too big. Thanks

edit : the rectangles are all parallel to the axis

  • 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-26T03:26:01+00:00Added an answer on May 26, 2026 at 3:26 am

    Personally, I would solve this with a KD-Tree or a BIH-Tree. They are both adaptive spatial data structures that have a log(n) search time. I have an implementation of both for my Ray Tracer, and they scream.

    — UPDATE —

    Store all of your fixed rectangles in the KD-Tree. When you are testing intersections, iterate through the KD-Tree as follows:

    function FindRects(KDNode node, Rect searchRect, List<Rect> intersectionRects)
    
    // searchRect is the rectangle you want to test intersections with
    // node is the current node. This is a recursive function, so the first call
    //    is the root node
    // intersectionRects contains the list of rectangles intersected
    
    int axis = node.Axis;
    
    // Only child nodes actually have rects in them
    if (node is child)
    {
        // Test for intersections with each rectangle the node owns
        for each (Rect nRect in node.Rects)
        {
            if (nRect.Intersects(searchRect))
                  intersectionRects.Add(nRect);
        }
    }
    else
    {
        // If the searchRect's boundary extends into the left bi-section of the node
        // we need to search the left sub-tree for intersections
        if (searchRect[axis].Min  // Min would be the Rect.Left if axis == 0, 
                                  // Rect.Top if axis == 1
                    < node.Plane) // The absolute coordinate of the split plane
        {
            FindRects(node.LeftChild, searchRect, intersectionRects);
        }
    
        // If the searchRect's boundary extends into the right bi-section of the node
        // we need to search the right sub-tree for intersections
        if (searchRect[axis].Max  // Max would be the Rect.Right if axis == 0
                                  // Rect.Bottom if axis == 1
                    > node.Plane) // The absolute coordinate of the split plane
        {
            FindRects(node.RightChild, searchRect, intersectionRects);
        }
    }
    

    This function should work once converted from pseudo-code, but the algorithm is correct. This is a log(n) search algorithm, and possibly the slowest implementation of it (convert from recursive to stack based).

    — UPDATE — Added a simple KD-Tree building algorithm

    The simplest form of a KD tree that contains area/volume shapes is the following:

    Rect bounds = ...; // Calculate the bounding area of all shapes you want to 
                  // store in the tree
    int plane = 0; // Start by splitting on the x axis
    
    BuildTree(_root, plane, bounds, insertRects);
    
    function BuildTree(KDNode node, int plane, Rect nodeBds, List<Rect> insertRects)
    
    if (insertRects.size() < THRESHOLD /* Stop splitting when there are less than some
                                          number of rects. Experiment with this, but 3
                                          is usually a decent number */)
    {
         AddRectsToNode(node, insertRects);
         node.IsLeaf = true;
         return;
    }
    
    float splitPos = nodeBds[plane].Min + (nodeBds[plane].Max - nodeBds[plane].Min) / 2;
    
    // Once you have a split plane calculated, you want to split the insertRects list
    // into a list of rectangles that have area left of the split plane, and a list of
    // rects that have area to the right of the split plane.
    // If a rect overlaps the split plane, add it to both lists
    List<Rect> leftRects, rightRects;
    FillLists(insertRects, splitPos, plane, leftRects, rightRects); 
    
    Rect leftBds, rightBds; // Split the nodeBds rect into 2 rects along the split plane
    
    KDNode leftChild, rightChild; // Initialize these
    // Build out the left sub-tree
    BuildTree(leftChild, (plane + 1) % NUM_DIMS, // 2 for a 2d tree
              leftBds, leftRects);
    // Build out the right sub-tree
    BuildTree(rightChild, (plane + 1) % NUM_DIMS,
              rightBds, rightRects);
    
    node.LeftChild = leftChild;
    node.RightChild = rightChild;
    

    There a bunch of obvious optimizations here, but build time is usually not as important as search time. That being said, a well build tree is what makes searching fast. Look up SAH-KD-Tree if you want to learn how to build a fast kd-tree.

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

Sidebar

Related Questions

Let's say that I have a set of relations that looks like this: relations
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have some text as follows: do this, do that, then this,
let's say I have the following string: string s = A B C D
Let's say I have deployed my Rails app on a VPS, and everything works
Let's see if I make myself clear. I have an old set of scripts

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.