C++ Rectangle2D Class: Area, Perimeter, Containment, and Overlap
C++ Rectangle2D Class: Area, Perimeter, Containment, and Overlap
This code implements a C++ class called Rectangle2D that represents a rectangle in a two-dimensional plane. It provides functionalities to calculate the rectangle's area and perimeter, check if a point or another rectangle is contained within it, and determine if it overlaps with another rectangle.
UML Diagram:
+----------------+
| Rectangle2D |
+----------------+
| -x: double |
| -y: double |
| -width: double |
| -height: double|
+----------------+
| +getX(): double|
| +getY(): double|
| +getWidth(): double|
| +getHeight(): double|
| +setX(double x): void|
| +setY(double y): void|
| +setWidth(double width): void|
| +setHeight(double height): void|
| +Rectangle2D(): void|
| +Rectangle2D(double x, double y, double width, double height): void|
| +getArea(): double|
| +getPerimeter(): double|
| +contains(double x, double y): bool|
| +contains(const Rectangle2D &r): bool|
| +overlaps(const Rectangle2D &r): bool|
+----------------+
C++ Code:
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
class Rectangle2D {
private:
double x, y, width, height;
public:
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 getX() const {
return x;
}
double getY() const {
return y;
}
double getWidth() const {
return width;
}
double getHeight() const {
return height;
}
void setX(double x) {
this->x = x;
}
void setY(double y) {
this->y = y;
}
void setWidth(double width) {
this->width = width;
}
void setHeight(double height) {
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;
double right = this->x + width / 2;
double top = this->y + height / 2;
double bottom = this->y - height / 2;
return (x > left && x < right && y > bottom && y < top);
}
bool contains(const Rectangle2D &r) const {
double left1 = x - width / 2;
double right1 = x + width / 2;
double top1 = y + height / 2;
double bottom1 = y - height / 2;
double left2 = r.x - r.width / 2;
double right2 = r.x + r.width / 2;
double top2 = r.y + r.height / 2;
double bottom2 = r.y - r.height / 2;
return (left1 <= left2 && right1 >= right2 &&
top1 >= top2 && bottom1 <= bottom2);
}
bool overlaps(const Rectangle2D &r) const {
double left1 = x - width / 2;
double right1 = x + width / 2;
double top1 = y + height / 2;
double bottom1 = y - height / 2;
double left2 = r.x - r.width / 2;
double right2 = r.x + r.width / 2;
double top2 = r.y + r.height / 2;
double bottom2 = r.y - r.height / 2;
return !(left1 >= right2 || right1 <= left2 ||
top1 <= bottom2 || bottom1 >= top2);
}
};
int main() {
// 创建三个 Rectangle2D 对象
Rectangle2D r1(2, 2, 5.5, 4.9);
Rectangle2D r2(4, 5, 10.5, 3.2);
Rectangle2D r3(3, 5, 2.3, 5.4);
// 输出 r1 的面积和周长
cout << "r1's area is " << r1.getArea() << endl;
cout << "r1's perimeter is " << r1.getPerimeter() << endl;
// 判断点 (3, 3) 是否在 r1 中
if (r1.contains(3, 3)) {
cout << "(3, 3) is inside r1" << endl;
} else {
cout << "(3, 3) is not inside r1" << endl;
}
// 判断 r2 是否在 r1 中
if (r1.contains(r2)) {
cout << "r2 is inside r1" << endl;
} else {
cout << "r2 is not inside r1" << endl;
}
// 判断 r3 是否和 r1 相交
if (r1.overlaps(r3)) {
cout << "r3 overlaps r1" << endl;
} else {
cout << "r3 does not overlap r1" << endl;
}
// 将结果保存到文件
ofstream outfile("Result.txt");
outfile << "r1's area is " << r1.getArea() << endl;
outfile << "r1's perimeter is " << r1.getPerimeter() << endl;
if (r1.contains(3, 3)) {
outfile << "(3, 3) is inside r1" << endl;
} else {
outfile << "(3, 3) is not inside r1" << endl;
}
if (r1.contains(r2)) {
outfile << "r2 is inside r1" << endl;
} else {
outfile << "r2 is not inside r1" << endl;
}
if (r1.overlaps(r3)) {
outfile << "r3 overlaps r1" << endl;
} else {
outfile << "r3 does not overlap r1" << endl;
}
outfile.close();
return 0;
}
Explanation:
- The
Rectangle2Dclass has private data membersx,y,width, andheightrepresenting the center coordinates, width, and height of the rectangle, respectively. - The class provides getter and setter methods for each data member.
- The
getArea()andgetPerimeter()methods calculate the area and perimeter of the rectangle. - The
contains(double x, double y)method checks if a given point is inside the rectangle. - The
contains(const Rectangle2D &r)method checks if another rectangle is fully contained within the current rectangle. - The
overlaps(const Rectangle2D &r)method checks if the current rectangle overlaps with another rectangle. - The
main()function demonstrates the usage of theRectangle2Dclass by creating three rectangles, performing calculations and containment checks, and writing the results to a file named "Result.txt".
This code provides a comprehensive implementation of the Rectangle2D class with various useful methods for working with rectangular shapes in C++ programs. It's well-documented and includes a clear test program for validation.
AI 优化版
原文地址:https://www.cveoy.top/t/topic/orBH 著作权归作者所有。请勿转载和采集!