Qt 对于QPainterPath 的镜像、旋转、平移、缩放等处理

开门见山,直接上代码

旋转

//************************************
// 名    称	: GetRotatePath
// 创建日期 : 2023/3/15
// 参    数	: const QPainterPath& path	源path
// 参    数	: qreal angle 角度
// 参    数	: const QPointF& center 旋转中心
// 返 回 值 : QPainterPath
// 备    注 : 按指定中心旋转
//************************************
QPainterPath GetRotatePath(const QPainterPath& path, qreal angle, const QPointF& center)
{
    // 将角度转换为弧度
    qreal rad = qDegreesToRadians(angle);

    // 创建一个QTransform对象,用于旋转路径
    QTransform transform;
    transform.translate(center.x(), center.y());
    transform.rotateRadians(rad);
    transform.translate(-center.x(), -center.y());

    // 对路径进行旋转变换
    return transform.map(path);
}
//************************************
// 名    称	: GetRotatePath
// 创建日期 : 2023/3/15
// 参    数	: const QPainterPath& path	源path
// 参    数	: qreal angle 角度
// 返 回 值 : QPainterPath
// 备    注 : 按path中心旋转
//************************************
QPainterPath GetRotatePath(const QPainterPath& path, qreal angle)
{
    const QPointF center = path.boundingRect().center();

    // 将角度转换为弧度
    qreal rad = qDegreesToRadians(angle);

    // 创建一个QTransform对象,用于旋转路径
    QTransform transform;
    transform.translate(center.x(), center.y());
    transform.rotateRadians(rad);
    transform.translate(-center.x(), -center.y());

    // 对路径进行旋转变换
    return transform.map(path);
}

镜像

//************************************
// 名    称	: GetMirroredPath
// 创建日期 : 2023/3/15
// 参    数	: const QPainterPath& path	源path
// 参    数	: Qt::Orientation orientation 方向
// 返 回 值 : QPainterPath
// 备    注 : 按边界镜像path
//************************************
QPainterPath GetMirroredPath(const QPainterPath& path, Qt::Orientation orientation)
{
    // 创建一个变换矩阵
    QTransform transform;
    if (orientation == Qt::Horizontal) {
        // 水平镜像
        transform = QTransform(-1, 0, 0, 1, path.boundingRect().width(), 0);
    } else if (orientation == Qt::Vertical) {
        // 垂直镜像
        transform = QTransform(1, 0, 0, -1, 0, path.boundingRect().height());
    }

    // 应用变换矩阵并返回镜像后的QPainterPath对象
    return transform.map(path);
}

//************************************
// 名    称	: GetCenterMirrorPath
// 创建日期 : 2023/3/15
// 参    数	: const QPainterPath& path	源path
// 参    数	: Qt::Orientation orientation 方向
// 返 回 值 : QPainterPath
// 备    注 : 按中心镜像path
//************************************
QPainterPath GetCenterMirrorPath(const QPainterPath& path, Qt::Orientation orientation)
{
    QRectF boundingRect = path.boundingRect();
    QPointF center = boundingRect.center();

    // 创建一个变换矩阵
    QTransform transform;
    if (orientation == Qt::Horizontal) {
        // 水平镜像
        transform = QTransform(-1, 0, 0, 1, 2 * center.x(), 0);
    } else if (orientation == Qt::Vertical) {
        // 垂直镜像
        transform = QTransform(1, 0, 0, -1, 0, 2 * center.y());
    }

    // 应用变换矩阵并返回镜像后的QPainterPath对象
    return transform.map(path);
}

缩放

//************************************
// 名    称	: GetScalePath
// 创建日期	: 2022/3/15
// 参    数	: const QPainterPath & path
// 参    数	: double sx		X方向比例
// 参    数	: double sy		Y方向比例
// 返 回 值	: QPainterPath
// 备    注	: 缩放QPainterPath
//************************************
QPainterPath GetScalePath(const QPainterPath& path, double sx, double sy)
{
    QPointF center = path.boundingRect().center();

    QTransform transform;
    transform.translate(center.x(), center.y());
    transform.scale(sx, sy);
    transform.translate(-center.x(), -center.y());

    return transform.map(path);
}

平移

//************************************
// 名    称	: GetTranslatePath
// 创建日期	: 2023/3/15
// 参    数	: const QPainterPath & path
// 参    数	: const QPointF& offset
// 返 回 值	: QPainterPath
// 备    注	: 移动QPainterPath
//************************************
QPainterPath GetTranslatePath(const QPainterPath& path, const QPointF& offset)
{
    // 创建一个QTransform对象,用于平移路径
    QTransform transform;
    transform.translate(offset.x(), offset.y());

    return transform.map(path);
}

缩放并平移到目标区域


//************************************
// 名    称	: GetScaleAndTranslatePath
// 创建日期	: 2022/3/15
// 参    数	: const QPainterPath & path
// 参    数	: const QRectF& targetRect 目标位置
// 返 回 值	: QPainterPath
// 备    注	: 缩放并平移到rect中
//************************************
QPainterPath GetScaleAndTranslatePath(const QPainterPath& path, const QRectF& targetRect)
{
    QRectF rect = path.boundingRect();
    QRectF tarRect = targetRect;

    qreal scaleX = tarRect.width() / rect.width();
    qreal scaleY = tarRect.height() / rect.height();
    qreal scale = qMin(scaleX, scaleY);


    QTransform trans;
    trans.scale(scale, scale);
    QPainterPath tmp_path = trans.map(path);

    QPointF delta = tarRect.center() - tmp_path.boundingRect().center();
    tmp_path.translate(delta.x(), delta.y());

    return tmp_path;
}

//************************************
// 名    称	: GetScaleAndTranslatePath
// 创建日期	: 2022/3/15
// 参    数	: const QPainterPath & path
// 参    数	: const QRectF& targetRect 目标位置
// 参    数	: qreal padding 边距
// 返 回 值	: QPainterPath
// 备    注	: 缩放并平移到rect中
//************************************
QPainterPath GetScaleAndTranslatePath(const QPainterPath& path, const QRectF& targetRect, qreal padding)
{
    QRectF rect = path.boundingRect();
    QRectF tarRect = targetRect.marginsRemoved(QMarginsF(padding, padding, padding, padding));

    qreal scaleX = tarRect.width() / rect.width();
    qreal scaleY = tarRect.height() / rect.height();
    qreal scale = qMin(scaleX, scaleY);


    QTransform trans;
    trans.scale(scale, scale);
    QPainterPath tmp_path = trans.map(path);

    QPointF delta = tarRect.center() - tmp_path.boundingRect().center();
    tmp_path.translate(delta.x(), delta.y());

    return tmp_path;
}

你可能感兴趣的:(qt,c++,c++,开发语言,qt)