Question: In Python: The circle.py module contains the Circle class described in Chapter 3.5. The Circle class is a subclass of Shape, which means that it

In Python:

The circle.py module contains the Circle class described in Chapter 3.5. The Circle class is a subclass of Shape, which means that it inherits all of the Shape classs properties, including the location(), move(), and __str__() methods.

Add two class methods to the Circle class.

distance(c1,c2), a Circle class method that calculates the distance between the two circles. Finding the distance between the centers of the circles can be done by using the coordinates returned by each circles location() method and doing a few calculations using the Pythagorean Theorem (http://mathworld.wolfram.com/PythagoreanTheorem.html).

is_collision(c1,c2), a Circle class method that takes two circle objects as arguments and returns the boolean value True if two circles intersect (collide), and None if they do not. This function compares the distance between the centers of two circles to the sum of their radii. If the distance is greater than the sum of the radii, the circles are not colliding.

Heres some code to get you started on circle.py

#!/usr/local/bin/python3 # Creator: Your Name # File : circle.py # Date : 2016-07-01 Fri import math from shape import Shape class Circle(Shape): pi = 3.14159265359 def __init__(self, x=0, y=0, radius=1): Shape.__init__(self, x, y) self.radius = radius def area(self): return math.pi * self.radius ** 2 def __str__(self): return Shape.__str__(self) + ", radius: {radius}, area: {area:.2f}".format(radius=self.radius, area = self.area()) @classmethod def is_collision(Circle,c1, c2): """Test whether two circle objects are occupying the same space. YOUR CODE GOES HERE""" ########################################## # TODO TODO TODO --- COMPLETE THIS METHOD ########################################## pass @classmethod def distance(Circle,c1,c2): """ YOUR CODE GOES HERE""" ########################################## # TODO TODO TODO --- COMPLETE THIS METHOD ########################################## pass def _main(): # run functions here. """Testing Circle class for area(), move(), location(), __str__()""" print("--- START ---") c1x = -10 c1y = 0 c2x = 10 c2y = 0 i1 = 0 i2 = 0 c1 = Circle(0, 0, 5) c2 = Circle(0, 0, 5) for i in range(10): print('--') print('Circle 1: ',c1) print('Circle 2: ',c2) print("Collision: ",Circle.is_collision(c1, c2)) i1 += i i2 -= i c1.move(i, i1) c2.move(i, i2) print("c1 Final location: ", c1.location()) print("c2 Final location: ", c2.location()) print(c1) print(c2) print("--- END ---") # Testing code: run this module as a standalone script if __name__ == '_main': _main() 

Circle.__str__() in action

The following example output shows the output of a program testing two circles to see if they are colliding. The customized Circle.__str__() method creates a meaningful description of the objects being processed.

--- START --- -- Circle 1: Circle, x:0, y:0, radius: 5, area: 78.54 Circle 2: Circle, x:0, y:0, radius: 5, area: 78.54 Collision: True -- Circle 1: Circle, x:0, y:0, radius: 5, area: 78.54 Circle 2: Circle, x:0, y:0, radius: 5, area: 78.54 Collision: True -- Circle 1: Circle, x:1, y:1, radius: 5, area: 78.54 Circle 2: Circle, x:1, y:-1, radius: 5, area: 78.54 Collision: True -- Circle 1: Circle, x:3, y:4, radius: 5, area: 78.54 Circle 2: Circle, x:3, y:-4, radius: 5, area: 78.54 Collision: True -- Circle 1: Circle, x:6, y:10, radius: 5, area: 78.54 Circle 2: Circle, x:6, y:-10, radius: 5, area: 78.54 Collision: None -- Circle 1: Circle, x:10, y:20, radius: 5, area: 78.54 Circle 2: Circle, x:10, y:-20, radius: 5, area: 78.54 Collision: None -- Circle 1: Circle, x:15, y:35, radius: 5, area: 78.54 Circle 2: Circle, x:15, y:-35, radius: 5, area: 78.54 Collision: None -- Circle 1: Circle, x:21, y:56, radius: 5, area: 78.54 Circle 2: Circle, x:21, y:-56, radius: 5, area: 78.54 Collision: None -- Circle 1: Circle, x:28, y:84, radius: 5, area: 78.54 Circle 2: Circle, x:28, y:-84, radius: 5, area: 78.54 Collision: None -- Circle 1: Circle, x:36, y:120, radius: 5, area: 78.54 Circle 2: Circle, x:36, y:-120, radius: 5, area: 78.54 Collision: None c1 Final location: (45, 165) c2 Final location: (45, -165) Circle, x:45, y:165, radius: 5, area: 78.54 Circle, x:45, y:-165, radius: 5, area: 78.54 --- END ---

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!