Hello, I could not run it, so i need help. Thank you. Design and implement a cla
ID: 3665471 • Letter: H
Question
Hello, I could not run it, so i need help. Thank you.
Design and implement a class named Circle2D that contains:
Two data fields (i.e., instance variables) named x and y that specify the center of the circle.
A data field radius with get/set methods. The method getRadius() returns the value of the radius while setRadius() sets a new value for the radius.
A constructor (i.e., __init__ method) that creates a circle with specified x, y, and radius. Use the default value 0 for all parameters.
An __str__ method that return a string representation of the form "Circle with center (x, y) and radius radius", where x, y, and radius will be replaced by the circle's actual values of the center and raduis. For example, on a circle object with center at (2, 3) and radius 10, this method will return the string "Circle with center (2, 3) and radius 10".
A getX() and getY() methods.
A method getArea() that returns the area of the circle.
A method getPerimeter() that returns the perimeter of the circle.
A method containsPoint(x, y) that returns True if the specified point (x, y) is inside this circle. (Hint: see Figure 1. A point P is inside the circle c iff distance between c.center and P is less than c.radius.)
A method contains(circle2D) that returns True if the specified circle is inside this circle. (Hint: see Figure 2. Circle c1 contains circle c2 iff the distance between c1.radius and c2.radius + c2.radius <= c1.radius.)
A method overlaps(circle2D) that returns True if the specified circle overlaps with this circle. (Hint: see Figure 3. Circle c1 overlaps with circle c2 iff the distance between c1.radius and c2.radius < c1.radius + c2.radius.)
Implement the _ _contains_ _(self, anotherCircle) method that returns True if anotherCircle is contained in this circle. The _ _contains_ _(self, item) special method is used to implement the membership test operator in. It should return true if item is in self, and false otherwise.
Implement the _ _lt_ _, _ _le_ _, _ _gt_ _, _ _ge_ _, _ _eq_ _, and _ _ne_ _ methods that compare two circles based on their radii.
Use the following main function to test your class.
Explanation / Answer
Here is the errr with your code::
containsPoint is a method therefore it requires self as its first argument:
def containsPoint(self, x, y):
you know that if we need to know the distance between the point and center of the circle yu must use the Pythagorean theorem.
In this case the sides would be differences in coordinates of the point and of the circle's center (ie. distance in x and y axes) and the distance you want would make the hypotenuse:
dist^2 = (x - self._x)^2 + (y - self._y)^2 # Note: not valid Python code
Note that since the subtraction result is squared, you don't need to care of the order
If this distance is smaller than or equal to the radius, the point lies within the circle:
(x - self._x)^2 + (y - self._y)^2 <= self._r^2
To calculate if a circle overlaps or is contained in other circle you must use the same calculation as for the point to calculate the distance between both circles' centers and then compare it with their radi.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.