#1040 : 矩形判断

#1040 : 矩形判断

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

描述

给出平面上4条线段,判断这4条线段是否恰好围成一个面积大于0的矩形。

输入

输入第一行是一个整数T(1<=T<=100),代表测试数据的数量。

每组数据包含4行,每行包含4个整数x1, y1, x2, y2 (0 <= x1, y1, x2, y2 <= 100000);其中(x1, y1), (x2,y2)代表一条线段的两个端点。

输出

每组数据输出一行YES或者NO,表示输入的4条线段是否恰好围成矩形。

样例输入
3
0 0 0 1
1 0 1 1
0 1 1 1
1 0 0 0
0 1 2 3
1 0 3 2
3 2 2 3
1 0 0 1
0 1 1 0
1 0 2 0
2 0 1 1
1 1 0 1
样例输出
YES
YES
NO
#include <iostream>
#include <limits.h>
#include <set>
using namespace std;


struct Line {
	int x1, y1, x2, y2;
};


int main()
{
	int T;
	cin >> T;
	set<pair<int, int>> st;

	while (T--) {
		st.clear();

		Line line1, line2, line3, line4;

		cin >> line1.x1 >> line1.y1 >> line1.x2 >> line1.y2;
		st.insert(make_pair(line1.x1, line1.y1));
		st.insert(make_pair(line1.x2, line1.y2));


		cin >> line2.x1 >> line2.y1 >> line2.x2 >> line2.y2;
		st.insert(make_pair(line2.x1, line2.y1));
		st.insert(make_pair(line2.x2, line2.y2));

		cin >> line3.x1 >> line3.y1 >> line3.x2 >> line3.y2;
		st.insert(make_pair(line3.x1, line3.y1));
		st.insert(make_pair(line3.x2, line3.y2));

		cin >> line4.x1 >> line4.y1 >> line4.x2 >> line4.y2;
		st.insert(make_pair(line4.x1, line4.y1));
		st.insert(make_pair(line4.x2, line4.y2));

		if (st.size() != 4) {
			cout << "NO" << endl;
			continue;
		}

		double slope1, slope2, slope3, slope4;
		slope1 = (line1.x1 == line1.x2) ? INT_MAX : (double)(line1.y2 - line1.y1) / (line1.x2 - line1.x1);
		slope2 = (line2.x1 == line2.x2) ? INT_MAX : (double)(line2.y2 - line2.y1) / (line2.x2 - line2.x1);
		slope3 = (line3.x1 == line3.x2) ? INT_MAX : (double)(line3.y2 - line3.y1) / (line3.x2 - line3.x1);
		slope4 = (line4.x1 == line4.x2) ? INT_MAX : (double)(line4.y2 - line4.y1) / (line4.x2 - line4.x1);

		if (slope1 == slope2) {
			if (slope3 != slope4) {
				cout << "NO" << endl;
				continue;
			}
			//垂直
			if (((line1.y1 - line1.y2) * (line3.y1 - line3.y2) +
				(line1.x1 - line1.x2) * (line3.x1 - line3.x2)) == 0) {
				cout << "YES" << endl;
			}

			else {
				cout << "NO" << endl;
			}
		}
		///////////////////////////////////
		else if (slope1 == slope3) {
			if (slope2 != slope4) {
				cout << "NO" << endl;
				continue;
			}
			//垂直
			if (((line1.y1 - line1.y2) * (line2.y1 - line2.y2) +
				(line1.x1 - line1.x2) * (line2.x1 - line2.x2)) == 0) {
				cout << "YES" << endl;
			}

			else {
				cout << "NO" << endl;
			}
		}
		/////////////////////////////////
		else if(slope1 == slope4) {
			if (slope2 != slope3) {
				cout << "NO" << endl;
				continue;
			}
			//垂直
			if (((line1.y1 - line1.y2) * (line3.y1 - line3.y2) +
				(line1.x1 - line1.x2) * (line3.x1 - line3.x2)) == 0) {
				cout << "YES" << endl;
			}

			else {
				cout << "NO" << endl;
			}
		}
		/////////////////////////////
		else {
			cout << "NO" << endl;
		}
	}

	return 0;
}




<think>根据用户当前的问题“我想查找使用std::进行矩形表示的相关内容 请问使用std::进行矩形表示的方法”,结合提供的引用内容,我们可以从引用[3]中得知,OpenCV中提供了cv::Rect类用于表示矩形,并且有多种构造函数和成员函数。而引用[1]和引用[2]则分别展示了std::vector<cv::Rect>的使用和std::visit的高级用法。引用[4]展示的是使用智能指针管理图形元素。 用户的问题核心是“使用std::进行矩形表示”,但需要注意的是,标准库(std)本身并没有提供专门的矩形类。通常,表示矩形可以通过以下方式: 1. 使用标准库中的容器(如std::array, std::tuple)组合基本类型来表示矩形。 2. 使用其他库(如OpenCV的cv::Rect)结合标准库容器(如std::vector)来存储和操作矩形。 根据引用[1]和引用[3],用户提到的上下文中似乎是在使用OpenCV的cv::Rect。因此,我们可以这样回答: 在C++中,标准库(std)本身没有提供专门的矩形类,但我们可以使用标准库中的容器和工具来辅助表示和操作矩形。例如: - 使用`std::array<int, 4>`表示一个矩形,其中四个元素分别代表左上角x坐标、左上角y坐标、宽度和高度。 - 使用`std::tuple<int, int, int, int>`同样可以表示矩形,但访问时需要使用std::get。 此外,在图像处理领域,通常会使用像OpenCV这样的第三方库,它们提供了专门的矩形类(如cv::Rect),并且可以与标准库容器(如std::vector)结合使用。 下面是一个使用std::array表示矩形的简单示例: ```cpp #include <array> #include <iostream> int main() { // 使用std::array表示矩形: [x, y, width, height] std::array<int, 4> rect = {10, 20, 30, 40}; std::cout << "矩形: x=" << rect[0] << ", y=" << rect[1] << ", 宽度=" << rect[2] << ", 高度=" << rect[3] << std::endl; return 0; } ``` 然而,在实际图像处理中,更推荐使用OpenCV的cv::Rect,因为它提供了丰富的成员函数(如计算面积、判断是否矩形内等)。引用[3]中详细介绍了cv::Rect的构造函数和成员函数。例如: ```cpp #include <vector> #include <opencv2/opencv.hpp> int main() { // 使用std::vector存储多个cv::Rect std::vector<cv::Rect> rects; rects.push_back(cv::Rect(10, 20, 30, 40)); // 添加一个矩形 rects.emplace_back(50, 60, 70, 80); // 直接构造并添加 // 遍历并打印每个矩形的信息 for (const auto& rect : rects) { std::cout << "矩形: x=" << rect.x << ", y=" << rect.y << ", 宽度=" << rect.width << ", 高度=" << rect.height << ", 面积=" << rect.area() << std::endl; } return 0; } ``` 另外,如果需要多态行为,可以像引用[4]那样使用智能指针来管理矩形对象(如果矩形是某个图形基类的派生类)。但通常矩形是简单的几何图形,直接使用值类型即可。 综上所述,如果用户的项目中已经使用了OpenCV,那么使用cv::Rect配合std::vector等容器是常用且方便的方法。如果用户不希望引入外部库,则可以使用std::array或自定义结构来表示矩形。 §§相关问题§§ 1. 如何用标准库方式计算两个矩形的交集? 2. 使用OpenCV的cv::Rect与使用自定义结构相比有什么优势? 3. 除了std::array和std::tuple,还有哪些标准库组件可以用来表示矩形? </think>### 使用 `std::` 进行矩形表示的方法 在 C++ 中,标准库 (`std::`) 本身**没有直接提供矩形类**,但可以通过以下两种方式实现矩形表示: --- ### 方法 1:使用标准库容器组合表示 用 `std::tuple` 或 `std::array` 组合基本类型: ```cpp // 使用 std::tuple (x, y, width, height) std::tuple<int, int, int, int> rect1{10, 20, 30, 40}; // 使用 std::array [x, y, width, height] std::array<int, 4> rect2{50, 60, 70, 80}; ``` **操作示例**: ```cpp // 访问元素 auto [x1, y1, w1, h1] = rect1; // C++17 结构化绑定 int area = w1 * h1; // 存储多个矩形 std::vector<std::array<int, 4>> rects; rects.push_back({0, 0, 100, 200}); ``` --- ### 方法 2:结合第三方库(推荐) 使用 OpenCV 的 `cv::Rect` 配合标准库容器: ```cpp #include <vector> #include <opencv2/core.hpp> std::vector<cv::Rect> rects; // 标准库容器存储 OpenCV 矩形 // 添加矩形(引用[1][3]) rects.emplace_back(10, 20, 30, 40); // (x,y,w,h) rects.emplace_back(cv::Point(50,60), cv::Size(70,80)); // 遍历操作(引用[1]) for (const auto& rect : rects) { std::cout << "Area: " << rect.area() // 引用[3] << " Top-Left: " << rect.tl() // 成员函数 << "\n"; } ``` **`cv::Rect` 核心功能(引用[3])**: - 构造函数:`cv::Rect(x, y, width, height)` - 关键成员: - `area()`:计算矩形面积 - `tl()`:获取左上角点 - `contains(Point)`:判断是否矩形内 - `width`/`height`:直接访问宽高 --- ### 方法 3:自定义结构 + 标准库工具 ```cpp #include <vector> #include <memory> struct Rect { // 自定义矩形结构 int x, y, w, h; int area() const { return w * h; } }; // 使用标准库容器管理(引用[4]) std::vector<std::shared_ptr<Rect>> rects; rects.push_back(std::make_shared<Rect>(Rect{0,0,100,200})); ``` --- ### 总结建议 1. **纯标准库方案** 使用 `std::array<int,4>` 或 `std::tuple` 表示矩形,适合简单场景。 2. **OpenCV 集成方案**(推荐) 用 `std::vector<cv::Rect>` 实现高效操作(支持面积计算、点包含检测等专业功能)[^1][^3]。 3. **自定义结构方案** 通过 `std::vector` + `std::shared_ptr` 实现内存安全和多态扩展[^4]。 > 对于图形处理,**推荐方法2**(OpenCV+标准库),可直接操作图像边界框(引用[1]示例)并利用专业几何计算功能[^3]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值