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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:59:07+00:00 2026-06-17T04:59:07+00:00

I have a list of rectangles stored as points. Currently, the data looks something

  • 0

I have a list of rectangles stored as points. Currently, the data looks something like this:

boxes = [{'p1': (0,0), 'p2': (100,20)},
         {'p1': (5,5), 'p2': (15,15)}, 
         {'p1': (20,5), 'p2': (30,15)},
         {'p1': (35,5), 'p2': (45,15)},
         {'p1': (70,5), 'p2': (80,15)}]

I also have a basic function that tests if a rectangle is contained within another:

def isChild(b1,b2):
    return (b1 != b2 and 
            (b1['p2'][0]-b1['p1'][0] * b1['p2'][1]-b1['p1'][1]) > (b2['p2'][0]-b2['p1'][0] * b2['p2'][1]-b2['p1'][1]) and
            b1['p1'][0] < b2['p2'][0] and 
            b1['p2'][0] > b2['p1'][0] and 
            b1['p1'][1] < b2['p2'][1] and 
            b1['p2'][1] > b2['p1'][1])

I’d like to wind up with a set of rectangles and their children, but I’m not sure how I should store them. I’m thinking something like this:

[{'p1': (0,0), 
  'p2': (100,20),
  'children': [{'p1': (5,5), 'p2': (15,15)}, 
               {'p1': (20,5), 'p2': (30,15)},
               {'p1': (35,5), 'p2': (45,15)},
               {'p1': (70,5), 'p2': (80,15)}]}]

The context of this is that the boxes represent elements on a web page. The data structure is meant to generate the basic markup, so the structure above would wind up being something like this:

<div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>
  1. Are the source/goal data structures a good fit for this? If not, what is?
  2. How can I get from the source to the goal?
  3. A friend suggested using r-trees. Would that make sense here?
  • 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-17T04:59:08+00:00Added an answer on June 17, 2026 at 4:59 am

    Use classes. This is a typical usecase for OO programming. Create a class Rectangle

    from random import random
    
    class Rectangle(object):
        def __init__(self, x1, y1, x2, y2):
            self._p1 = (x1, y1)
            self._p2 = (x2,y2)
            self._children = []
    
        def __str__(self):
            return "Rectangle defined by %s, %s, %i children" % (self._p1, self._p2, len(self._children))
    
        def is_child_of(self, other):
            return (self is not other and 
                self._p1[0] > other._p1[0] and 
                self._p2[0] < other._p2[0] and 
                self._p1[1] > other._p1[1] and 
                self._p2[1] < other._p2[1])
    
        def add_child(self, other):
            self._children.append(other)
    
        def check_relation_and_connect(self, other):
            if self.is_child_of(other):
                other.add_child(self)
            elif other.is_child_of(self):
                self.add_child(other)
    
    
    if __name__=="__main__":
        rectangles = [Rectangle(random()*5, random()*5, random()*5+5, random()*5+5) for i in range(10)]
    
        for i in range(len(rectangles)):
            for j in range(i):
                rectangles[i].check_relation_and_connect(rectangles[j])
    
        for rectangle in rectangles:
            print rectangle
    

    The class consist of two points, _p1 and _p2, and a list of children. The logic of finding parent-child relation goes into a method of this class (btw, does your method work? I changed it, as it returned nonsense for me. Maybe I have a different understanding of how the rectangle is defined.)

    As you’re talking about websites and <div>, I assume you won’t have thousands of rectangles. So this approach should be fine.

    This example can also be extended to plot all rectangles, so one can check the rectangles and the calculated kinship. Keeping class Rectangle unchanged, one can write:

    if __name__=="__main__":
        import matplotlib.pyplot as plt
        from matplotlib import patches
    
        rectangles = [Rectangle(random()*5, random()*5, random()*5+5, random()*5+5) for i in range(5)]
    
        for i in range(len(rectangles)):
            for j in range(i):
                rectangles[i].check_relation_and_connect(rectangles[j])
    
        for rectangle in rectangles:
            print rectangle
    
        colormap = plt.get_cmap("Paired")
        for i, rect in enumerate(rectangles):
            ax = plt.axes()
            color = colormap((1.*i)/len(rectangles))
            patch = patches.Rectangle(rect.p1, rect.p2[0]-rect.p1[0], rect.p2[1]-rect.p1[1], fc="none", ec=color, lw=2)
            ax.add_patch(patch)
        plt.xlim(-1,11)
        plt.ylim(-1,11)
        plt.show()
    

    This gives a plot like:
    enter image description here

    For this example, exactly one Rectangle had a child (violet is a child of green).

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

Sidebar

Related Questions

I have a theoretical grid of overlapping rectangles that might look something like this:
I have a square shape like this that is devided to 9 rectangles: So
I have code like this: IntPtr hwndGetValue = new IntPtr(67904); List<IntPtr> windows = GetChildWindows(hwndGetValue);
I have a list of rectangles and a list of points. I want to
I have list of input area in the form with id like contact1_title, contact2_title,
I have list of structure. I want to modify a particular data from the
I have, as input, an arbitrary formation, which is a list of rectangles, F
I have a list component in my report that displays a set of data.Its
I have a set of rectangles and I would like to reduce the set
I have this image : And i want take out the triangle and rectangles

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.