根据倾斜矩形中心点,长宽和倾斜角度,计算顶点,计算两直线交点

已知倾斜矩形中心点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/

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值