Computer Science

This is a cpp file allows you to draw three points using the mouse clicks. The points are stored in an array Points[3]. It allows drawing the three points. this code also allows you to move the three points, up, down, left, and right using the keys ‘w’, ‘s’, ‘a’, and ‘d’, respectively. The Question is to… Modify the code and implement the following functionality: Pressing the ‘t’ key will toggle the drawing of the triangle using the three points in the red color, and its centroid in green color. The centroid of a triangle is defined by (x1 + x2 + x3) / 3, and (y1 + y2 + y3) /3. Where x1, x2, and x3 are the x coordinates of the three points, and y1, y2, and y3 are the y coordinates of the three points and User should be able to rotate the triangle counterclockwise and clockwise, by 10 degrees by pressing ‘q’ and ‘e’ keys, respectively. Use glRotate() to rotate the triangle around its centroid and also Allow the user to scale the triangle up and down by a factor of 0.1, by pressing ‘z’ and ‘c’ keys, respectively. Make sure that the scale factor does not go down less than 0.1 and does not go up more than 4. Use glScale() to scale the triangle around its centroid and Pressing the ‘x’ key, should reset rotation and scaling. The cpp file…….. using openGL #include #include #include #include #include #include using namespace std; const int screenWidth = 800; const int screenHeight = 600; struct Point2D { Point2D() { x = 0; y = 0; } Point2D(double _x, double _y) { x = _x; y = _y; } double x, y; }; Point2D Points[4]; int num_clicks = 0; void setupWindow(double left, double right, double bottom, double top) { //Define the World Window glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(left, right, bottom, top); } //<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>> void myInit(void) { glClearColor(1.0, 1.0, 1.0, 0.0); // set white background color glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color glPointSize(4.0); // a ‘dot’ is 4 by 4 pixels setupWindow(0, screenWidth, 0, screenHeight); glViewport(0, 0, screenWidth, screenHeight); } //<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>> void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); // clear the screen glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Write your drawing code here glColor3f(0, 0, 0); glBegin(GL_POINTS); for (int i = 0; i < num_clicks; i++) { glVertex2d(Points[i].x, Points[i].y); } glEnd(); glFlush(); // send all output to display glutSwapBuffers(); //Move drawing to the display buffer } void myKeyboard(unsigned char key, int x, int y) { if (num_clicks < 3) return; if (key == ‘w’) { for (int i = 0; i < num_clicks; i++) { Points[i].y += 10; } } if (key == ‘s’) { for (int i = 0; i < num_clicks; i++) { Points[i].y -= 10; } } if (key == ‘a’) { for (int i = 0; i < num_clicks; i++) { Points[i].x -= 10; } } if (key == ‘d’) { for (int i = 0; i < num_clicks; i++) { Points[i].x += 10; } } //Add the code for Rotation and Scale keys glutPostRedisplay(); } void myReshape(int newWidth, int newHeight) { glutPostRedisplay(); } void myMouse(int button, int state, int x, int y) { y = screenHeight – y; if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && num_clicks < 3) { Points[num_clicks] = Point2D(x, y); num_clicks++; } if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) num_clicks = 0; glutPostRedisplay(); } //<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>> int main(int argc, char** argv) { glutInit(&argc, argv); // initialize the toolkit glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_MULTISAMPLE); // set display mode glutInitWindowSize(screenWidth, screenHeight); // set window size glutInitWindowPosition(100, 100); // set window position on screen glutCreateWindow(“My Graphics App”); // open the screen window glutDisplayFunc(myDisplay); // register redraw function glutKeyboardFunc(myKeyboard); glutReshapeFunc(myReshape); //register the reshape function glutMouseFunc(myMouse); myInit(); glutMainLoop(); // go into a perpetual loop return 0; }