矩阵坐标转换


比方说画面宽度640,你希望从320开始截取,那就是320/640 = 0.5

E:
X0 = 0
Y0 = 2160
X1 = 1920
Y1 = 3240

OFF_X0 = 0 / 3840
OFF_Y0 = 2160 / 3240 
OFF_X1 = 1920 / 3840
OFF_Y1 = 3240 / 3240

// 转换为矩阵
Rect Control::GetRect(int _width, int _height, int _top, int _left)
{
    Rect rect;
    rect.x0 = _left;
    rect.y0 = _top;
    rect.x1 = _width + _left;
    rect.y1 = _height + _top;
    return std::move(rect);
}

// 计算矩形交点
Rect Control::GetIntersectRect(Rect* _a, Rect* _b, bool* _error)
{
    // 重置
    (*_error) = false;
    // 对象
    Rect rect;
    // 计算
    rect.x0 = max(_a->x0, _b->x0);
    rect.y0 = max(_a->y0, _b->y0);
    rect.x1 = min(_a->x1, _b->x1);
    rect.y1 = min(_a->y1, _b->y1);
    // 判断是否相交
    if (rect.x0 >= rect.x1 || rect.y0 >= rect.y1)
    {
    *_error = true;
    }
    // 相交区域
    return std::move(rect);
}

Offset Control::GetOffset(int _src_top, int _src_left, int _dst_top, int _dst_left)
{
    Offset offset;
    if(_src_left < _dst_left)
    {
    offset.left = 0;
    }
    else
    {
    offset.left = _src_left - _dst_left;
    }
    if(_src_top < _dst_top)
    {
    offset.top = 0;
    }
    else
    {
    offset.top = _src_top - _dst_top;
    }
    return std::move(offset);
}

Size Control::GetRectSize(Rect* _rect)
{
    Size size;
    size.width = _rect->x1 - _rect->x0;
    size.height = _rect->y1 - _rect->y0;
    return std::move(size);
}

// _width 是Pobj的宽
// _height 是Pobj的高
// _rect 是相交区域矩阵坐标
Change Control::GetChangeRect(int _width, int _height, Rect* _rect)
{
    Change change;
    change.off_x0 = float(_rect->x0) / _width;
    change.off_y0 = float(_rect->y0) / _height;
    change.off_x1 = float(_rect->x1) / _width;
    change.off_y1 = float(_rect->y1) / _height;
    return std::move(change);
}

// _b 是Pobj的矩阵坐标
// _c 是相交区域矩阵坐标
Change Control::GetChangeRect(Rect* _b, Rect* _c)
{
    int b_width = _b->x1 - _b->x0;
    int b_height = _b->y1 - _b->y0;
    int c_width = _c->x1 - _c->x0;
    int c_height = _c->y1 - _c->y0;
    Rect v;
    v.x0 = _c->x0 - _b->x0;
    v.y0 = _c->y0 - _b->y0;
    v.x1 = v.x0 + c_width;
    v.y1 = v.y0 + c_height;
    Change change;
    change.off_x0 = float(v.x0) / b_width;
    change.off_y0 = float(v.y0) / b_height;
    change.off_x1 = float(v.x1) / b_width;
    change.off_y1 = float(v.y1) / b_height;
    return std::move(change);
}