Mar-10-2019, 07:35 AM
I've got __iadd__ working for the Point class, but don't know how to implement it to where it works for the Circle class. It should work like this:
>>> circle1 = Circle(radius=2.5, center=Point(1, 1)) >>> circle2 = Circle(center=Point(2, 3), radius=1) >>> circle1 += circle2 >>> circle2 Circle(center=Point(2, 3), radius=1) >>> circle1 Circle(center=Point(3, 4), radius=3.5)Also, I should be able to have the the instance of a Point object default to (0, 0) like this:
>>> circle = Circle() >>> circle Circle(center=Point(0, 0), radius=1) >>> print(circle) Circle with center at (0, 0) and radius 1This works before I added some code to raise a TypeError, but I need it to work with this too:
@center.setter def center(self, center): if not isinstance(center, Point): raise TypeError("The center must be a Point!") self._center = centerHere's the full code:
import math class Point: def __init__(self, x=0, y=0): self.x = x self.y = y self.center=[x,y] def __getitem__(self, index): return self.center[index] def __iter__(self): yield self.x yield self.y def __add__(self, other): return Point(self.x + other.x, self.y + other.y) def __iadd__(self, other): self.x += other.x self.y += other.y return self def __mul__(self, other): return Point(self.x * other, self.y * other) def __rmul__(self, other): return Point(other * self.x, other * self.y) def __imul__(self, other): self.x *= other self.y *= other return self @classmethod def from_tuple(self, coords): if not isinstance(coords, tuple): raise TypeError() return self(coords[0], coords[1]) def loc_from_tuple(self, coords): if not isinstance(coords, tuple): raise TypeError() self.x = coords[0] self.y = coords[1] def __str__(self): return "Point at ({}, {})".format(self.x, self.y) def __repr__(self): return "Point(x={}, y={})".format(self.x, self.y) @property def magnitude(self): return math.sqrt(self.x ** 2 + self.y ** 2) def distance(self, other): return ((self.x - other.x)**2 + (self.y - other.y)**2)**0.5
class Circle: def __init__(self, center=(0,0), radius=1): Point.__init__(self, center) self.center = center self.radius = radius def __getitem__(self, index): return self.center[index] def __str__(self): return "Circle with center at ({0}, {1}) and radius {2}".format(self.center[0], self.center[1], self.radius) def __repr__(self): return "Circle(center=Point({0}, {1}), radius={2})".format(self.center[0], self.center[1], self.radius) def __add__(self, other): return Circle( Point(self.center[0] + other.center[0], self.center[1] + other.center[1]), self.radius + other.radius) @classmethod def from_tuple(self, center, radius=1): if not isinstance(center, tuple): raise TypeError() new_point = Point(center[0],center[1]) return self(new_point, radius) @classmethod def center_from_tuple(self, center, radius=1): if not isinstance(center, tuple): raise TypeError() new_point = Point(center[0],center[1]) self.center = new_point @property def center(self): return self._center @center.setter def center(self, center): if not isinstance(center, Point): raise TypeError("The center must be a Point!") self._center = center @property def area(self): return math.pi * self.radius ** 2 @property def diameter(self): return self.radius * 2 @diameter.setter def diameter(self, diameter): self.radius = diameter / 2 @property def radius(self): return self._radius @radius.setter def radius(self, radius): if radius < 0: raise ValueError("The radius cannot be negative!") self._radius = radius