1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include "stdafx.h"
struct point { int x; int y; bool polygonStart; }; std::vector<point> points; bool newPolygon = true; bool deleteMode = false; bool moveMode = false;
bool dragging = false;
int draggedNum = -1;
void RenderSence() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1, 1, 1); for (int i = 0; i < points.size(); i++) { if (points[i].polygonStart) { if (i != 0) { glEnd(); } glBegin(GL_LINE_STRIP); } glVertex2f(points[i].x, 600 - points[i].y); } glEnd(); glutSwapBuffers(); }
int findNearPointNum(int x, int y) { if (points.size() == 0) { return -1; } int near = 0; double mix = -1; for (int i = 0; i < points.size(); i++) { double distence = (points[i].x - x)*(points[i].x - x) + (points[i].y - y)*(points[i].y - y); if (mix == -1) { mix = distence; } else if (mix > distence) { mix = distence; near = i; } } return near; }
void processMouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON&&state == GLUT_DOWN) { if (deleteMode) { int near = findNearPointNum(x, y); if (near == -1) { return; } if (points[near].polygonStart) { if (points.size() > near + 1) { points[near + 1].polygonStart = true; } } points.erase(points.begin() + near, points.begin() + near + 1); return; } if (moveMode) { dragging = true; draggedNum = findNearPointNum(x, y); return; } point t{ x,y,false }; if (newPolygon) { newPolygon = false; t.polygonStart = true; } points.push_back(t); } else if (button == GLUT_LEFT_BUTTON&&state == GLUT_UP) { if (!dragging) { return; } dragging = false; if (draggedNum != -1) { points[draggedNum].x = x; points[draggedNum].y = y; } } }
void processKey(unsigned char key, int x, int y) { if (key == 'b') { newPolygon = true; } if (key == 'd') { deleteMode = true; } if (key == 'm') { moveMode = true; } if (key == 'r') { points.clear(); } if (key == 'q') { exit(0); } }
int main(int argc, char* argv[]) { glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(1000, 600); glutInitWindowPosition(100, 100); glutCreateWindow("Hello GLUT");
glutDisplayFunc(RenderSence); glutIdleFunc(RenderSence);
gluOrtho2D(0, 1000, 0, 600);
glutMouseFunc(processMouse); glutKeyboardFunc(processKey);
glutMainLoop();
return 0; }
|