天天看点

图像几何变换--平移变换 代码实现

/*
c++14
date:2021-3-8
author:hsj
实现对某个点平移变换算法,矩阵乘法
*/

#include<bits/stdc++.h>
using namespace std;
const int N=4;
struct Matrix
{
    int mat[N][N];
    Matrix(){
        memset(mat, 0, sizeof(mat));
    }
    Matrix operator *  (const Matrix &o)const
    {
        Matrix res;
        memset(res.mat, 0, sizeof(res.mat));
        for(int i=0;i<N;++i)
        for(int j=0;j<N;++j)
        for(int k=0;k<N;++k) res.mat[i][j] += mat[i][k]*o.mat[k][j];
        return res;
    }

};

struct node
{
    int x, y, z, w;
    node (int x0, int y0, int z0){
        x=x0, y=y0, z=z0, w=1;
    }
    node(){}
    node glTranslated(int x0, int y0, int z0)
    {
        Matrix first, second, res;
        first.mat[0][0]=1;first.mat[0][3]=x0;
        first.mat[1][1]=1;first.mat[1][3]=y0;
        first.mat[2][2]=1;first.mat[2][3]=z0;
        first.mat[3][3]=1;first.mat[3][3]=1;
        second.mat[0][0]=x;
        second.mat[1][0]=y;
        second.mat[2][0]=z;
        second.mat[3][0]=1;
        res = first * second;
        node ans = {res.mat[0][0], res.mat[1][0], res.mat[2][0]};
        return ans;

    }
};

int main()
{
    node first = {1,1,1};
    node ans = first.glTranslated(1,2,3);
    printf("%d %d %d\n", ans.x, ans.y, ans.z);
}
           

继续阅读