In: Computer Science
PYTHON CODING:
In this problem, we will figure out if two balls are colliding. We will think in 2D to simplify things, though 3D isn’t different conceptually. For calculating collision, we only care about a ball’s position in space, as well as its size. We can store a ball’s position with the (x, y) coordinates of its center point, and we can calculate its size if we know its radius. Thus, we represent a ball in 2D space as a tuple of (x, y, r). To figure out if two balls are colliding, we need to compute the distance between their centers, then see if this distance is less than or equal to the sum of their radii.
PART A: Write a function ball_collide that takes two balls as parameters and computes if they are colliding; your function should return a Boolean saying whether or not the balls are colliding.
PART B: write your function to work with balls in 3D space. How should you represent the balls? You will also need to write your own test cases - be sure to figure out any edge cases you need to test.
PART - A:
Below is the program in which the function ball_collide returns boolean value. 'TRUE' if collision takes place, else 'FALSE'. The function parameters are tuples of (x,y,r) for each ball in 2 dimensional space.
------------------------------------------------------------------------------------------------------
import math
def ball_collide(c1,c2):
d = math.sqrt( ( int(c1[0]) - int(c2[0]) )*2 + ( int(c1[1]) -
int(c2[1]) )*2 )
sum_radius = int(c1[2]) + int(c2[2])
if sum_radius >= d:
return True
else:
return False
x1,y1,r1 = input("Enter co_ordinates and radius of first
circle\n").split(" ")
x2,y2,r2 = input("Enter co_ordinates and radius of second
circle\n").split(" ")
ball1 = (x1,y1,r1)
ball2 = (x2,y2,r2)
#print(ball1[0])
print(ball_collide(ball1,ball2))
------------------------------------------------------------------------------------------------------------
Snapshot of the above code:
(x1,y1,r1) | (x2,y2,r2) | Output | |
---|---|---|---|
1 1 5 | 2 3 9 | True | Collide |
2 5 6 | 10 10 3 | False | Not Collide |
15 12 4 | 19 21 9 | True | Collide |
10 10 4 | 10 20 6 | True | Collide |
PART - B:
This program implements the function ball_collide to work with balls in 3 Dimensional space. Here, a ball can be represented by using x,y and z coordinates along with r i.e.(x,y,z,r).
-------------------------------------------------------------------------------------------------------------------------------------------
import math
def is_colliding(c1,c2):
d = math.sqrt( ( int(c1[0]) - int(c2[0]) )*2 + ( int(c1[1]) -
int(c2[1]) )2 + ( int(c1[2]) - int(c2[2]) )*2 )
sum_radius = c1[3] + c2[3]
if sum_radius >= d:
return True
else:
return False
x1,y1,z1,r1 = input("Enter co_ordinates and radius of first
circle\n").split(" ")
x2,y2,z2,r2 = input("Enter co_ordinates and radius of second
circle\n").split(" ")
ball1 = (int(x1),int(y1),int(z1),int(r1))
ball2 = (int(x2),int(y2),int(z2),int(r2))
print(is_colliding(ball1,ball2))
--------------------------------------------------------------------------------------------------------------------------------------------
Snapshot of the above code:
(x1,y1,z1,r1) | (x2,y2,z2,r2) | Output | |
---|---|---|---|
1 1 1 5 | 2 3 5 9 | True | Collide |
2 5 1 6 | 10 11 9 3 | False | Not Collide |
34 21 19 11 | 11 25 10 3 | False | Not Collide |
10 10 5 4 | 10 20 5 6 | True | Collide |