矩形旋转算法
小女子实在是菜手,稍大一点的算法就不知怎么办了,全乱套了!真希望各位高手出手相救,帮我看下这里的C++代码,哪怕再幼稚的问题,也请高手们不惜笔墨指出来。谢谢啦!#include<GL/glut.h>
#include<math.h>
#include<stdlib.h>
#define DEG_TO_RAD 0.017453
float theta=30.0;
float x=300.0, y=200.0;
float length=200;
float width=300;
GLint nVerts=3;
class wcPt2D {
public:
GLfloat x, y;
};
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 600.0, 0.0, 400.0);
}
void rotatePolygon (wcPt2D * verts, GLint nVerts, wcPt2D pivPt,
GLdouble theta)
{
wcPt2D * vertsRot;
GLint k;
for (k = 0; k < nVerts; k++) {
vertsRot [k].x = pivPt.x + (verts [k].x - pivPt.x) * cos (theta)
- (verts [k].y - pivPt.y) * sin (theta);
vertsRot [k].y = pivPt.y + (verts [k].x - pivPt.x) * sin (theta)
+ (verts [k].y - pivPt.y) * cos (theta);
}
glBegin {GL_POLYGON};
for (k = 0; k < nVerts; k++)
glVertex2f (vertsRot [k].x, vertsRot [k].y);
glEnd ( );
}
void display (void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 0.0);
glBegin (GL_POLYGON);
glVertex2f (pivPt.x,pivPt.y);
glVertex2f (vertsRot[k].x,vertsRot[k].y );
glEnd ( );
glutSwapBuffers ( );
}
void myKeyboard(unsigned char key,int x,int y)
{
if(key == 'a' || key == 'A')
theta += 5.0;
if(key == 's' || key == 'S')
theta -= 5.0;
if(key == 'c' || key == 'C')
exit(c);
if (theta>360) theta -=360;
if (theta<0) theta +=360;
glutPostRedisplay();
}
void mySpecialKeyboard(int key, int x, int y)
{
if(key == GLUT_KEY_F1 || key == GLUT_KEY_LEFT)
theta += 5.0;
if(key == GLUT_KEY_F2 || key == GLUT_KEY_RIGHT)
theta -= 5.0;
if (theta>360) theta -=360;
if (theta<0) theta +=360;
glutPostRedisplay(); //重新调用绘制函数
}
void main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition (50, 100);
glutInitWindowSize (600 400);
glutCreateWindow ("Draw Rectangle with Double Buffer");
glBegin(GL_POLYGON);
glVertex2f(pivPt.x,pivPt.y);
glVertex2f ( pivPt.x + length*cos(DEG_TO_RAD*theta),
pivPt.y + length*sin(DEG_TO_RAD*theta) );
glVertex2f ( pivPt.x + length*cos(DEG_TO_RAD*(theta+45.0)),
pivPt.y + length*sin(DEG_TO_RAD*(theta+45.0) );
glVertex2f ( pivPt.x + length*cos(DEG_TO_RAD*(theta+90.0)),
pivPt.y + length*sin(DEG_TO_RAD*(theta+90.0) );
glEnd();
init ( );
glutDisplayFunc (display);
glutSpecialFunc(mySpecialKeyboard); //指定空闲回调函数
glutMainLoop ( );
}