# Answer to Question #38568 in Python for Shane

Question #38568

The computer game function collision () checks whether two circular objects collide; it returns True if they do and False otherwise. Each circular object will be given by it's radius and the (x,y) coordinates of it's center. Thus the function will take six numbers as input: the coordinates x1 and y1 of the center and the radius r1 of the first circle, and the coordinates x2 and y2 and the radius r2 of the second circle.

>>> collision (0,0,,3,0,5,3)

true

>>> collision(0,0,1.4,2,2,1.4)

false

>>> collision (0,0,,3,0,5,3)

true

>>> collision(0,0,1.4,2,2,1.4)

false

Expert's answer

import math

def collision(x1, y1, r1, x2, y2, r2):

if math.pow( math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2), 0.5) <= r2 + r1:

return True

else:

return False

print collision(0, 0, 5, 7, 7, 10)

print collision(0, 0, 2, 7, 0, 3)

def collision(x1, y1, r1, x2, y2, r2):

if math.pow( math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2), 0.5) <= r2 + r1:

return True

else:

return False

print collision(0, 0, 5, 7, 7, 10)

print collision(0, 0, 2, 7, 0, 3)

## Comments

## Leave a comment