前面对于任意方位矩形相交面积的计算发过一篇博客,但是算法耗时比较大,需要800us,无法满足应用需求。于是对其进行了优化,耗时减了一半,只需400us。现在将优化的代码分享出来。算法计算原理还是跟原始的差不多,只是在具体的实现方式上对代码进行了优化。部分代码也是参考网上的代码,在此表示感谢!!!
1. 矩形框的头文件以及对矩形框类的定义
#include <algorithm>
#include <csignal>
#include <ctime>
#include <map>
#include <string>
#include <utility>
#include <vector>
#include <math.h>
#include <iostream>
#define Max(a,b) ((a>b)? a:b)
#define Min(a,b) ((a<b)? a:b)
#define Pi 3.1415926
class NormalizedBBox
{
public:
float x;
float y;
float w;
float h;
float theta;
NormalizedBBox(float x , float y , float w , float h , float theta ){
this->x = x;
this->y = y;
this->w = w;
this->h = h;
this->theta = theta;
}
}
2. 坐标点类简单定义
class Point{
public:
float x;
float y;
Point(float x, float y)
{
this->x = x;
this->y = y;
}
Point()
{
this->x = 0;
this->y = 0;
}
}
3. 计算一个矩形的顶点坐标
void getRectanglVertexs(NormalizedBBox box, vector<Point>& vertexs){
Point temp(0,0);
float cos_theta = cos(box.theta);
float sin_theta = sin(box.theta);
temp.x &#