I’m writing code that requires me to seek out all factors inside circle on sure elements of the display screen lots of of instances per body. I wrote a quadtree for this and have 1 methodology referred to as query_circle that appears like this:
def query_circle(self, middle, radius, lis=None):
'''
Makes use of given bounds of middle and radius and
creates AABB to look all quadtrees that intersect that area for
the in that area
'''
bounds = [center[0] - radius, middle[1] - radius, radius * 2, radius * 2]
x1, y1 = (bounds[0], bounds[1])
x2, y2 = (bounds[0] + bounds[2], bounds[1] + bounds[3])
if lis == None:
lis = []
if self.nodes:
if y1 <= self.middle[1]:
if x1 <= self.middle[0]:
self.nodes[0].query_circle(middle, radius, lis)
if x2 > self.middle[0]:
self.nodes[1].query_circle(middle, radius, lis)
if y2 > self.middle[1]:
if x1 <= self.middle[0]:
self.nodes[2].query_circle(middle, radius, lis)
if x2 > self.middle[0]:
self.nodes[3].query_circle(middle, radius, lis)
for level in self.factors:
if (level.x - middle[0]) ** 2 + (level.y - middle[1]) ** 2 <= radius**2:
lis.append(level)
return lis
What is occurring is I create an AABB for the circle after which discover all of the QuadTrees which can be intersecting with the AABB and utilizing these QuadTrees to verify if the factors within them are inside the circle. Sadly, the animation was working at lower than 3 frames per second and the foundation trigger was this methodology. I profiled the strategy and located this:
As you’ll be able to seet the final 4 strains of code the place I verify if the purpose is contained in the circle and append it to an inventory are what are slowing down the strategy probably the most. Is there any approach I can use to significantly velocity up that course of?