Aim:
Write a function ball_collide that takes two balls as parameters and computes if they are colliding. Your function should return a Boolean representing whether or not the balls are colliding.
Hint: Represent a ball on a plane as a tuple of (x, y, r), r being the radius If (distance between two balls centers) <= (sum of their radii) then (they are colliding)
Program:
import math def ball_collide(x1,y1,r1,x2,y2,r2): dist=math.sqrt((x2-x1)**2+(y2-y1)**2); print("Distance b/w two balls:",dist) center=dist/2; print("Collision point",center); r=r1+r2; print("Sum of radious",r) if(center<=r): print("They are Colliding") return True; else: print("Not Colliding") return False; c=ball_collide(4,4,3,2,2,3) print(c) c=ball_collide(100,200,20,200,100,10) print(c)
Execution:
Distance b/w two balls: 2.8284271247461903 Collision point 1.4142135623730951 Sum of radious 6 They are Colliding True Distance b/w two balls: 141.4213562373095 Collision point 70.71067811865476 Sum of radious 30 Not Colliding False