矩阵的变换。包括缩放、平移、错切

不打扰是莪最后的温柔 提交于 2020-03-11 16:06:49

矩阵的变换。包括缩放、平移、错切-the transformation matrix. Incl...原文链接

#include<graphics.h>
#include<math.h>
typedef struct Matrix
{
float _a11,_a12,_a13;
float _a21,_a22,_a23;
float _a31,_a32,_a33;
} Matrix;
typedef struct Vert3
{
float x;
float y;
float z;

} Vert3;
Matrix *MatTranslation(Matrix * mat,float xoffset,float yoffset)
{
mat->_a11=1.0f;mat->_a12=0.0f;mat->_a13=0.0f;
mat->_a21=0.0f;mat->_a22=1.0f;mat->_a23=0.0f;
mat->_a31=xoffset;mat->_a32=yoffset;mat->_a33=1.0f;

return mat;
}
Matrix *MatTranslationBS(Matrix * mat,float xoffset,float yoffset)
{
mat->_a11=xoffset;mat->_a12=0.0f;mat->_a13=0.0f;
mat->_a21=0.0f;mat->_a22=yoffset;mat->_a23=0.0f;
mat->_a31=0.0f;mat->_a32=0.0f;mat->_a33=1.0f;

return mat;
}
Matrix *MatTranslationCQ(Matrix * mat,float xoffset,float yoffset)
{
mat->_a11=1.0f;mat->_a12=xoffset;mat->_a13=0.0f;
mat->_a21=yoffset;mat->_a22=1.0f;mat->_a23=0.0f;
mat->_a31=0.0f;mat->_a32=0.0f;mat->_a33=1.0f;

return mat;
}
Matrix *MatTranslationx(Matrix * mat)
{
mat->_a11=1.0f;mat->_a12=0.0f;mat->_a13=0.0f;
mat->_a21=0.0f;mat->_a22=-1.0f;mat->_a23=0.0f;
mat->_a31=0.0f;mat->_a32=0.0f;mat->_a33=1.0f;

return mat;
}
Matrix *MatTranslationy(Matrix * mat)
{
mat->_a11=-1.0f;mat->_a12=0.0f;mat->_a13=0.0f;
mat->_a21=0.0f;mat->_a22=1.0f;mat->_a23=0.0f;
mat->_a31=0.0f;mat->_a32=0.0f;mat->_a33=1.0f;

return mat;
}
Matrix *MatTranslationyx(Matrix * mat)
{
mat->_a11=0.0f;mat->_a12=1.0f;mat->_a13=0.0f;
mat->_a21=1.0f;mat->_a22=0.0f;mat->_a23=0.0f;
mat->_a31=0.0f;mat->_a32=0.0f;mat->_a33=1.0f;

return mat;
}
Matrix *MatRotation(Matrix * mat,float alpha)
{
float sinalpha = (float)sin(alpha);
float cosalpha = (float)cos(alpha);
mat->_a11=cosalpha; mat->_a12=sinalpha; mat->_a13=0.0f;
mat->_a21=-sinalpha; mat->_a22=cosalpha; mat->_a23=0.0f;
mat->_a31=0.0f; mat->_a32=0.0f; mat->_a33=1.0f;
return mat;
}

Vert3 * MultVertWithMatrix(Vert3 *vertout, Vert3 *vertin, Matrix mat)
{
vertout->x = mat._a11 * vertin->x + mat ._a21 * vertin ->y + mat ._a31 * vertin ->z ;
vertout->y = mat._a12 * vertin->x + mat ._a22 * vertin ->y + mat ._a32 * vertin ->z ;
vertout->z = mat._a13 * vertin->x + mat ._a23 * vertin ->y + mat._a33 * vertin ->z ;
return vertout;

}
main()
{
    Vert3 a[4]={{0,0,0},{0,0,0},{0,0,0}};
    Vert3 c[4]={{0,0,0},{0,0,0},{0,0,0}};
    Vert3 d[4]={{0,0,0},{0,0,0},{0,0,0}};
    Vert3 e[4]={{0,0,0},{0,0,0},{0,0,0}};
    Vert3 f[4]={{0,0,0},{0,0,0},{0,0,0}};
    Vert3 b[4]={{150,150,1},{150,250,1},{200,250,1},{200,150,1}};
    Matrix m={1,0,0,0,1,0,0,0,1};
    int i,j;

    int graphDriver=DETECT;
    int graphMode=0;
    initgraph(&graphDriver,&graphMode,"");
    setbkcolor(0);
    setcolor(1);

    moveto(b[0].x,b[0].y);
    for(j=0;j<4;j++)
    lineto(b[j].x,b[j].y);
    lineto(b[0].x,b[0].y);
    setcolor(2);
    MatTranslationBS(&m,1.30,1.6);
    i=0;
    MultVertWithMatrix(&a[i], &b[i], m);
    moveto(a[0].x,a[0].y);
    for(i=1;i<4;i++){
        MultVertWithMatrix(&a[i],&b[i], m);
        lineto(a[i].x,a[i].y);
    }
    lineto(a[0].x,a[0].y);
    setcolor(3);
MatTranslation(&m,100,200);
    i=0;
    MultVertWithMatrix(&c[i], &b[i], m);
    moveto(c[0].x,c[0].y);
    for(i=1;i<4;i++){
        MultVertWithMatrix(&c[i],&b[i], m);
        lineto(c[i].x,c[i].y);
    }
    lineto(c[0].x,c[0].y);
    setcolor(4);

MatRotation(&m,-0.6);
    i=0;
    MultVertWithMatrix(&d[i], &b[i], m);
    moveto(d[0].x,d[0].y);
    for(i=1;i<4;i++){
        MultVertWithMatrix(&d[i],&b[i], m);
    lineto(d[i].x,d[i].y);
    }
    lineto(d[0].x,d[0].y);
    setcolor(5);
MatTranslationyx(&m);
    i=0;
    MultVertWithMatrix(&e[i], &b[i], m);
    moveto(e[0].x,e[0].y);
    for(i=1;i<4;i++){
        MultVertWithMatrix(&e[i],&b[i], m);
    lineto(e[i].x,e[i].y);
    }
    lineto(e[0].x,e[0].y);
    setcolor(6);
MatTranslationCQ(&m,0.6,0.9);
    i=0;
    MultVertWithMatrix(&f[i], &b[i], m);
    moveto(f[0].x,f[0].y);
    for(i=1;i<4;i++){
        MultVertWithMatrix(&f[i],&b[i], m);
    lineto(f[i].x,f[i].y);
    }
    lineto(f[0].x,f[0].y);

    getch();
    closegraph();
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!