Reputation: 57
Design and implement a class named Circle2D that contains:
Two data fields 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 radius. 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.
A method contains(circle2D) that returns True if the specified circle is inside this circle.
A method overlaps(circle2D) that returns True if the specified circle overlaps with this circle.
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.
My code so far:
import math
class Circle2D(object):
def __init__(self, x = 0, y = 0, r = 0):
"""Creates a circle with specified x, y, and radius."""
self._x = x
self._y = y
self._r = r
def __str__(self):
"""Returns the string representation."""
return ("Circle with center" + "(" + "%0.0f" % (self._x) + ", "
+ "%0.0f" % (self._y) + ")" + "and radius " + "%0.0f" % (self._r))
def getX(self):
"""Returns the X value."""
return self._x
def getY(self):
"""Returns the Y value."""
return self._y
def getRadius(self):
"""Returns the value of the radius"""
return self._r
def setRadius(self):
"""Sets a new value for the radius."""
pass
def getArea(self):
"""Returns the area of the circle."""
return math.pi * self._r * self._r
def getPerimeter(self):
"""Returns the perimeter of the circle."""
return 2 * math.pi * self._r
def containsPoint(x,y):
"""Returns True if the specified point (x, y) is inside this circle."""
if (self._x)^2 + (self._y)^2 <= (self._r)^2:
return True
else:
return False
def contains(circle2D):
pass
def overlaps(circle2D):
pass
def main():
x = float(input("Enter x coordinate for the center of circle 1: "))
y = float(input("Enter y coordinate for the center of circle 1: "))
r = float(input("Enter the radius of circle 1: "))
c1 = Circle2D(x, y, r)
print(c1)
x = float(input("\nEnter x coordinate for the center of circle 2: "))
y = float(input("Enter y coordinate for the center of circle 2: "))
r = float(input("Enter the radius of circle 2: "))
c2 = Circle2D(x, y, r)
print(c2)
#Test the getArea() and getPerimeter() methods
print("\nArea of a %s is %0.2f" % (c1, c1.getArea()))
print("Perimeter of a %s is %0.2f" % (c1, c1.getPerimeter()))
print("\nArea of a %s is %0.2f" % (c2, c2.getArea()))
print("Perimeter of a %s is %0.2f" % (c2, c2.getPerimeter()))
#-------------------
#Test containsPoint() method
print("\nResult of c1.containsPoint(c2.getX( ), c2.getY( )) is",
c1.containsPoint(c2.getX( ), c2.getY( )))
#Test contains() method
if c1.contains(c2):
print("\n%s contains %s" % (c1, c2))
else:
print("\n%s does not contain %s" % (c1, c2))
print("\nResult of c2.contains(c1) is",
c2.contains(c1))
#----------------
#Test overlap() method
if c1.overlaps(c2):
print("\n%s overlaps with %s" % (c1,c2))
else:
print("\n%s does not overlap with %s" % (c1,c2))
#--------------
#Test overloaded in operator
print("\nResult of c2 in c1 is", c2 in c1)
#Testing overloaded comparison and equality operators
#Something similar to this
print("\nTesting overloaded comparison operators...")
print("c1 == c2?", c1 == c2)
print("c1 != c2?", c1 != c2)
print("c1 < c2?", c1 < c2)
print("c1 <= c2?", c1 <= c2)
print("c1 > c2?", c1 > c2)
print("c1 >= c2?", c1 >= c2)
print('c1 == "Hello"?', c1 == "Hello")
print('c1 != "Hello"?', c1 != "Hello")
main()
Right now, I am focusing on the containsPoint, contains, and overlaps methods. I attempted the containsPoint, but I am getting an error that states: "containsPoint() takes 2 positional arguments but 3 were given"
Any help would be appreciated.
-Thanks
Upvotes: 1
Views: 4456
Reputation: 9609
containsPoint
is a method and therefore requires self
as its first argument:
def containsPoint(self, x, y):
You obviously know that if you need to know the distance between the point and center of the circle, you need to use 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 for the order (the subtraction result can be negative and still yields correct result).
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 would use the same calculation as for the point to calculate the distance between both circles' centers and then compare it with their radii.
Upvotes: 3