C++ Rectangle2D Class: Area, Perimeter, and Containment Checks

This code implements a Rectangle2D class in C++ to represent a rectangle with its center coordinates (x, y), width, and height. The class provides methods for calculating the rectangle's area and perimeter, as well as checking if a given point or another rectangle is inside or overlaps the current rectangle.

UML Diagram:

+--------------------------------+ 
|       Rectangle2D Class        | 
+--------------------------------+ 
| - x: double                     | 
| - y: double                     | 
| - width: double                 | 
| - height: double                | 
+--------------------------------+ 
| + getX(): double               | 
| + setX(x: double): void         | 
| + getY(): double               | 
| + setY(y: double): void         | 
| + getWidth(): double           | 
| + setWidth(width: double): void | 
| + getHeight(): double          | 
| + setHeight(height: double): void| 
| + Rectangle2D()                | 
| + Rectangle2D(x: double, y: double, width: double, height: double)| 
| + getArea(): double            | 
| + getPerimeter(): double       | 
| + contains(x: double, y: double): bool | 
| + contains(r: Rectangle2D): bool| 
| + overlaps(r: Rectangle2D): bool| 
+--------------------------------+ 

Implementation:

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

class Rectangle2D {
private:
    double x, y, width, height;

public:
    double getX() const {
        return x;
    }
    void setX(double x) {
        this->x = x;
    }
    double getY() const {
        return y;
    }
    void setY(double y) {
        this->y = y;
    }
    double getWidth() const {
        return width;
    }
    void setWidth(double width) {
        this->width = width;
    }
    double getHeight() const {
        return height;
    }
    void setHeight(double height) {
        this->height = height;
    }

    Rectangle2D() {
        x = 0;
        y = 0;
        width = 1;
        height = 1;
    }

    Rectangle2D(double x, double y, double width, double height) {
        this->x = x;
        this->y = y;
        this->width = width;
        this->height = height;
    }

    double getArea() const {
        return width * height;
    }

    double getPerimeter() const {
        return 2 * (width + height);
    }

    bool contains(double x, double y) const {
        double left = this->x - width / 2.0;
        double right = this->x + width / 2.0;
        double top = this->y - height / 2.0;
        double bottom = this->y + height / 2.0;
        return (x >= left && x <= right && y >= top && y <= bottom);
    }

    bool contains(const Rectangle2D& r) const {
        double left = r.x - r.width / 2.0;
        double right = r.x + r.width / 2.0;
        double top = r.y - r.height / 2.0;
        double bottom = r.y + r.height / 2.0;
        return (contains(left, top) && contains(right, top)
            && contains(left, bottom) && contains(right, bottom));
    }

    bool overlaps(const Rectangle2D& r) const {
        double x1 = x - width / 2.0;
        double y1 = y - height / 2.0;
        double x2 = r.x - r.width / 2.0;
        double y2 = r.y - r.height / 2.0;
        double x3 = x + width / 2.0;
        double y3 = y + height / 2.0;
        double x4 = r.x + r.width / 2.0;
        double y4 = r.y + r.height / 2.0;
        double left = max(x1, x2);
        double top = max(y1, y2);
        double right = min(x3, x4);
        double bottom = min(y3, y4);
        return (left < right && top < bottom);
    }
};

int main() {
    ofstream fout('Result.txt');
    Rectangle2D r1(2, 2, 5.5, 4.9);
    Rectangle2D r2(4, 5, 10.5, 3.2);
    Rectangle2D r3(3, 5, 2.3, 5.4);
    fout << 'r1's area: ' << r1.getArea() << endl;
    fout << 'r1's perimeter: ' << r1.getPerimeter() << endl;
    fout << 'r1 contains (3, 3): ' << r1.contains(3, 3) << endl;
    fout << 'r1 contains r2: ' << r1.contains(r2) << endl;
    fout << 'r1 overlaps r3: ' << r1.overlaps(r3) << endl;
    fout.close();
    return 0;
}

Explanation:

  1. Class Definition: The Rectangle2D class has private data members x, y, width, and height, representing the center coordinates, width, and height of the rectangle.
  2. Constructors:
    • The default constructor initializes a rectangle with center (0, 0) and width and height of 1.
    • The parameterized constructor allows you to specify these values when creating a rectangle object.
  3. Getters and Setters: Accessor (getter) and mutator (setter) functions are provided to allow controlled access to the private data members.
  4. getArea() and getPerimeter(): These methods calculate and return the area and perimeter of the rectangle.
  5. contains(double x, double y): This function checks if a given point (x, y) lies inside the rectangle.
  6. contains(const Rectangle2D& r): This function checks if another rectangle r is completely contained within the current rectangle.
  7. overlaps(const Rectangle2D& r): This function checks if another rectangle r overlaps with the current rectangle (meaning they share any common area).

Testing and Output:

The main function creates three Rectangle2D objects, r1, r2, and r3. It calculates r1's area and perimeter, and checks whether r1 contains point (3, 3), contains r2, and overlaps r3. The results are written to a text file named 'Result.txt'.

Key Points:

  • The code demonstrates a good understanding of object-oriented programming principles such as encapsulation, with private data members and public getters and setters.
  • It effectively calculates geometric properties and performs containment and overlap checks on rectangles.
  • The use of comments and clear function names makes the code more readable and understandable.
C++ Rectangle2D Class: Area, Perimeter, and Containment Checks