Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Now define a subclass Circle of Ellipse to represent a circle. We should be a

ID: 3604027 • Letter: 3

Question

3.
Now define a subclass Circle of Ellipse to represent a circle. We should be able to print a circle, get the area of the circle, move a circle and decide if one circle is inside another. Use inheritance wherever it is possible and natural to do so. Here is the first part of the contains method.

   def contains(self,other):
   ''' returns True if and only if circle other is completely
       contained inside circle self
   '''
   # complete the code; use pictures to guide you work;
   # it may help to consider the line from the
   # center of self through the center of other.

You should also define a function getCircle() that prompts the user for the appropriate information for a Circle, then returns that Circle object. This function is not a class method.

Explanation / Answer

# Import numpy for sqrt, square, pi methods
import numpy as np


# Point class containing point x, y as member variable.
# It has methods for translate, dist and string representation.
class Point:
    # Point constructor passing x and y point
    def __init__(self, x, y):
        self.x = x
        self.y = y

    # Translate method for translating x & y
    def translate(self, dx, dy):
        self.x = self.x + dx
        self.y = self.y + dy

    # Distance method computes distance from self to other point
    def dist(self, other):
        return np.sqrt(np.square(self.x - other.x) + np.square(self.y - other.y))

    # For string representation of Point
    def __str__(self):
        return "(" + str(self.x) + "," + str(self.y) + ")"


# Ellipse class containing center, x_extent and y_extent as member variable.
# It has methods for translate, get_area and string representation.
class Ellipse:
    # Ellipse constructor passing center, x_extent and y_extent
    def __init__(self, center, x_extent, y_extent):
        self.center = center
        self.x_extent = x_extent
        self.y_extent = y_extent

    # Translate method for translating x & y
    def translate(self, dx, dy):
        self.center.x = self.center.x + dx
        self.center.y = self.center.y + dy

    # Returns area of the ellipse
    def get_area(self, point):
        return np.pi * self.x_extent * 2 + np.pi * self.y_extent * 2

    # String representation
    def __str__(self):
        return "Ellipse with Center: (" + str(self.center.x) +
               "," + str(self.center.y) + "); "
                                          "Width: " + str(2 * self.y_extent) +
               "; Height: " + str(2 * self.x_extent)


# Circle class containing center and radius as member variable.
# It has methods for translate, get_area, contains and string representation.
class Circle(Ellipse):
    # Circle constructor passing center and radius
    def __init__(self, center, radius):
        # Initializing the super class
        Ellipse.__init__(self, center, radius, radius)
        self.radius = radius

    # Calcuate and returns the area of the circle
    def get_area(self, point):
        return np.pi * self.radius * self.radius

    # Checks if other circle is inside the self circle
    # it first calculates the distance between the center of self circle and other circle.
    # second, it finds the absolute distance between the radius of the circles
    # if distance is less than or equal to radius difference then other is inside self. Otherwise not
    def contains(self, other):
        p1 = self.center
        p2 = other.center
        distance = p1.dist(p2)
        diff_radius = np.square((self.radius - other.radius) ** 2)

        if distance <= diff_radius:
            return True
        else:
            return False

    # string representation
    def __str__(self):
        return "Center with Center: (" + str(self.center.x) +
               "," + str(self.center.y) + "); "
                                          "Radius: " + str(self.radius)


# It asks user to provide circle details and returns the circle object
def get_circle():
    x = input("Circle center x? ")
    y = input("Circle center y? ")
    radius = input("Circle radius? ")
    p = Point(x, y)
    return Circle(p, radius)


# Main method
if __name__ == '__main__':
    p = Point(3.4, 5.7)
    print("Here is the point %s" % p)

    e = Ellipse(p, 2.3, 1.2)
    print(e)

    c1 = get_circle()
    print(c1)

    c2 = get_circle()

    print("C1 contains C2? " + str(c1.contains(c2)))

    c3 = get_circle()

    print("C1 contains C3? " + str(c1.contains(c3)))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote