I have to import two modules into a file and create a code that shows when two circles collide with each other, but I cannot get the example output that was given.
this is my code for the two modules and the file I imported them to:
import math
class Shape:
"""Shape class: provides three methods:
1. __init__() set initial x,y coordinates
2. move() changes x,y coordinates
3. location() returns x,y coordinates
The __str__() method is customized to show an object's class and x,y coordinates.
You have to complete the location() method."""
# __init__() is the constructor
# Instance methods always have a "self" parameter.
def __init__(self, x, y):
"""Assign x,y coordinates to the object"""
self.x = x
self.y = y
def move(self, deltaX, deltaY):
"""Moves Shape object by adding values to the object's x and y coordinates."""
self.x = self.x + deltaX
self.y = self.y + deltaY
# Custom __str__() method to report an objects properties
# Every class inherits a __str__() method for the top-level Object class.
# We can modify it to produce more descriptive output for this class.
def __str__(self):
"""Return class name, x, and y"""
return "{}, x:{}, y:{}".format(type(self).__name__, self.x, self.y)
def location(self):
tup = (self.x, self.y)
return tup
import math
from shape import *
class Circle(Shape):
"""The Quick Python Book"""
pi = 3.14159265359
def __init__(self, x=0, y=0, radius=1):
Shape.__init__(self, x, y)
self.radius = radius
def area(self):
return Circle.pi * self.radius ** 2
def __str__(self):
return Shape.__str__(self) + ", radius: {radius}, area: {area}".format(radius=self.radius, area = self.area())
# Class methods are declared using the @classmethod keyword
# "cls" is a standin for MyClass.
@classmethod
def is_collision(self,c1,c2):
a = self.distance(c1,c2)
if a <= (c1.radius + c2.radius):
return True
return None
@classmethod
def distance(self,c1,c2):
a = c1.location()
b = c2.location()
return math.sqrt(( (a[0] - b[0]) * (a[0] - b[0]) +(a[1] - b[1]) * (a[1] - b[1]) ))
import math
from shape import Shape
from circle import Circle
print('''
#########################
week8.16.plaintext.py
#########################
''')
print("""Testing Circle class for area(), move(), location()""")
# Initial x,y coordinates
i2 = 0
x1 = 0
y1 = 0
x2 = 0
y2 = 0
c1 = Circle(100, 0, 20)
c2 = Circle(200, 0, 20)
c1_o_loc = c1.__str__()
c2_o_loc = c2.__str__()
print("""Starting with two circles:
Circle One: {}
Circle Two: {}
Minimum distance between their centers: {}""".format(c1_o_loc,c2_o_loc, 40))
print("""
|============================================================|
| Num | x1 | y1 | x2 | y2 | Distance | Collision? |
|============================================================|""")
for i in range(1,31):
template = """| {count:>3} | {x1_val:>4} | {y1_val:>4} | {x2_val:>4} | {y2_val:>4} | {distance:>10.2f} | {collision} |"""
# Get the x,y coordinates of both circles
c1_x = c1.location()[0]
c1_y = c1.location()[1]
c2_x = c2.location()[0]
c2_y = c2.location()[1]
# print each line of data using a template
print(template.format(count=i,x1_val=c1_x,y1_val=c1_y,x2_val=c2_x,y2_val=c2_y,
distance=Circle.distance(c1,c2), collision=Circle.is_collision(c1,c2)))
print("|------------------------------------------------------------|")
# Change directions now and then for circle 1
if i < 15:
x1 = 20 * i + 100
else:
x1 = 300 + ((18 - i) * 20)
# Circle 2 follows a sine wave.
y1 = 35 * i
x1 = int(i + x1**1.2)
y2 = int(x1**1.19)
x2 = int( 35 * math.sin(i**0.99) + 200 )
y2 = 35 * i
c1.move(x1, y1)
c2.move(x2, y2)
What I have tried:
I have tried to change the x's and y's in the file
y1 = 35 * i
x1 = int(i + x1**1.2)
y2 = int(x1**1.19)
x2 = int( 35 * math.sin(i**0.99) + 200 )
y2 = 35 * i
but without any luck. The output I am supposed to get is:
#########################
lab8.16.py
#########################
Testing Circle class for area(), move(), location(), __str__()
Starting with two circles:
Circle One: This is a Circle at x,y locations 100, 0 and radius 20.
Circle Two: This is a Circle at x,y locations 200, 0 and radius 20.
Minimum distance between their centers: 40
|============================================================|
| Num | x1 | y1 | x2 | y2 | Distance | Collision? |
|============================================================|
| 1 | 100 | 0 | 200 | 0 | 100.00 | None |
|------------------------------------------------------------|
| 2 | 120 | 35 | 229 | 35 | 109.00 | None |
|------------------------------------------------------------|
| 3 | 140 | 70 | 232 | 70 | 92.00 | None |
|------------------------------------------------------------|
| 4 | 160 | 105 | 206 | 105 | 46.00 | None |
|------------------------------------------------------------|
| 5 | 180 | 140 | 174 | 140 | 6.00 | True |
|------------------------------------------------------------|
| 6 | 200 | 175 | 165 | 175 | 35.00 | True |
|------------------------------------------------------------|
| 7 | 220 | 210 | 186 | 210 | 34.00 | True |
|------------------------------------------------------------|
| 8 | 240 | 245 | 219 | 245 | 21.00 | True |
|------------------------------------------------------------|
| 9 | 260 | 280 | 234 | 280 | 26.00 | True |
|------------------------------------------------------------|
| 10 | 280 | 315 | 220 | 315 | 60.00 | None |
|------------------------------------------------------------|
| 11 | 300 | 350 | 188 | 350 | 112.00 | None |
|------------------------------------------------------------|
| 12 | 320 | 385 | 166 | 385 | 154.00 | None |
|------------------------------------------------------------|
the output I get is:
#########################
week8.16.plaintext.py
#########################
Testing Circle class for area(), move(), location()
Starting with two circles:
Circle One: Circle, x:100, y:0, radius: 20, area: 1256.637061436
Circle Two: Circle, x:200, y:0, radius: 20, area: 1256.637061436
Minimum distance between their centers: 40
|============================================================|
| Num | x1 | y1 | x2 | y2 | Distance | Collision? |
|============================================================|
| 1 | 100 | 0 | 200 | 0 | 100.00 | None |
|------------------------------------------------------------|
| 2 | 413 | 35 | 429 | 35 | 16.00 | True |
|------------------------------------------------------------|
| 3 | 791 | 105 | 661 | 105 | 130.00 | None |
|------------------------------------------------------------|
| 4 | 1235 | 210 | 867 | 210 | 368.00 | None |
|------------------------------------------------------------|
| 5 | 1747 | 350 | 1041 | 350 | 706.00 | None |
|------------------------------------------------------------|
| 6 | 2329 | 525 | 1206 | 525 | 1123.00 | None |
|------------------------------------------------------------|
| 7 | 2982 | 735 | 1392 | 735 | 1590.00 | None |
|------------------------------------------------------------|
| 8 | 3707 | 980 | 1611 | 980 | 2096.00 | None |
|------------------------------------------------------------|
| 9 | 4505 | 1260 | 1845 | 1260 | 2660.00 | None |
|------------------------------------------------------------|
| 10 | 5378 | 1575 | 2065 | 1575 | 3313.00 | None |
|------------------------------------------------------------|
| 11 | 6326 | 1925 | 2253 | 1925 | 4073.00 | None |
|------------------------------------------------------------|
| 12 | 7351 | 2310 | 2419 | 2310 | 4932.00 | None |
|------------------------------------------------------------|