当前位置: > 装修知识 > 正文
矩形面积计算
2022-10-18 17:04:33矩形面积计算
题目描述
已知矩形的边与坐标轴平行,试求其周长,面积,以及两个矩形相交部分面积。
输入格式
x1 y1 x2 y2 x3 y3 x4 y4
说明:输入为8个整数,中间以空格分隔。前四个整数确定第一个矩形的位置,形如 x1 y1 x2 y2,其中为左下角坐标,为右上角坐标。后四个整数确定第二个矩形的位置。
输出格式
5个整数矩形面积,以空格分隔矩形面积,分别是第一个矩形的周长、面积;第二个矩形的周长、面积;以及两个矩形相交部分面积。
输入样例
1 1 4 4 2 2 5 5
输出样例
12 9 12 9 4
#include
#include
#include
#include
using namespace std;
class Point
{
public:
Point(int xx = 0, int yy = 0)
{
_x = xx;
_y = yy;
}
Point(Point &p);
int getX();
int getY();
private:
int _x;
int _y;
};
Point::Point(Point &p)
{ //复制构造函数的实现
_x = p._x;
_y = p._y;
}
int Point::getX()
{
return this->_x;
}
int Point::getY()
{
return this->_y;
}
class Line
{
public:
Line(Point &a, Point &b);
int getLength();
private:
void calLength(); //计算线段长度,并保存到属性 _len 中
Point _pa, _pb;
int _len;
};
Line::Line(Point &a, Point &b) : _pa(a), _pb(b)
{
this->calLength();
}
int Line::getLength()
{
return this->_len;
}
//只考虑平行状况
void Line::calLength()
{
int x=abs(_pa.getX()-_pb.getX());
int y=abs(_pa.getY()-_pb.getY());
_len=sqrt(x*x+y*y);
}
class Rectangle
{
public:
Rectangle(Point &lb_pt, Point &rt_pt); //两个输入参数分别是左下角,右上角
int getPerimeter(); //周长
int getArea(); //面积
int getOverlappedArea(Rectangle &rec); //计算与另一个矩形重合面积
private:
bool isOverlapped(Rectangle &rec);
Point _LB, _RT, _LT, _RB; //左下角,左上角,右下角,右上角
Line _horizontal_line;
Line _vertical_line;
};
Rectangle::Rectangle(Point &lb_pt, Point &rt_pt) : _LB(lb_pt),
_RT(rt_pt),
_LT(_LB.getX(), _RT.getY()),
_RB(_RT.getX(), _LB.getY()),
_horizontal_line(_LB, _RB),
_vertical_line(_LB, _LT)
{
}
int Rectangle::getPerimeter()
{
return 2 * (this->_horizontal_line.getLength() + this->_vertical_line.getLength());
}
int Rectangle::getArea()
{
return this->_horizontal_line.getLength() * this->_vertical_line.getLength();
}
int Rectangle::getOverlappedArea(Rectangle &rec)
{
if (!this->isOverlapped(rec))
{
return 0;
}
else
{
int a[4]={0},b[4]={0};
a[0]=_LB.getX()-_RT.getX();
a[1]=_LB.getX()-rec._RT.getX();
a[2]=rec._LB.getX()-_RT.getX();
a[3]=rec._LB.getX()-rec._RT.getX();
b[0]=_RT.getY()-_LB.getY();
b[1]=_RT.getY()-rec._LB.getY();
b[2]=rec._RT.getY()-_LB.getY();
b[3]=rec._RT.getY()-rec._LB.getY();
int x=abs(a[0]),y=abs(b[0]);//x为长,y为宽
for(int i=1;i<4;i++){
if(abs(a[i])0&&(R_L*R_R)>0)||((T_T*T_B)>0&&(B_T*B_B)>0)){
return false;
}
else return true;
}
int main()
{
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int x3, y3, x4, y4;
cin >> x3 >> y3 >> x4 >> y4;
Point p1(x1, y1), p2(x2, y2), p3(x3, y3), p4(x4, y4);
Rectangle rec1(p1, p2);
Rectangle rec2(p3, p4);
cout << rec1.getPerimeter() << " " << rec1.getArea();
cout << " " << rec2.getPerimeter() << " " << rec2.getArea();
cout << " " << rec1.getOverlappedArea(rec2);
return 0;
}
以上内容为蝴蝶兰风评投稿者为大家精心整理,希望对大家有所帮助!
- 上一篇:公共卫生间隔断尺寸是多少(图片)
- 下一篇:
猜你喜欢
- 2022-09-30 10:28:30Leetcode - 223 矩形面积
- 2022-09-30 04:37:28微积分与矩形面积
留言与评论(共有 条评论) |
- 搜索
-
- 10-18矩形面积计算
- 10-18公共卫生间隔断尺寸是多少(图片)
- 10-18老房子翻新装修后多久可以入住
- 10-18细石混凝土容重 公路养护技师、高级工、初级工考试复习重点
- 10-18饮水机的水能烧到100度吗0
- 10-18S45C是什么材料,性能参数怎样?
- 10-18分手后女人不可能复合的表现是什么?什么情况分手可以复合?
- 10-18玻化砖厚度
- 10-18公共卫生间隔断尺寸是多少
- 10-18一种钢渣混凝土的制作方法