Defining a Class
Define the class Rectangle. It’s constructor takes a pair of numbers representing the top-left corner, and two other numbers representing the width and height. It has the following methods:
Note: this problem uses the coordinate system commonly used for computer graphics. In this system, the origin is in the top-left corner and the y-axis is flipped. The bottom-right corner will therefore have a larger y coordinate than the top-left corner.
Examples:
Each example below follows on from the previous one. Be careful to
get the spacing right for str - i.e. comma followed by space.
>>> r = Rectangle((2,3), 5, 7) >>> str(r) '((2, 3), (7, 10))' >>> r.move((5,6)) >>> str(r) '((5, 6), (10, 13))' >>> r.resize(2,3) >>> str(r) '((5,6), (7, 9))' >>> r.get_bottom_right() (7, 9)
class Rectangle:
def __init__(self, top_left, width, height):
self.top_left = top_left
self.width = width
self.height = height
def get_bottom_right(self):
bottom_right=[0, 0]
bottom_right[0] = self.top_left[0] + self.width
bottom_right[1] = self.top_left[1] + self.height
return (bottom_right[0], bottom_right[1])
def move(self, p):
self.top_left = p
def resize(self, width, height):
self.width = width
self.height = height
def __str__(self):
bottom_right = self.get_bottom_right()
return str((self.top_left, self.get_bottom_right()))
The class definition is quite self-explanatory. Please go through it, and let me know if you need any further clarifications.
Also, please rate the answer.
Get Answers For Free
Most questions answered within 1 hours.