博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
4.4.1 二维复合矩阵编程实例
阅读量:7082 次
发布时间:2019-06-28

本文共 2807 字,大约阅读时间需要 9 分钟。

(a)变换前的三角形                 (b)变换后的三角形          (c)程序显示结果

1 #include 
2 3 #include
4 5 #include
6 7 /* 初始化显示窗口大小 */ 8 9 GLsizei winWidth=600,winHeight=600; 10 11 /* 设置世界坐标系的显示范围 */ 12 13 GLfloat xwcMin=0.0,xwcMax=225.0; 14 15 GLfloat ywcMin=0.0,ywcMax=225.0; 16 17 /* 定义二维点数据结构 */ 18 19 class wcPt2D 20 21 { 22 23 public: 24 25 GLfloat x, y; 26 27 }; 28 29 typedef GLfloat Matrix3x3 [3][3]; 30 31 Matrix3x3 matComposite; //定义复合矩阵 32 33 const GLdouble pi=3.14159; 34 35 void init (void) 36 37 { 38 39 /* 设置显示窗口的背景颜色为白色 */ 40 41 glClearColor(1.0,1.0,1.0,0.0); 42 43 } 44 45 /* 构建3*3的单位矩阵 */ 46 47 void matrix3x3SetIdentity(Matrix3x3 matIdent3x3) 48 49 { 50 51 GLint row,col; 52 53 for (row=0;row<3;row++) 54 55 for (col=0;col<3;col++) 56 57 matIdent3x3[row][col]=(row==col); 58 59 } 60 61 /* 变换矩阵m1前乘矩阵m2,储存结果到m2中 */ 62 63 void matrix3x3PreMultiply(Matrix3x3 m1, Matrix3x3 m2) 64 65 { 66 67 GLint row, col; 68 69 Matrix3x3 matTemp; 70 71 for(row=0; row<3;row++) 72 73 for(col=0;col<3;col++) 74 75 matTemp[row][col]=m1[row][0]*m2[0][col]+m1[row][1]*m2[1][col]+m1[row][2]*m2[2][col]; 76 77 for(row=0;row<3;row++) 78 79 for(col=0;col<3;col++) 80 81 m2[row][col]=matTemp[row][col]; 82 83 } 84 85 /* 平移变换函数,平移量tx,ty */ 86 87 void translate2D(GLfloat tx,GLfloat ty) 88 89 { 90 91 Matrix3x3 matTransl; 92 93 /* 初始化平移矩阵为单位矩阵 */ 94 95 matrix3x3SetIdentity(matTransl); 96 97 matTransl[0][2]=tx; 98 99 matTransl[1][2]=ty;100 101 /* 将平移矩阵前乘到复合矩阵matComposite中 */102 103 matrix3x3PreMultiply(matTransl,matComposite);104 105 }106 107 /* 旋转变换函数,参数为中心点pivotPt和旋转角度theta */108 109 void rotate2D(wcPt2D pivotPt, GLfloat theta)110 111 {112 113 Matrix3x3 matRot;114 115 /* 初始化旋转矩阵为单位矩阵 */116 117 matrix3x3SetIdentity(matRot);118 119 matRot[0][0]=cos(theta);120 121 matRot[0][1]=-sin(theta);122 123 matRot[0][2]=pivotPt.x*(1-cos(theta))+pivotPt.y*sin(theta);124 125 matRot[1][0]=sin(theta);126 127 matRot[1][1]=cos(theta);128 129 matRot[1][2]=pivotPt.y*(1-cos(theta))-pivotPt.x*sin(theta);130 131 /* 将旋转矩阵前乘到复合矩阵matComposite中 */132 133 matrix3x3PreMultiply(matRot,matComposite);134 135 }136 137 /* 比例变换函数,参数为基准点fixedPt和缩放比例sx、sy */138 139 void scale2D(GLfloat sx,GLfloat sy,wcPt2D fixedPt)140 141 {142 143 Matrix3x3 matScale;144 145 /* 初始化缩放矩阵为单位矩阵 */146 147 matrix3x3SetIdentity(matScale);148 149 matScale[0][0]=sx;150 151 matScale[0][2]=(1-sx)*fixedPt.x;152 153 matScale[1][1]=sy;154 155 matScale[1][2]=(1-sy)*fixedPt.y;156 157 /* 将缩放矩阵前乘到复合矩阵matComposite中 */158 159 matrix3x3PreMultiply(matScale,matComposite);160 161 }162 163 /* 利用复合矩阵计算变换后坐标 */164 165 void transformVerts2D(GLint nVerts,wcPt2D * verts)166 167 {168 169 GLint k;170 171 GLfloat temp;172 173 for(k=0;k

附上本实验的VC++(VC++2008)

转载于:https://www.cnblogs.com/opengl/p/3790393.html

你可能感兴趣的文章
项目实战:LAMP环境+Xcache+Redis,另附Memcached配置。
查看>>
ORA-30036故障处理思路
查看>>
WINDOWS7更改访问windows共享的用户名和密码
查看>>
Advanced Threat Analytics 2016
查看>>
SFB 项目经验-31-批量为n台服务器导入PFX证书
查看>>
0-Microsoft Lync Server 2010-部署
查看>>
混乱开发,既伤身体又伤感情
查看>>
Hi,我们的代码重构了
查看>>
电子商务站点遭勒索 F5路见不平显神功
查看>>
如何在项目实施过程提高系统运维水平
查看>>
sqlldr的介绍
查看>>
iptables从入门到精通
查看>>
马哥Linux线下活动和学员风采
查看>>
PHP中$_SERVER的详细参数与说明
查看>>
Android Studio使用说明
查看>>
功能应用程序延迟发送:4款定时发送短信的应用程序
查看>>
linux shell脚本获得当前文件路径
查看>>
畅想(3)-下一步的计划
查看>>
Tail Recursion, Recursion, Concepts and Examples
查看>>
任务超期后的定时器处理
查看>>