已知倾斜矩形中心点center,长w,宽h,倾斜角theta。顶点计算公式为:
void getRectVertex(const cv::Point center,float theta,float w,float h,Point& a, Point& b, Point& c, Point& d)
{
a.x = center.x + w / 2 * cos(theta) - h / 2 * sin(theta);
a.y = center.y - w / 2 * sin(theta) - h / 2 * cos(theta);
b.x = center.x - w / 2 * cos(theta) - h / 2 * sin(theta);
b.y = center.y + w / 2 * sin(theta) - h / 2 * cos(theta);
c.x = center.x - w / 2 * cos(theta) + h / 2 * sin(theta);
c.y = center.y + w / 2 * sin(theta) + h / 2 * cos(theta);
d.x = center.x + w / 2 * cos(theta) + h / 2 * sin(theta);
d.y = center.y - w / 2 * sin(theta) + h / 2 * cos(theta);
return;
}
求两直线的焦点(向量叉乘法)
//二维向量
struct Vec
{
double x, y;
Vec() = default;
Vec(double x, double y) : x(x), y(y) {}
double len2() const { return x * x + y * y; }
double len() const { return sqrt(len2()); }
Vec operator+(const Vec &b) const { return Vec(x + b.x, y + b.y); }
Vec operator-(const Vec &b) const { return Vec(x - b.x, y - b.y); }
Vec operator*(double t) const { return Vec(x * t, y * t); }
Vec operator/(double t) const { return Vec(x / t, y / t); }
Vec operator-() const { return Vec(-x, -y); }
};
//向量积的模
double cross(const Vec &a, const Vec &b) { return a.x * b.y - b.x * a.y; }
//求解两直线交点(假设为AB,CD)
//p1:直线1的起点A
//v1:直线1的向量(B-A)
//p2 v2同理
Vec intersection(const Vec &p1, const Vec &v1, const Vec &p2, const Vec &v2)
{
double t = cross((p2 - p1), v2) / cross(v1, v2);
return p1 + v1 * t;
}
//https://cx.51halcon.com/halcon/