QT QMatrix4x4 矩阵,用于原点 x 和 y 的缩放和旋转

标签 qt matrix rotation qml

我正在进行图像转换,我想同时从 x 原点和 y 原点缩放和旋转图像。我尝试使用单独的 Scale、Rotate,但它们同时工作。这是我的代码

function setRotation(rotation){
    rt.origin.x=imagQuickitem.anchorPoint.x;
    rt.origin.y=imagQuickitem.anchorPoint.y;
    rt.angle=rotation
    image.transform=rt;
    image.transform=sc;
}

function setScale(scale){
    sc.origin.x=imagQuickitem.anchorPoint.x;
    sc.origin.y=imagQuickitem.anchorPoint.y;
    sc.xScale=scale;
    sc.yScale=scale;
    image.transform=sc;
}

Scale { id:sc; }
Rotation { id:rt; }

好吧,似乎解决方案是 QMatrix4x4,我尝试使用 QMatrix4x4 并找到了这个链接 Qt Transform matrix 但我不知道如何编写同时适用于缩放和旋转的矩阵,我是否应该使用旋转矩阵来实现多重缩放矩阵?

最佳答案

到目前为止,我能想到两种解决方案。解决方案 1:结合旋转和缩放。方案二:显式计算变换矩阵。请参阅以下代码片段:

Image {
    id: img
    source: "Lenna.png"
    property real rotD: 45 // angle (degrees)
    property real rotR:  rotD * Math.PI / 180 // angle (radians)
    property real scale: 2 // scaling value
    property real x0: 264 // origin.x
    property real y0: 264 // origin.y
    // solution 1: combine 2 transforms
    transform: [
        Rotation{origin.x: img.x0; origin.y: img.y0; angle: img.rotD; },
        Scale{origin.x: img.x0; origin.y: img.y0; xScale: img.scale; yScale: img.scale}
    ]
    // solution 2: do the math to calculate the matrix
//    transform: Matrix4x4 {
//    matrix: Qt.matrix4x4(
//        img.scale*Math.cos(img.rotR), -img.scale*Math.sin(img.rotR), 0, -img.scale*Math.cos(img.rotR)*img.x0 + img.scale*Math.sin(img.rotR)*img.y0 + img.x0,
//        img.scale*Math.sin(img.rotR), img.scale*Math.cos(img.rotR),  0, -img.scale*Math.sin(img.rotR)*img.x0 - img.scale*Math.cos(img.rotR)*img.y0 + img.y0,
//        0, 0, 1, 0,
//        0, 0, 0, 1)
//    }
}

关于QT QMatrix4x4 矩阵,用于原点 x 和 y 的缩放和旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47498673/

相关文章:

android - 图像位置无法使用刻度类型矩阵设置中心

python - 使用 ndimage 在 python 中旋转

qt - Qt中quint64的最大数值是多少?

qt - 无法使用Qt环境在Linux中解析库

R:从矩阵中提取相对于行/列位置的值

regex - 如何将两个字符之间的字符串拆分为R中的子组

c++ - 根据 3D 对象的位置旋转相机

swift - 跟踪节点方向

c++ - Qt LNK2019 基本 Qt5 应用程序错误

c++ - 如何编写嵌套的 Initialiser 列表,例如 QPairs 的 QVector