矩形面积计算
题目描述
已知矩形的边与坐标轴平行,试求其周长,面积,以及两个矩形相交部分面积。
输入格式
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 <iostream>
#include <string.h>
#include <cmath>
#include <algorithm>
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])<x)x=abs(a[i]);
}
for(int i=1;i<4;i++){
if(abs(b[i])<y)y=abs(b[i]);
}
return x*y;
}
}
bool Rectangle::isOverlapped(Rectangle &rec)
{
int L_L=_LB.getX()-rec._LB.getX(),
R_L=_RT.getX()-rec._LB.getX(),
R_R=_RT.getX()-rec._RT.getX(),
L_R=_LB.getX()-rec._RT.getX();
int T_T=_RT.getY()-rec._RT.getY(),
B_T=_LB.getY()-rec._RT.getY(),
B_B=_LB.getY()-rec._LB.getY(),
T_B=_RT.getY()-rec._LB.getY();
if(((L_L*L_R)>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;
}