四边形的平移旋转

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
 /* 初始化显示窗口大小 */ 
GLsizei winWidth = 600, winHeight = 600;
/* 设置世界坐标系的显示范围 */ 
GLfloat xwcMin = 0.0, xwcMax = 225.0;
GLfloat ywcMin = 0.0, ywcMax = 225.0;
/* 定义二维点数据结构 */ 
class wcPt2D
  { public:
     GLfloat x, y;
   };
typedef GLfloat Matrix4x3[4][3];
  //定义复合矩阵
typedef GLfloat Matrix3x3[3][3];
Matrix3x3 matComposite; 

const GLdouble pi = 3.14159;

void init(void)
  { /* 设置显示窗口的背景颜色为白色 */ 
      glClearColor(1.0, 1.0, 1.0, 0.0);
   }

/* 构建3*3的单位矩阵,对角线为(1,1,1) */ 

void matrix3x3SetIdentity(Matrix3x3 matIdent3x3)
  {
      GLint row, col;
      for (row = 0; row<3; row++)
         for (col = 0; col<3; col++)
             matIdent3x3[row][col] = (row == col);
   }
/* 变换矩阵m1 乘矩阵m2,储存结果到m2中,即矩阵乘法 */
void matrix3x3PreMultiply(Matrix3x3 m1, Matrix3x3 m2)
  {
      GLint row, col;
      Matrix3x3 matTemp;
      for (row = 0; row<3; row++)
           for (col = 0; col<3; col++)
               matTemp[row][col] = m1[row][0] * m2[0][col] 
              + m1[row][1] * m2[1][col] + m1[row][2] * m2[2][col];//行×行,列×列
      for (row = 0; row<3; row++)
            for (col = 0; col<3; col++)
                 m2[row][col] = matTemp[row][col];
}
/* 平移变换函数,平移量tx,ty */ 
void translate2D(GLfloat tx, GLfloat ty)
  {   Matrix3x3 matTransl;
     /* 初始化平移矩阵为单位矩阵 */ 
      matrix3x3SetIdentity(matTransl);
      matTransl[0][2] = tx;//tx为平移的横坐标
      matTransl[1][2] = ty;//ty为平移的纵坐标
      /* 将平移矩阵前乘到复合矩阵matComposite中 */ 
      matrix3x3PreMultiply(matTransl, matComposite);
   } 
/* 旋转变换函数,参数为中心点pivotPt和旋转角度theta */ 
void rotate2D(wcPt2D pivotPt, GLfloat theta)
  {
  Matrix3x3 matRot;
        /* 初始化旋转矩阵为单位矩阵 */ 
  matrix3x3SetIdentity(matRot);
  matRot[0][0] = cos(theta);
  matRot[0][1] = -sin(theta);
  matRot[0][2] = pivotPt.x*(1 - cos(theta)) + pivotPt.y*sin(theta);//先把旋转点平移到原点,再旋转,再平移回去
  matRot[1][0] = sin(theta);
  matRot[1][1] = cos(theta);
  matRot[1][2] = pivotPt.y*(1 - cos(theta)) - pivotPt.x*sin(theta);//先把旋转点平移到原点,再旋转,再平移回去
         /* 将旋转矩阵前乘到复合矩阵matComposite中 */ 
  matrix3x3PreMultiply(matRot, matComposite);
} 
void transformVerts2D(GLint nVerts, wcPt2D * verts)
{
	 GLint k;
	 GLfloat temp;
	 for (k = 0; k<nVerts; k++)
	 {
		//变换矩阵和原来的齐次坐标矩阵相乘
		temp = matComposite[0][0] * verts[k].x + matComposite[0][1] * verts[k].y + matComposite[0][2];
		verts[k].y = matComposite[1][0] * verts[k].x + matComposite[1][1] * verts[k].y + matComposite[1][2];
		 verts[k].x = temp;
	 }
}
void square(wcPt2D * verts)
{
  GLint k;
  glBegin(GL_POLYGON);//绘制四边形的每一个点
      for (k = 0; k<4; k++)  glVertex2f(verts[k].x, verts[k].y);
  glEnd();
} 
void displayFcn(void)
{
	GLint nVerts = 4;
	wcPt2D verts[4] = {  { 50.0,25.0 },{ 150.0,25.0 },{ 150.0,70.0 },{50.0,70.0} };
	wcPt2D centroidPt;
	GLint k, xSum = 0, ySum = 0;
    for (k = 0; k<nVerts; k++)
	 {  xSum += verts[k].x;
		   ySum += verts[k].y; }
	centroidPt.x = GLfloat(xSum) / GLfloat(nVerts);//横坐标平均值为旋转点的x
	centroidPt.y = GLfloat(ySum) / GLfloat(nVerts);//纵坐标平均值为旋转点的y
	/* 设置几何变换参数*/ 
	wcPt2D pivPt, fixedPt;
    pivPt = centroidPt;
	fixedPt = centroidPt;
	GLdouble theta = pi / 2.0; 

	glClear(GL_COLOR_BUFFER_BIT); // 清空显示窗口
    glColor3f(0.0, 0.6, 0.6); // 设置前景色为蓝色
    square(verts); //显示蓝色三角形(变换前)
	  /* 初始化复合矩阵为单位矩阵 */ 
	matrix3x3SetIdentity(matComposite);
	/* 根据变换序列重建复合矩阵 */ 
	rotate2D(pivPt, theta); //变换序列2:旋转变换
	translate2D(0.0, 100.0); //变换序列3:平移变换
	/* 应用复合矩阵到四边形 */ 
	transformVerts2D(nVerts, verts);
	glColor3f(1.0, 0.5, 0.0); //重新设置前景色为红色
	square(verts); //显示红色三角形(变换后)

	glFlush();
}
void winReshapeFcn(GLint newWidth, GLint newHeight)
   {glMatrixMode(GL_PROJECTION); //将当前矩阵指定为投影矩阵 
   glLoadIdentity(); //将当前矩阵设置为单位矩阵 
   gluOrtho2D(xwcMin, xwcMax, ywcMin, ywcMax); 
//定义二维正投影窗口 
   glClear(GL_COLOR_BUFFER_BIT); //清除当前可写的颜色缓冲 
    } 
void main(int argc, char ** argv)
   {
   glutInit(&argc, argv);  // 初始化GLUT库 
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  
 //单缓冲,建立RGB模式窗口 
   glutInitWindowPosition(50, 50);//窗口位置
   glutInitWindowSize(winWidth, winHeight);//窗口大小
   glutCreateWindow("二维几何变换实例-复合变换");
   init();
   glutDisplayFunc(displayFcn);//显示图形
   glutReshapeFunc(winReshapeFcn);
   glutMainLoop();
  } 

通过矩阵的乘法来确定变换矩阵——平移和旋转(缩放也简单,但没时间写了)在本例中尽管变换的是四边形,但是变换矩阵依然是3X3的。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值