#include #include template struct ListElement { T value; ListElement *next; ListElement(T val){ value = val; next = NULL; } }; template struct List { ListElement *first; ListElement *current; int length = 0; List(ListElement *f = NULL){ first = f; current = NULL; } List *push(T val){ if(first == NULL){ first = new ListElement(val); current = first; } else { ListElement *curr = first; while(curr->next) curr = curr->next; curr->next = new ListElement(val); } length++; return this; } List *next(){ current = current->next; return this; } bool nextExists(){ return current->next != NULL; } }; //int pos0X = 100, pos0Y = 300, repeat = 5; struct Dot { int x, y; Dot(int _x, int _y){ x = _x; y = _y; } }; List *dots = new List(); void display(){ //if(dots->length < 2 || dots->length % 2 != 0) return; glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glColor3f(1.0, 1.0, 1.0); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); if(dots->length > 2 && dots->length % 2 == 0){ printf("Draw!!!\n"); glBegin(GL_POLYGON); ListElement *curr = dots->first; Dot *d1, *d2; glVertex2f(curr->value->x, curr->value->y); curr = curr->next; glVertex2f(curr->value->x, curr->value->y); curr = curr->next; while(curr){ d1 = curr->value; curr = curr->next; d2 = curr->value; curr = curr->next; glVertex2f(d1->x, d1->y); glVertex2f(d2->x, d2->y); glEnd(); glBegin(GL_POLYGON); glVertex2f(d1->x, d1->y); glVertex2f(d2->x, d2->y); } glEnd(); } glPopMatrix(); glutSwapBuffers(); } void onClick(int button, int state, int x, int y){ if(state != 0 || button != 0) return; dots->push(new Dot(x, y)); printf("click: %d %d %d\n", x, y, dots->length); if(dots->length > 2 && dots->length % 2 == 0) display(); } void init(){ glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_FLAT); } void reshape(int w, int h){ glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, 800, 600, 0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(800, 600); glutInitWindowPosition(100, 100); glutCreateWindow("Lab 3 :: Task 2"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(onClick); glutMainLoop(); return 0; }