//Project - Rect #include <iostream> #include <cmath> using namespace std; //定义Point类 //定义Rect类 int main() { auto rt = Rect(Point(1,6),Point(7,8)); printf("Vertices of rectangle rt:\n"); printf("(%d,%d)-----------------------(%d,%d)\n", rt.tl.x,rt.tl.y,rt.topRight().x,rt.topRight().y); printf("(%d,%d)-----------------------(%d,%d)\n", rt.bottomLeft().x,rt.bottomLeft().y,rt.br.x,rt.br.y); printf("Size information of rectangle rt:\n"); printf("width - %d height - %d\n",rt.width(),rt.height()); printf("area - %d diagonal legnth - %.2f",rt.area(),rt.diagonalLength()); return 0; }请你补全这个代码
时间: 2024-01-17 19:05:15 浏览: 108
下面是补全后的代码:
```
#include <iostream>
#include <cmath>
using namespace std;
//定义Point类
class Point {
public:
int x, y;
Point(int x, int y) : x(x), y(y) {}
};
//定义Rect类
class Rect {
public:
Point tl, br;
Rect(Point tl, Point br) : tl(tl), br(br) {}
// 返回矩形的左下角坐标
Point bottomLeft() const {
return Point(tl.x, br.y);
}
// 返回矩形的右上角坐标
Point topRight() const {
return Point(br.x, tl.y);
}
// 返回矩形的宽度
int width() const {
return abs(br.x - tl.x);
}
// 返回矩形的高度
int height() const {
return abs(br.y - tl.y);
}
// 返回矩形的面积
int area() const {
return width() * height();
}
// 返回矩形对角线的长度
double diagonalLength() const {
return sqrt(pow(width(), 2) + pow(height(), 2));
}
};
int main() {
auto rt = Rect(Point(1,6), Point(7,8));
printf("Vertices of rectangle rt:\n");
printf("(%d,%d)-----------------------(%d,%d)\n", rt.tl.x, rt.tl.y, rt.topRight().x, rt.topRight().y);
printf("(%d,%d)-----------------------(%d,%d)\n", rt.bottomLeft().x, rt.bottomLeft().y, rt.br.x, rt.br.y);
printf("Size information of rectangle rt:\n");
printf("width - %d height - %d\n", rt.width(), rt.height());
printf("area - %d diagonal legnth - %.2f", rt.area(), rt.diagonalLength());
return 0;
}
```
阅读全文