Python Homework Help: Problem 1 - Collision Detection of Balls
Python Homework Help: Problem 1 - Collision Detection of Balls
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 and its size. We can store position with
its center x-y coordinates, and we can use its radius for size. So a ball is 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 the sum of their radii. If so, they are colliding.
Write a function that takes two balls and computes if they are colliding. Then call the function
with two sets of balls. The first set is (0, 0, 1) and (3, 3, 1); these should not be colliding. The
second set is (5, 5, 2) and (2, 8, 3); these should be colliding.
It will be useful to define a list or tuple at the top called VOWELS. This way, you can check if
a letter x is a vowel with the expression x in VOWELS.
It’s tricky for us to deal with punctuation and numbers with what we know so far, so
instead, ask the user to enter only words and spaces. You can convert their input from a
string to a list of strings by calling split on the string:
Using this list, you can go through each word and convert it to Pig-Latin. Also, to get a
word except for the first letter, you can use word[1:].
Hints: It will make your life much easier — and your code much better — if you separate
tasks into functions, e.g. have a function that converts one word to Pig-Latin rather than
putting it into your main program code.
Solution
# collision.py
# Example solution for Lab 6, problem 1
#
# Aseem Kishore
#
# 6.189 - Intro to Python
# IAP 2008 - Class 4
# Imports should usually go at the top of a program instead of in the main
code.
from math import *
# These helper functions let me "abstract away" the syntax of getting a
ball's
# x- and y- coordinates, or its radius. This makes my code more readable
and
# also helps prevent bugs where I use x instead of y, etc.
def get_x(ball):
return ball[0]
def get_y(ball):
return ball[1]
def get_r(ball):
return ball[2]
# I got this function from the second day of class.
We've been trying to tell
# you guys the importance of functions; here's one --
reuse. There are many
# applications for finding the distance between two
points; detecting collision
# is one, so we can reuse the function. This is also why
we don't ask for input
# or print our result inside the function.
def distance(x1, y1, x2, y2):
return sqrt((x2-x1)**2 + (y2-y1)**2)
# Here is my detect collision function. Note that I'm NOT taking six variables
# like x1, y1, r1, x2, y2, r2 -- that's the purpose of combining x, y, r into a
# tuple, as every ball has an x, y and r.
# Now, let's write the main program code, to ask the user
and convert.
print "Type in a sentence, and it'll get
converted to Pig-Latin!"
print "Please don't use punctuation or
numbers."
print "Also, we can't handle
uppercase/lowercase yet, so lowers only
please!"
print
text = raw_input() # nothing in the
parentheses, because there's nothing else
# extra to tell the user before he is
allowed to type
print
print convert_sentence(text)