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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:41:47+00:00 2026-06-02T08:41:47+00:00

I add 2 objects to the QuadTree but when I look through the whole

  • 0

I add 2 objects to the QuadTree but when I look through the whole list for the objects I can only find 1 object. Why is this and what can I do to fix it?

from pygame import draw

class QuadTree(object):
    def __init__(self, box, current_level, max_level=3):# box (top_left_x, top_left_y, size_x, size_y)
        self.location = (box[0], box[1])
        self.size = (box[2], box[3])
        self.current_level = current_level
        self.max_level = max_level

        self.objects = []
        self.__setupchirldren__()

    def __setupchirldren__(self):
        self.top_right =        None
        self.top_left =         None
        self.bottom_right =     None
        self.bottom_left =      None

    def elements(self):
        if self.current_level == self.max_level:
            for x in self.objects:
                print x, x.rect
        else:
            if self.bottom_left != None:
                self.bottom_left.elements()
            if self.bottom_right != None:
                self.bottom_right.elements()
            if self.top_left != None:
                self.top_left.elements()
            if self.top_right != None:
                self.top_right.elements()

    def add_object(self, new_object, rect):
        if self.current_level == self.max_level:
            #print new_object, rect
            self.objects.append(new_object)
            #print self.objects
        else:
            half_size = (self.size[0]/2, self.size[1]/2)
            if rect.colliderect(self.location, half_size):
                self.top_left =         QuadTree((self.location[0], self.location[1], half_size[0], half_size[1]), self.current_level+1, self.max_level)
                self.top_left.add_object(new_object, rect)
            if rect.colliderect((self.location[0]+half_size[0], self.location[1]), half_size):
                self.top_right =        QuadTree((self.location[0]+half_size[0], self.location[1], half_size[0], half_size[1]), self.current_level+1, self.max_level)
                self.top_right.add_object(new_object, rect)
            if rect.colliderect((self.location[0], self.location[1]+half_size[1]), half_size):
                self.bottom_left =      QuadTree((self.location[0], self.location[1]+half_size[1], half_size[0], half_size[1]), self.current_level+1, self.max_level)
                self.bottom_left.add_object(new_object, rect)
            if rect.colliderect((self.location[0]+half_size[0], self.location[1]+half_size[1]), half_size):
                self.bottom_right =     QuadTree((self.location[0]+half_size[0], self.location[1]+half_size[1], half_size[0], half_size[1] ), self.current_level+1, self.max_level)
                self.bottom_right.add_object(new_object, rect)

    def draw(self, screen):
        #if self.current_level == self.max_level:
        draw.line(screen, (255, 0, 0), self.location, (self.location[0]+self.size[0], self.location[1]))
        draw.line(screen, (255, 0, 0), self.location, (self.location[0], self.location[1]+self.size[1]))
        draw.line(screen, (255, 0, 0), (self.location[0]+self.size[0], self.location[1]+self.size[1]), (self.location[0]+self.size[0], self.location[1]))
        draw.line(screen, (255, 0, 0), (self.location[0]+self.size[0], self.location[1]+self.size[1]), (self.location[0], self.location[1]+self.size[1]))
        if self.current_level != self.max_level:
            if self.bottom_left != None:
                self.bottom_left.draw(screen)
            if self.bottom_right != None:
                self.bottom_right.draw(screen)
            if self.top_left != None:
                self.top_left.draw(screen)
            if self.top_right != None:
                self.top_right.draw(screen)

    def get_elements(self, rect):
        #ret = self.objects
        if self.current_level == self.max_level:
            #print self.objects
            return self.objects
        else:
            half_size = (self.size[0]/2, self.size[1]/2)
            if self.top_left!= None and rect.colliderect((self.location, half_size)):
                return self.top_left.get_elements(rect)
                #for x in self.top_left.get_elements(rect):
                #   ret.append(x)
            if self.top_right!= None and rect.colliderect(((self.location[0]+self.size[0]/2, self.location[1]), half_size)):
                return self.top_right.get_elements(rect)
                #for x in self.top_right.get_elements(rect):
                #   ret.append(x)
            if self.bottom_left!= None and rect.colliderect(((self.location[0], self.location[1]+self.size[1]/2), half_size)):
                return self.bottom_left.get_elements(rect)
                #for x in self.bottom_left.get_elements(rect):
                #   ret.append(x)
            if self.bottom_right!= None and rect.colliderect(((self.location[0]+self.size[0]/2, self.location[1]+self.size[1]/2), half_size)):
                return self.bottom_right.get_elements(rect)
                #for x in self.bottom_right.get_elements(rect):
                #   ret.append(x)
            #print ret
        return []

When I insert objects it prints out

platform.Platforms object at 0x0236F950 platform.Platforms object at
0x0236F950 platform.Platforms object at 0x0236F950 platform.Platforms
object at 0x0236F950 platform.Platforms object at 0x0236FAB0
platform.Platforms object at 0x0236FAB0 platform.Platforms object at
0x0236FAB0 platform.Platforms object at 0x0236FAB0

which is good I want two distinct objects in mutable trees
but when I call it it only has the second one in the list

so i made a function

def elements(self):
    if self.current_level == self.max_level:
        for x in self.objects:
            print x, x.rect
    else:
        if self.bottom_left != None:
            self.bottom_left.elements()
        if self.bottom_right != None:
            self.bottom_right.elements()
        if self.top_left != None:
            self.top_left.elements()
        if self.top_right != None:
            self.top_right.elements()

which prints out

platform.Platforms object at 0x02320A70 rect(350, 630, 110, 110)
platform.Platforms object at 0x02320A70 rect(350, 630, 110, 110)
platform.Platforms object at 0x02320A70 rect(350, 630, 110, 110)
platform.Platforms object at 0x02320A70 rect(350, 630, 110, 110)

  • 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-02T08:41:49+00:00Added an answer on June 2, 2026 at 8:41 am

    Your add_object class makes a new lower-level quadtree every time, even if there is already one there.

    I was going to suggest this on your earlier quadtree question (pretty sure that was you) but you removed it before I had a chance: you’ll probably be better off if the QuadTree class has a method that finds-or-creates the appropriate sub-tree given a location (what you call rect in both add_objects and get_elements). Note that this assumes that each object lives entirely within one sub-tree, which is true for points but not for arbitrary rectangles. (The most obvious case is that a very large rectangle—one that covers the entire field—occupies all four sub-trees of any given level of quadtree.)

    Assuming that the logic in get_elements is basically correct, for instance, you might define something like:

    def find_or_create(self, rect, create = False):
        "find or create the sub-tree that contains the given point"
        if self.current_level == self.max_level:
            return self # we contain it
        half_size = (self.size[0]/2, self.size[1]/2)
        if rect.collide_rect((self.location, half_size)):
            name = 'top_left'
            location = self.location
        else if rect.collide_rect(...):
            name = 'top_right'
            location = ... # top right location
        else ... [as before, each time setting name and location]
        # now, after deciding which sub-quadrant applies...
        quad = getattr(self, name)
        # if the sub-quadrant already exists, recurse
        if quad:
            return quad.find_or_create(rect, create)
        # otherwise, if we are supposed to create it, do that
        if create:
            quad = QuadTree(...) # this is why you also have to compute "location"
            setattr(self, name, quad)
        return quad # return the new quadtree, or None, as appropriate
    

    This gives you a way to find the containing quadtree (returning None if it does not exist) or, for adding objects, creating it (returning the existing max-level quadtree, or creating a new max-level quadtree in the appropriate quadrant).

    The getattr and setattr operations let you use the name you computed to get and set self.top_left, self.top_right, and so on.

    Adding an object is then pretty trivial:

        quad = self.find_or_create(rect, True)
        quad.objects.append(new_object)
    

    There’s a better way to handle the location computation-and-test as well but I leave that as an exercise. (And, of course, if rects can span multiple sub-trees—if they are not points—you’ll need to compute a list of all the appropriate sub-trees, rather than the single appropriate top-left / top-right / bottom-left / bottom-right sub-tree.)

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

Sidebar

Related Questions

We can have an NSMutableArray object, and add objects to it. But CGPoint is
I believe you can add objects use list.add(); However, is there an alternative way
I'm trying to add objects to this NSArray (labelArray) but for some reason, it
I try to add objects to a List<String> instance but it throws an UnsupportedOperationException
Suppose I have several javascript object {type:gotopage,target:undefined} {type:press,target:a} {type:rotate,target:long} How can I add this
I'm trying to add objects to NSMutableArray (categoriasArray), but its not done by the
So I need to add the objects from an NSArray that the user has
I have one custom action (add) and two custom objects (favorites list) and (tv
I have a situation where i want to add LinePragmas to CodeDom objects. But
I currently add objects to an array with a label taken from the textfield

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.