由于我们团队项目进行到一半,不便传上来代码。我就这周软件工程课程的内容写一点感想吧。
下面这是我们图形学课程实验的部分代码:
/**********myInit**********/ void myInit() { pn = 0; glClearColor(1.0, 1.0, 1.0, 0.0); glColor3f(0.2f, 0.2f, 0.9f); glPointSize(5.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluOrtho2D(-300, 300, -300, 300); } /**********myDisplay**********/ void myDisplay() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_LINES); glVertex2i(-301, 0); glVertex2i(301, 0); glVertex2i(0, -301); glVertex2i(0, 301); glEnd(); glFlush(); } /**********myMouse**********/ void myMouse(int button, int state, int x, int y) { if (state == GLUT_DOWN) { pn++; p[pn].px = x - 300; p[pn].py = 300 - y; glBegin(GL_POINTS); glVertex2i(x - 300 , 300 - y); glEnd(); glFlush(); } } /**********main***********/ void main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(600, 600); glutInitWindowPosition(100, 150); glutCreateWindow("Tuxingxue2_1"); //创建菜单,绑定右键 int menu_id; menu_id = glutCreateMenu(right_menu); glutAddMenuEntry("Clear Screen", 0); glutAddMenuEntry("Draw Line", 1); glutAddMenuEntry("Draw Polygon", 2); glutAddMenuEntry("Draw Circle", 3); glutAddMenuEntry("Translate", 4); glutAddMenuEntry("Rotate", 5); glutAddMenuEntry("Size", 6); glutAddMenuEntry("Exit", 10); glutAttachMenu(GLUT_RIGHT_BUTTON); myInit(); glutDisplayFunc(myDisplay); glutMouseFunc(myMouse); glutMainLoop(); } //画圆 void DrawCircle(void) { glBegin(GL_POLYGON); for (int i = 0; i < n; ++i) { glVertex2f(0.1*cos(2 * Pi / n*i), 0.1*sin(2 * Pi / n*i)); } glEnd(); glFlush(); } //右键菜单内容选择事件在这里触发 void right_menu(int id) { if (id == 10) exit(0); else if (id == 0) //Clear Screen { pn = 0; glClearColor(1.0, 1.0, 1.0, 0.0); glColor3f(0.2f, 0.2f, 0.9f); glPointSize(5.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluOrtho2D(-300, 300, -300, 300); glutPostRedisplay(); } else if (id == 1) //Draw Line { //glClear(GL_COLOR_BUFFER_BIT); DrawType = 0; glBegin(GL_LINES); for (int i = 1; i <= pn; i++) { glVertex2i(p[i].px, p[i].py); } glEnd(); glFlush(); } else if (id == 2) //Draw Polygon { DrawType = 1; //glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); for (int i = 1; i <= pn; i++) { glVertex2i(p[i].px, p[i].py); } glEnd(); glFlush(); } else if (id == 3) //Draw Circle { DrawType = 2; //glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); for (int i = 0; i < n; ++i) { glVertex2f(p[pn].px + R*cos(2 * Pi / n*i), p[pn].py + R*sin(2 * Pi / n*i)); } glEnd(); glFlush(); } else if (id == 4) //Translate { glTranslatef(100, 80, 0.0); //向右移动,向上移动 glColor3f(0.0, 1.0, 0.0); switch (DrawType) { case 0: glBegin(GL_LINES); for (int i = 1; i <= pn; i++) { glVertex2i(p[i].px, p[i].py); } glEnd(); break; case 1: glBegin(GL_POLYGON); for (int i = 1; i <= pn; i++) { glVertex2i(p[i].px, p[i].py); } glEnd(); break; case 2: glBegin(GL_POLYGON); for (int i = 0; i < n; ++i) { glVertex2f(p[pn].px + R*cos(2 * Pi / n*i), p[pn].py + R*sin(2 * Pi / n*i)); } glEnd(); break; default: break; } //glLoadIdentity(); glFlush(); } else if (id == 5) //Rotate { //glTranslatef(100, 0, 0.0); glRotatef(60, 0, 0, 1.0); //旋转60° //glTranslatef(-100, 0, 0.0); glColor3f(0.0, 1.0, 0.0); switch (DrawType) { case 0: glBegin(GL_LINES); for (int i = 1; i <= pn; i++) { glVertex2i(p[i].px, p[i].py); } glEnd(); break; case 1: glBegin(GL_POLYGON); for (int i = 1; i <= pn; i++) { glVertex2i(p[i].px, p[i].py); } glEnd(); break; case 2: glBegin(GL_POLYGON); for (int i = 0; i < n; ++i) { glVertex2f(p[pn].px + R*cos(2 * Pi / n*i), p[pn].py + R*sin(2 * Pi / n*i)); } glEnd(); break; default: break; } //glLoadIdentity(); glFlush(); } else if (id == 6) //Size { //glTranslatef(-100, -50, 0.0); glScalef(2, 2, 1.0); //x axis, y axis //glTranslatef(100, 50, 0.0); glColor3f(0.0, 1.0, 0.0); switch (DrawType) { case 0: glBegin(GL_LINES); for (int i = 1; i <= pn; i++) { glVertex2i(p[i].px, p[i].py); } glEnd(); break; case 1: glBegin(GL_LINE_LOOP); for (int i = 1; i <= pn; i++) { glVertex2i(p[i].px, p[i].py); } glEnd(); break; case 2: glBegin(GL_LINE_LOOP); for (int i = 0; i < n; ++i) { glVertex2f(p[pn].px + R*cos(2 * Pi / n*i), p[pn].py + R*sin(2 * Pi / n*i)); } glEnd(); break; default: break; } //glLoadIdentity(); glFlush(); } }
这周的软件工程课着重讲解了程序代码的格式以及注释的重要性。所以我在写程序的时候注意了一下。当然肯定没有要求的那么标准,只是简单地将程序分了“块”,核心“块”前面加上必要的注释。然后在每个函数之前都有注释说明函数内容。只是还没达到非常正规的要求。未能加入函数的参数以及返回值的具体作用。即使只加入了这么一点代码,就会使整体看着十分舒服。时隔数天再读代码都会觉得思路清晰。以前改格式只是为了看着好看,并没有注意到内在的东西,比如即使是一个函数中的一段代码,在逻辑上也是有层次的,分“块”的意义就在此,将这些内在的逻辑清楚明白地展现出来,既美观整洁,又有利于团队合作。