Question

In: Computer Science

PYTHON CODING: In this problem, we will figure out if two balls are colliding. We will...

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.

Solutions

Expert Solution

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:

Test Case table
(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:

Test Case table
(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

Related Solutions

Coding: Use MATLAB to figure out the following problem, if you do not know how to...
Coding: Use MATLAB to figure out the following problem, if you do not know how to use MATLAB then please do not answer. Coding is required for the exercise. For f(x) = arctan(x), find its zeros by implimenting Newtons method and the Secant method in Matlab. (Hint: Use Newtons method to calculate x1 for Secant method) Comment all code please since I would like to learn how to do this correctly in MATLAB. Thank you.
This is Python coding question, and topic is loop Can anyone help me figuring these out?...
This is Python coding question, and topic is loop Can anyone help me figuring these out? (Please do not use build in function) Thanks. 1. Write a program that prints your name 100 times to the screen. 2. Write a program that takes a string s and an integer n as parameters and prints the string s a total of in n times(once per line) 3. Write a for loop that prints all the integers from 3141 to 5926, skipping...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
Looking to see how to figure this out. So far when I break apart the coding...
Looking to see how to figure this out. So far when I break apart the coding it works but when it is all together it doesn't, Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly as difficult as it may appear. The way hangman works (for this assignment - we are doing a simplified game) is as follows: the computer will choose a word. (For this version, a word is selected from a...
For Python, I cannot figure out why "and" is printing at the beginning of my list....
For Python, I cannot figure out why "and" is printing at the beginning of my list. Can someone help? listToPrint = [] while True: newWord = input("Enter a word to add to the list (prest return to stop adding words) > ") newWord == "": break else: listToPrint.append(newWord) for item in listToPrint[:-1]: print(item, end=', ') print('and', listToPrint[-1]) If I enter m, n, b to the list, it executes to and m, n, b
Two balls connected by a rod, as shown in the figure below (Ignore rod’s mass).
Two balls connected by a rod, as shown in the figure below (Ignore rod’s mass). Mass of ball X is 8kg and the mass of ball Y is 5 kg  What is the moment of inertia of the system about AB?   
python pls create a function party_place: that search the dictionary and figure out where they party...
python pls create a function party_place: that search the dictionary and figure out where they party in that day. For example def party_place(dict2: dict, date: (int,int,int)): dict1= {'fire1': {(2000,5,20,480) : ('Aaron', 25, 300, ( 0, 300)), (2000,5,20,720) : ('Baily', 45, 1500, (1500,500)), (2000,5,21,490) : ('Aaron', 35, 500, (1300,500)) }, 'fire2': {(2000,5,20,810) : ('Baily', 45, 1400, (600,1600)), (2000,5,20,930) : ('Baily', 43, 1800, ( 0, 0)) }} output print(party_place(dict1, (2000,5,20,720)) = ['fire1', 'fire2'] print(party_place(dict1, (2000,5,21,720)) = ['fire1'] print(party_place(dict1, (2000,5,22,720)) = []
I can't figure this problem out! The problem is: Show the relationship between income and crime...
I can't figure this problem out! The problem is: Show the relationship between income and crime using indifference curves.
A box contains 2 ?red, 3 white and 3 green balls. Two balls are drawn out...
A box contains 2 ?red, 3 white and 3 green balls. Two balls are drawn out of the box in succession without replacement. What is the probability that both balls are the same? color?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT