已知载具(矩形)的中心点坐标、长、宽和倾斜角度,求载具(矩形)四个边界点

已知载具(矩形)的中心点坐标、长、宽和倾斜角度,求载具(矩形)四个边界点

  • 思路方法
  • 代码实现
  • 实验结果

思路方法

1、以中心点的坐标,求不旋转的情况下矩形的四个顶点坐标;
2、再求按逆时针旋转目标角度后的四个点坐标;

代码实现

#include 
#include 
#include 

using namespace std;

//坐标点
struct PPoint
{
	double x;//坐标点x坐标
	double y;//坐标点y坐标
	PPoint(double _x, double _y) :x(_x), y(_y) {};
	PPoint() :x(0), y(0) {};
	PPoint(const PPoint &other) :x(other.x), y(other.y) {};
	const PPoint operator=(const PPoint& p)
	{
		x = p.x;
		y = p.y;
		return *this;
	}
	bool operator==(const PPoint& p)
	{
		return(abs(x - p.x) < 0.0001 && abs(y - p.y) < 0.0001);
	}
};
//************************************
// Method:    GenerateCarBoundaryPoint
// FullName:  GenerateCarBoundaryPoint
// Access:    public 
// Returns:   void
// Qualifier:根据载具中心坐标生成载具四个边界点的坐标
// Parameter: double car_length		载具长
// Parameter: double car_width		载具宽
// Parameter: PPoint & center 		中心点
// Parameter: double angle			倾斜角度 非弧度角 [0,360) x轴正方向为0度
// Parameter: PPoint & front_left	左前点
// Parameter: PPoint & back_left	左后点
// Parameter: PPoint & back_right	右后点
// Parameter: PPoint & front_right	右前点
//************************************
void CalculateCarBoundaryPoint(double car_length, double car_width, PPoint &center, double angle, PPoint &front_left, PPoint &back_left, PPoint &back_right, PPoint &front_right)
{
	//角度为负值,错误输入,返回
	if (angle < 0) return;
	//角度转换
	angle = angle / 180 * 3.1415926;
	double X1, Y1, X2, Y2, X3, Y3, X4, Y4;
	//以(x,y)为中心点,不旋转的情况下四个顶点的坐标
	back_right.x = (center.x - car_length / 2);
	back_right.y = (center.y - car_width / 2);
	back_left.x = (center.x - car_length / 2);
	back_left.y = (center.y + car_width / 2);
	front_left.x = (center.x + car_length / 2);
	front_left.y = (center.y + car_width / 2);
	front_right.x = (center.x + car_length / 2);
	front_right.y = (center.y - car_width / 2);
	if (angle <= 0.00001)
		return;
	else
	{
		//按逆时针旋转角度center.x后的四个点坐标
		X1 = (back_right.x - center.x) * cos(angle) - (back_right.y - center.y) * sin(angle) + center.x;
		Y1 = (back_right.y - center.y) * cos(angle) + (back_right.x - center.x) * sin(angle) + center.y;
		X2 = (back_left.x - center.x) * cos(angle) - (back_left.y - center.y) * sin(angle) + center.x;
		Y2 = (back_left.y - center.y) * cos(angle) + (back_left.x - center.x) * sin(angle) + center.y;
		X3 = (front_left.x - center.x) * cos(angle) - (front_left.y - center.y) * sin(angle) + center.x;
		Y3 = (front_left.y - center.y) * cos(angle) + (front_left.x - center.x) * sin(angle) + center.y;
		X4 = (front_right.x - center.x) * cos(angle) - (front_right.y - center.y) * sin(angle) + center.x;
		Y4 = (front_right.y - center.y) * cos(angle) + (front_right.x - center.x) * sin(angle) + center.y;
		back_right.x = X1;
		back_right.y = Y1;
		back_left.x = X2;
		back_left.y = Y2;
		front_left.x = X3;
		front_left.y = Y3;
		front_right.x = X4;
		front_right.y = Y4;
	}
}
int main()
{

	PPoint center(20, 45);
	double angle = 16;
	vector<PPoint> points(4);
	CalculateCarBoundaryPoint(50, 20, center, angle, points[0], points[1], points[2], points[3]);
	cout << "中心点坐标为:" << center.x << "  " << center.y << endl;
	cout << "(逆时针)倾斜角度为:" << angle << endl;
	cout << "各个边角点的坐标为:" << endl;
	for (auto p : points)
		cout << p.x << "  " << p.y << endl;

	//输出到文件中
	fstream file;
	file.open("points.csv");
	if (!file.is_open())
	{
		cout << "error" << errno << endl;
		cerr << "文件打开出错" << endl;
		return 1;
	}
	file << center.x << ',' << center.y << endl;
	for (auto p : points)
		file << p.x << ',' << p.y << endl;
	file << points.front().x << ',' << points.front().y << endl;
	file.close();
	return 1;
}

实验结果

matlab绘制矩形的代码为:

 [Points,txt1,raw1]=xlsread('C:\Users\YW\source\repos\ConsoleApplication10\ConsoleApplication10\points.csv') ;
Points=Points';
%绘制中心点,以实心圆形代替
rectangle('Position',[Points(1,1),Points(2,1),1,1],'Curvature',[1,1],  'FaceColor','r');
hold on;
%绘制矩形
X=Points(1,2:6);
Y=Points(2,2:6);
plot(X,Y,'g','LineWidth',0.5);
hold on;
title('载具车矩形图');
xlabel('x坐标');ylabel('y坐标');axis normal

代码运行结果:

已知载具(矩形)的中心点坐标、长、宽和倾斜角度,求载具(矩形)四个边界点_第1张图片矩形的形状(红色圆为矩形中心点):

已知载具(矩形)的中心点坐标、长、宽和倾斜角度,求载具(矩形)四个边界点_第2张图片其它测试(长宽都为30):
已知载具(矩形)的中心点坐标、长、宽和倾斜角度,求载具(矩形)四个边界点_第3张图片已知载具(矩形)的中心点坐标、长、宽和倾斜角度,求载具(矩形)四个边界点_第4张图片
本博文完,感谢您的阅读,望您不吝点赞之手,再次感谢。

你可能感兴趣的:(图的算法及相关操作,c++,经验分享)