判断矩形是否重叠(复合类+友元)

判断矩形是否重叠(复合类+友元)

题目描述

用CPoint表示点,用两个CPoint对象表示矩形类CRect的对角线两点。分别实现CPoint类和CRect类,并在主函数用输入的坐标定义4个CPoint类对象,每2个CPoint对象再构造1个CRect对象,然后写个友元函数,判断2个矩形是否重叠。

输入

判断次数

矩形1的对角线顶点坐标x1, y1, x2, y2

矩形2的对角线顶点坐标x1, y1, x2, y2

输出

是否重叠

示例输入

3
1 5 2 9
1 3 2 4
5 6 7 8
5 7 7 7
2 5 1 0
9 4 2 9

示例输出

not overlapped
overlapped
overlapped

#include<iostream>
using namespace std;
class CPoint
{
	public:
		CPoint(){
		}
		CPoint(int x1,int y1)
		{
			x=x1;
			y=y1;
		}
		void set(int x1,int y1)
		{
			x=x1;
			y=y1;
		}
		int showx()
		{
			return x;
		}
		int showy()
		{
			return y;
		}
	public:
		int x;
		int y;
};

class CRect
{
	public:
		CRect(){
		}
		CRect(int x1,int y1,int x2,int y2):CPoint1(x1,y1),CPoint2(x2,y2)
		{
			
		}
		friend void display(CRect & CRect1,CRect & CRect2);
	private:
		CPoint CPoint1;
		CPoint CPoint2;
};

void display(CRect & CRect1,CRect & CRect2)
{
	int leftx1,leftx2,maxx,minx,maxy,miny,lefty1,lefty2;
	if(CRect2.CPoint1.showx()>=CRect2.CPoint2.showx())
	{
		maxx=CRect2.CPoint1.showx();
		minx=CRect2.CPoint2.showx();
	}
	else if(CRect2.CPoint1.showx()<CRect2.CPoint2.showx())
	{
		maxx=CRect2.CPoint2.showx();
		minx=CRect2.CPoint1.showx();
	}
	if(CRect2.CPoint1.showy()>=CRect2.CPoint2.showy())
	{
		maxy=CRect2.CPoint1.showy();
		miny=CRect2.CPoint2.showy();
	}
	else if(CRect2.CPoint1.showy()<CRect2.CPoint2.showy())
	{
		maxy=CRect2.CPoint2.showy();
		miny=CRect2.CPoint1.showy();
	}
	if(CRect1.CPoint1.showx()>=CRect1.CPoint2.showx())
	{
		leftx1=CRect1.CPoint1.showx();
		leftx2=CRect1.CPoint2.showx();
	}
	else if(CRect1.CPoint2.showx()>CRect1.CPoint1.showx())
	{
		leftx1=CRect1.CPoint2.showx();
		leftx2=CRect1.CPoint1.showx();
	}
	if(CRect1.CPoint1.showy()>=CRect1.CPoint2.showy())
	{
		lefty1=CRect1.CPoint1.showy();
		lefty2=CRect1.CPoint2.showy();
	}
	else if(CRect1.CPoint1.showy()<CRect1.CPoint2.showy())
	{
		lefty2=CRect1.CPoint1.showy();
		lefty1=CRect1.CPoint2.showy();		
	}
	if(leftx2<=maxx&&leftx2>=minx&&lefty2<=maxy&&lefty2>=miny||leftx1<=maxx&&leftx1>=minx&&lefty2<=maxy&&lefty2>=miny||leftx2<=maxx&&leftx2>=minx&&lefty1<=maxy&&lefty1>=miny||leftx1<=maxx&&leftx1>=minx&&lefty1<=maxy&&lefty1>=miny)
	{
		cout<<"overlapped"<<endl;
		
	}
	else if((lefty1==lefty2)&&(leftx2+(leftx1-leftx2)*1/2)<=maxx&&(leftx2+(leftx1-leftx2)*1/2)*1/2>=minx||(leftx1==leftx2)&&(lefty2+(lefty1-lefty2)*1/2)<=maxx&&(lefty2+(lefty1-lefty2)*1/2)>=minx||minx==maxx&&(miny+(maxy-miny)*1/2)<=leftx1&&(miny+(maxy-miny)*1/2)>=leftx2||miny==maxy&&(minx+(maxx-minx)*1/2)<=leftx1&&(minx+(maxx-minx)*1/2)>=leftx2)
	{
		cout<<"overlapped"<<endl;
	}
	else
	{
		cout<<"not overlapped"<<endl;
	}
}

int main()
{
	int i,t;
	cin>>t;
	for(i=0;i<t;i++)
	{
		int x1,y1,x2,y2,x3,y3,x4,y4;
		cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
		CPoint CPoint3(x1,y1);
		CPoint CPoint4(x2,y2);
		CRect CRect1(x1,y1,x2,y2);
		CPoint CPoint5(x3,y3);
		CPoint CPoint6(x4,y4);
		CRect CRect2(x3,y3,x4,y4);
		display(CRect1,CRect2);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值