This is an intersection-detection algorithm I developed as an alternative method to the one developed for my coursework. I won't post some of the other functions as they are used in the coursework too, feel free to ask if their names aren't self explanatory. This function simply returns whether 2 bcw
objects are intersecting. bcw
objects are either boxes or circles. What I'd like is any comments on my algorithm/coding style to help make me a better programmer.
def bcw_do_collide_i(obj_A,obj_B,tracer=None, accuracy = 30):
""" Check whether Box-Circle World objects obj_A
and obj_B collode. If so, return True,
otherwise return False.
"""
# items to be checked
comp1 = [obj_A]
comp2 = [obj_B]
# does a component circumcircle collide, True = collides
truth1 = [False]
truth2 = [False]
# Depth used to limit iterations to accuracy
depth = 0
while len(comp1)!=0 and depth<accuracy:
# Find any non colliding components
for c1,t1 in zip(comp1,range(len(truth1))):
circum_1 = bcw_circumcircle(c1)
for c2,t2 in zip(comp2,range(len(truth2))):
circum_2 = bcw_circumcircle(c2)
if tracer <> None:
tracer([c1,c2,circum_1,circum_2], ["k--","b--","r--","r--"])
if bcw_do_circles_overlap(circum_1,circum_2) == True:
truth1[t1] = True
truth2[t2] = True
# Check for colliding components
in_1 = bcw_incircle(c1)
in_2 = bcw_incircle(c2)
if tracer <> None:
tracer([c1,c2,in_1,in_2],
["g--","b--","r--","r--"])
if bcw_do_circles_overlap(in_1,in_2): return True
# Subdivide all components whose outers collide
sub = []
for c1,t1 in zip(comp1,truth1):
if t1:
sub+=bcw_components(c1)
comp1 = sub[::]
truth1 = [False for i in comp1]
sub =[]
for c2,t2 in zip(comp2,truth2):
if t2:
sub+=bcw_components(c2)
comp2 = sub[::]
truth2 = [False for i in comp1]
depth+=1
return False
I was tempted by using the bcw
objects as dictionary keys but they had to be mutable.