Here is 8.19: import math class Rectangle2D: def __init__(self, x = 0, y = 0, wi
ID: 3577987 • Letter: H
Question
Here is 8.19:
import math
class Rectangle2D: def __init__(self, x = 0, y = 0, width = 0, height = 0): self.x = x self.y = y self.width = width self.height = height
def getX(self): return self.x
def getY(self): return self.y
def getWidth(self): return self.width def getHeight(self): return self.height
def setHeight(self, x): self.x = x
def setHeight(self, y): self.y = y
def setWidth(self, width): self.width = width
def setHeight(self, height): self.height = height
def getPerimeter(self): return 2 * (self.width + self.height) def getArea(self): return self.width * self.height def containsPoint(self, x, y): return abs(x - self.x) <= self.width / 2 and abs(y - self.y) <= self.height / 2 def contains(self, r): return self.containsPoint(r.x - r.width / 2, r.y + r.height / 2) and self.containsPoint(r.x - r.width / 2, r.y - r.height / 2) and self.containsPoint(r.x + r.width / 2, r.y + r.height / 2) and self.containsPoint(r.x + r.width / 2, r.y - r.height / 2) def overlaps(self, r): return abs(self.x - r.x) <= (self.width + r.width) / 2 and abs(self.y - r.y) <= (self.height + r.height) / 2
def __contains__(self, anotherRectangle): return self.contains(anotherRectangle)
def __lt__(self, secondRectangle): return self.__cmp__(secondRectangle) < 0
def __le__(self, secondRectangle): return self.__cmp__(secondRectangle) <= 0
def __gt__(self, secondRectangle): return self.__cmp__(secondRectangle) > 0
def __ge__(self, secondRectangle): return self.__cmp__(secondRectangle) >= 0 # Compare two numbers def __cmp__(self, secondRectangle): if self.getArea() > secondRectangle.getArea(): return 1 elif self.getArea() < secondRectangle.getArea(): return -1 else: return 0 import math
class Rectangle2D: def __init__(self, x = 0, y = 0, width = 0, height = 0): self.x = x self.y = y self.width = width self.height = height
def getX(self): return self.x
def getY(self): return self.y
def getWidth(self): return self.width def getHeight(self): return self.height
def setHeight(self, x): self.x = x
def setHeight(self, y): self.y = y
def setWidth(self, width): self.width = width
def setHeight(self, height): self.height = height
def getPerimeter(self): return 2 * (self.width + self.height) def getArea(self): return self.width * self.height def containsPoint(self, x, y): return abs(x - self.x) <= self.width / 2 and abs(y - self.y) <= self.height / 2 def contains(self, r): return self.containsPoint(r.x - r.width / 2, r.y + r.height / 2) and self.containsPoint(r.x - r.width / 2, r.y - r.height / 2) and self.containsPoint(r.x + r.width / 2, r.y + r.height / 2) and self.containsPoint(r.x + r.width / 2, r.y - r.height / 2) def overlaps(self, r): return abs(self.x - r.x) <= (self.width + r.width) / 2 and abs(self.y - r.y) <= (self.height + r.height) / 2
def __contains__(self, anotherRectangle): return self.contains(anotherRectangle)
def __lt__(self, secondRectangle): return self.__cmp__(secondRectangle) < 0
def __le__(self, secondRectangle): return self.__cmp__(secondRectangle) <= 0
def __gt__(self, secondRectangle): return self.__cmp__(secondRectangle) > 0
def __ge__(self, secondRectangle): return self.__cmp__(secondRectangle) >= 0 # Compare two numbers def __cmp__(self, secondRectangle): if self.getArea() > secondRectangle.getArea(): return 1 elif self.getArea() < secondRectangle.getArea(): return -1 else: return 0 12.7 aninter: mo rectangles intersect?) Using the Rectangle2D class you defined in Exercise 8.19, write a program that enables the user to point the mouse inside rectangle and drag it. As the rectangle is being dragged, the label displays whether two rectangles overlap, as shown in Figure 12.19. Two Rectangles two rectangles don't intersect Two rectangles intersect (a) FoURE 12.19 Check whether two rectangles are overlapping.
Explanation / Answer
Hope it will help your:
import math
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
class Rect(object):
def __init__(self, point1, point2):
'''Store the top, bottom, left and right values for points
point1 and point2 are the (corners) in either order
'''
self.left = min(point1.x, point2.x)
self.right = max(point1.x, point2.x)
self.bottom = min(point1.y, point2.y)
self.top = max(point1.y, point2.y)
def overlap(self,r1,r2):
hoverlaps = True
voverlaps = True
if (r1.left > r2.right) or (r1.right < r2.left):
hoverlaps = False
if (r1.top < r2.bottom) or (r1.bottom > r2.top):
voverlaps = False
return hoverlaps and voverlaps
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.