EXERCISE - 8(a)
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).
We need to write a function to find out whether two balls are colliding or not by using the distance formaulae i.e,, If (distance between two balls centers) <= (sum of their radii) then we must return True. Otherwise, we must return False. Distance between two ball centers can be calculated by using the formulae:
Here, We are using the concept functions.
Functions:
A function is a group of related statements that performs a specific task. Funtions helps break our program into smaller and modular chunks. As our program grows longer, functions make it more organized and manageable.
Further more, It avoids repetition and code reusable. The general form of a function is as follows:
def function_name(parameter_list):
“””doc-string”””
statement(s)
Function Call:
Once we define a function, We can call it from another function, program or even from the Python prompt. To call a function, We can simply type the function name with appropriate paramenters.
return statement:
This statement is used to exit a function and go back to place from where it was called. The general form of return statement is:
return [expression]
No comments