Create: A method called contains(x, y) – returns True if the point at x,y is con
ID: 3697226 • Letter: C
Question
Create: A method called contains(x, y) – returns True if the point at x,y is contained within the Rectangle, false otherwise
Create: A method called intersects(otherRectangle) - returns True if this Rectangle intersects the otherRectangle, False otherwise
This is what I have thus far. please help!
class Rectangle(object):
__rectangleCount = 0
@staticmethod
def numRectangles():
return Rectangle.__rectangleCount
def __init__(self, x, y, width, height):
if width <= 0:
raise Exception("width must be a positive number")
if height <= 0:
raise Exception("height must be a positive number")
self.__x = x
self.__y = y
self.__width = width
self.__height = height
Rectangle.__rectangleCount += 1
#self.__xbot = (x + width)
#self.__ybot = (y + height)
def getHeight(self):
return self.__height
def getWidth(self):
return self.__width
def getX(self):
return self.__x
def getY(self):
return self.__y
def __str__(self):
return "Rectangle:x=" + str(self.__x) + ",y=" + str(self.__y) +
",width=" + str(self.__width) + ",height=" + str(self.__height)
def contains(self, x, y):
if self.__x < self.__width and self.__y < self.__height:
return True
else:
return False
def intersects(self, otherRectangle):
return True
Explanation / Answer
class Rectangle(object):
__rectangleCount = 0
@staticmethod
def numRectangles():
return Rectangle.__rectangleCount
def __init__(self, x, y, width, height):
if width <= 0:
raise Exception("width must be a positive number")
if height <= 0:
raise Exception("height must be a positive number")
self.__x = x
self.__y = y
self.__width = width
self.__height = height
Rectangle.__rectangleCount += 1
#self.__xbot = (x + width)
#self.__ybot = (y + height)
def getHeight(self):
return self.__height
def getWidth(self):
return self.__width
def getX(self):
return self.__x
def getY(self):
return self.__y
def __str__(self):
return "Rectangle:x=" + str(self.__x) + ",y=" + str(self.__y) +
",width=" + str(self.__width) + ",height=" + str(self.__height)
def contains(self, x, y):
if x-self.__x<=self.__width and x-self.__x>=0 and y-self.__y<=self.__height and y-self.__y>=0:
return True
else:
return False
def intersects(self, otherRectangle):
return (otherRectangle.contains(self.__x,self.__y))or(otherRectangle.contains(self.__x+self.__width,self.__y))or(otherRectangle.contains(self.__x,self.__y+self.__height))or(otherRectangle.contains(self.__x+self.__width,self.__y+self.__height))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.