def sub_circle(self): if self.corner1.x < self.corner2.x: center_x = (self.corner2.x - self.corner1.x) / 2 + self.corner1.x else: center_x = (self.corner1.x - self.corner2.x) / 2 + self.corner2.x if self.corner1.y < self.corner2.y: center_y = (self.corner2.y - self.corner1.y) / 2 + self.corner1.y else: center_y = (self.corner1.y - self.corner2.y) / 2 + self.corner2.y if abs(self.corner1.x - self.corner2.x) >= abs(self.corner1.y - self.corner2.y): return cir.Circle(pt.Point(center_x, center_y), abs(self.corner1.y - self.corner2.y) / 2) else: return cir.Circle(pt.Point(center_x, center_y), abs(self.corner1.x - self.corner2.x) / 2)
def main(): """ Tests the Circle class defined in the imported cir module. """ # TODO: Add code below (at the appropriate places) # to test your Circle class as you implement it. # Be sure that your tests print the EXPECTED results of the # tests as well as the ACTUAL results from the function calls. print('------------------------------------') print('Testing __init__:') circle1 = cir.Circle(pt.Point(50, 60), 25) circle2 = cir.Circle(pt.Point(10, 30), 50) print(circle1.center, circle1.radius) print(circle2.center, circle2.radius) print('\n------------------------------------') print('Testing __repr__:') print(circle1.__repr__()) print(circle2.__repr__()) print('\n------------------------------------') print('Testing clone:') print(circle1.clone()) print('\n------------------------------------') print('Testing move_by:') circle1.move_by(5, 10) print(circle1) circle2.move_by(20, 14) print(circle2) print('\n------------------------------------') print('Testing intersects:') circle_test = cir.Circle(pt.Point(70, 60), 40) print(circle1.intersects(circle_test)) print(circle2.intersects(circle_test)) print('\n------------------------------------') print('Testing closer_to_origin:') circle_test1 = cir.Circle(pt.Point(60, 40), 40) print(circle1.closer_to_origin(circle_test1)) print(circle2.closer_to_origin(circle_test1)) print('\n------------------------------------') print('Testing super_circle:') ft_circle = cir.Circle(pt.Point(40, 80), 20) print(circle1.super_circle(ft_circle))
def main(): """ Tests the Rectangle class defined in the imported rect module. """ # TODO: Add code below (at the appropriate places) # to test your Rectangle class as you implement it. # Be sure that your tests print the EXPECTED results of the # tests as well as the ACTUAL results from the function calls. print('------------------------------------') print('Testing __init__:') rect1 = rect.Rectangle(pt.Point(20, 50), pt.Point(80, 0)) print(rect1.corner1) print(rect1.corner2) print('\n------------------------------------') print('Testing __repr__:') print(rect1.__repr__()) print('\n------------------------------------') print('Testing clone:') print(rect1.clone()) print('\n------------------------------------') print('Testing move_by:') print(rect1.move_by(10, 15)) print('\n------------------------------------') print('Testing corners:') print(rect1.corners()) print('\n------------------------------------') print('Testing contains:') print(rect1.contains(pt.Point(42, 23))) print('\n------------------------------------') print('Testing intersects:') another_rect = rect.Rectangle(pt.Point(40, 60), pt.Point(80, 0)) print(rect1.intersects(another_rect)) print('\n------------------------------------') print('Testing sub_circle:') print(rect1.sub_circle()) print('\n------------------------------------') print('Testing super_rectangle:') rect3 = rect.Rectangle(pt.Point(70, 50), pt.Point(20, 15)) rect4 = rect.Rectangle(pt.Point(12, 15), pt.Point(70, 40)) print(rect1.super_rectangle([rect1, another_rect, rect3, rect4]))
def super_circle(self, fat_circle): a = (self.center.x + fat_circle.center.x) * 0.5 b = (self.center.y + fat_circle.center.y) * 0.5 radius = self.radius + fat_circle.radius fat_circle = Circle(pt.Point(a, b), radius) return "Circle(center=Point{}),radius={}".format(fat_circle.center, fat_circle.radius)
def corners(self): self.corner3 = pt.Point(self.corner2.x, self.corner1.y) self.corner4 = pt.Point(self.corner1.x, self.corner2.y) return 'corners={},{},{},{}'.format(self.corner1, self.corner2, self.corner3, self.corner4)
def move_by(self, dx, dy): self.corner1 = pt.Point(self.corner1.x + dx, self.corner1.y + dy) self.corner2 = pt.Point(self.corner2.x + dx, self.corner2.y + dy) return self
def __init__(self, corner1=pt.Point(0, 0), corner2=pt.Point(0, 0)): self.corner1 = corner1 self.corner2 = corner2