logo CodeStepByStep logo

Rectangle_union

Language/Type: Python classes
Related Links:

Add a method named union to the Rectangle class. Use your Rectangle class solution from the previous exercises as a starting point to solve this exercise.

Suppose a class named Rectangle has already been created that has the following members:

member name type description
Rectangle(x, y, w, h) constructor Initializes a new rectangle whose top-left corner is specified by the given (x, y) coordinates and with the given width and height, w by h. Raises a ValueError on a negative width or height.
ba._x,
ba._y,
ba._width,
ba._height
data attributes private attributes storing rectangle coordinates
ba.x,
ba.y,
ba.width,
ba.height
property the rectangle location and dimensions (read-only)
str(r) method returns a string representation of the rectangle, such as "Rectangle[x=1,y=2,width=3,height=4]".

Add a method named union to your class. Your method should accept another rectangle as a parameter, and return a new rectangle that represents the area occupied by the tightest bounding box that contains both the current rectangle (self) and the given other rectangle.

Member function: Write a member function that will become part of an existing class. You do not need to write the complete class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.