OpenCV以指定点为起点指定角度,垂直向上为零度,顺时针增加,画一条覆盖图像的线

void drawLineAngle(Mat& imgDraw, Point pointCenter, float fAngle, const Scalar& color, int thickness = 1, int lineType = LINE_8)
{
    float fDec = fAngle - (int)fAngle; //将任意角度转换到(-180°到180°]之间
    fAngle = (int)fAngle % 360 + fDec;
    if (fAngle > 180.0)
        fAngle = fAngle - 360;
    if (fAngle <= -180.0)
        fAngle = fAngle + 360;

    int nWidth = imgDraw.cols;
    int nHight = imgDraw.rows;
    Point pointEnd(0, 0);
    if (fAngle == 0)
    {
        pointEnd = Point(pointCenter.x, 0);
    }
    else if (fAngle == 180)
    {
        pointEnd = Point(pointCenter.x, nHight - 1);
    }
    else if (fAngle > 0 && fAngle <= 90)//y-y0=k(x-x0)
    {
        float k = -tan((90.0 - fAngle) * CV_PI / 180);
        float x0 = nWidth - 1;
        float y0 = k * (x0 - pointCenter.x) + pointCenter.y;
        float y1 = 0;
        float x1 = (y1 - pointCenter.y) / k + pointCenter.x;
        if (y0 < 0)
            pointEnd = Point(x0, y0);
        else
            pointEnd = Point(x1, y1);
    }
    else if (fAngle > 90.0 && fAngle < 180)
    {
        float k = tan((fAngle - 90.0) * CV_PI / 180);
        float x0 = nWidth - 1;
        float y0 = k * (x0 - pointCenter.x) + pointCenter.y;
        float y1 = nHight - 1;
        float x1 = (y1 - pointCenter.y) / k + pointCenter.x;
        if (y0 < nHight - 1)
            pointEnd = Point(x0, y0);
        else
            pointEnd = Point(x1, y1);
    }
    else if (fAngle >= -90 && fAngle < 0)//y-y0=k(x-x0)
    {
        float k = tan((90.0 + fAngle) * CV_PI / 180);
        float x0 = 0;
        float y0 = k * (x0 - pointCenter.x) + pointCenter.y;
        float y1 = 0;
        float x1 = (y1 - pointCenter.y) / k + pointCenter.x;
        if (y0 < nHight - 1)
            pointEnd = Point(x0, y0);
        else
            pointEnd = Point(x1, y1);
    }
    else if (fAngle > -180 && fAngle < -90)
    {
        float k = tan((fAngle + 90.0) * CV_PI / 180);
        float x0 = 0;
        float y0 = k * (x0 - pointCenter.x) + pointCenter.y;
        float y1 = nHight - 1;
        float x1 = (y1 - pointCenter.y) / k + pointCenter.x;
        if (y0 < nHight - 1)
            pointEnd = Point(x0, y0);
        else
            pointEnd = Point(x1, y1);
    }
    line(imgDraw, pointCenter, pointEnd, color, thickness, lineType);
}

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值