Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. #include <GLFW/glfw3.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. GLfloat scrW = 1280, scrH = 960;
  9.  
  10.  
  11.  
  12. struct vertex {
  13. GLfloat x, y;
  14. };
  15.  
  16. struct vertexFlag {
  17. int number;
  18. bool flag;
  19. };
  20.  
  21. struct vertexInfo {
  22. int nubmerMain;
  23. vertexFlag prev, next;
  24. GLfloat dprev, dnext;
  25. };
  26.  
  27. vector<vertex> mas;
  28.  
  29. int flag = 0;
  30.  
  31. GLfloat centerScreenW, centerScreenH;
  32.  
  33. //вспомогательная функция сортировки по X
  34. bool compareByX(const GLfloat &a, const GLfloat &b) {
  35. return a < b;
  36. }
  37.  
  38. void drawhui(){
  39. vector<vertexInfo> arrayCurrentVertex;
  40. for (GLfloat currentY = 0; currentY < scrH; currentY++) {
  41.  
  42. for (int i = 0; i < mas.size(); i++) {
  43. if (mas[i].y == currentY) {
  44. vertexInfo main;
  45. main.nubmerMain = i;
  46. vertexFlag prev, next;
  47. if (i != 0) {
  48. prev.number = i - 1;
  49. } else {
  50. prev.number = mas.size() - 1;
  51. }
  52. if (i != mas.size() - 1) {
  53. next.number = i + 1;
  54. } else {
  55. next.number = 0;
  56. }
  57.  
  58. if (currentY >= max(mas[i].y, mas[prev.number].y)) {
  59. prev.flag = true;
  60. for (int q = 0; q < arrayCurrentVertex.size(); q++) {
  61. if (arrayCurrentVertex[q].nubmerMain == prev.number) {
  62. arrayCurrentVertex[q].next.flag = true;
  63. }
  64. }
  65. } else {
  66. prev.flag = false;
  67. }
  68.  
  69. if (currentY >= max(mas[i].y, mas[next.number].y)) {
  70. next.flag = true;
  71. for (int q = 0; q < arrayCurrentVertex.size(); q++) {
  72. if (arrayCurrentVertex[q].nubmerMain == next.number) {
  73. arrayCurrentVertex[q].prev.flag = true;
  74. }
  75. }
  76. } else {
  77. next.flag = false;
  78. }
  79.  
  80. main.prev = prev;
  81. main.next = next;
  82.  
  83. main.dprev = (mas[prev.number].x - mas[i].x)/abs(mas[prev.number].y - mas[i].y);
  84. main.dnext = (mas[next.number].x - mas[i].x)/abs(mas[next.number].y - mas[i].y);
  85.  
  86. arrayCurrentVertex.push_back(main);
  87. }
  88. }
  89.  
  90. cout << "-------------------------XYI---------------------------" << endl;
  91. cout << endl;
  92. cout << "currentY: " << currentY << endl;
  93. // for (int i = 0; i < arrayCurrentVertex.size(); i++) {
  94. // cout << "Number: " << arrayCurrentVertex[i].nubmerMain << endl;
  95. // cout << "Prev: " << arrayCurrentVertex[i].prev.number << " flag: " << arrayCurrentVertex[i].prev.flag << endl;
  96. // cout << "Next: " << arrayCurrentVertex[i].next.number << " flag: " << arrayCurrentVertex[i].next.flag << endl;
  97. // cout << "dprev: " << arrayCurrentVertex[i].dprev << " dnext: " << arrayCurrentVertex[i].dnext << endl;
  98. // cout << endl;
  99. //
  100. // }
  101.  
  102. if (!arrayCurrentVertex.empty()) {
  103. bool flagExit = true;
  104.  
  105. for (int i = 0; i < arrayCurrentVertex.size(); i++) {
  106. if (arrayCurrentVertex[i].prev.flag == false) {
  107. flagExit = false;
  108. }
  109. if (arrayCurrentVertex[i].next.flag == false) {
  110. flagExit = false;
  111. }
  112. }
  113.  
  114. if (flagExit) {
  115. return;
  116. }
  117. }
  118.  
  119.  
  120. if (!arrayCurrentVertex.empty()) {
  121. vector<GLfloat> currentVertexOnX;
  122. for (int i = 0; i < arrayCurrentVertex.size(); i++) {
  123.  
  124. for (int g = 0; g < arrayCurrentVertex.size(); g++) {
  125. if (arrayCurrentVertex[g].prev.flag == false) {
  126. GLfloat addx = mas[arrayCurrentVertex[g].nubmerMain].x +
  127. arrayCurrentVertex[g].dprev *
  128. abs(currentY - mas[arrayCurrentVertex[g].nubmerMain].y);
  129. currentVertexOnX.push_back(addx);
  130. }
  131. if (arrayCurrentVertex[g].next.flag == false) {
  132. GLfloat addx = mas[arrayCurrentVertex[g].nubmerMain].x +
  133. arrayCurrentVertex[g].dnext *
  134. abs(currentY - mas[arrayCurrentVertex[g].nubmerMain].y);
  135. currentVertexOnX.push_back(addx);
  136. }
  137.  
  138. }
  139. }
  140.  
  141. sort(currentVertexOnX.begin(), currentVertexOnX.end(), compareByX);
  142. vector<GLfloat> currentVertexOnXHelp;
  143.  
  144.  
  145. for (int i = 0; i < currentVertexOnX.size() - 1; i++) {
  146. if (currentVertexOnX[i] != currentVertexOnX[i + 1]) {
  147. currentVertexOnXHelp.push_back(currentVertexOnX[i]);
  148. } else {
  149. i++;
  150. }
  151.  
  152. }
  153. if (currentVertexOnX[currentVertexOnX.size()-1] != currentVertexOnX[currentVertexOnX.size()-2]) {
  154. currentVertexOnXHelp.push_back(currentVertexOnX[currentVertexOnX.size()-1]);
  155. }
  156. currentVertexOnX.clear();
  157. currentVertexOnX = currentVertexOnXHelp;
  158. currentVertexOnXHelp.clear();
  159.  
  160. cout << "currentVertexOnX: " << endl;
  161.  
  162. for (int p = 0; p < currentVertexOnX.size(); p++) {
  163. cout << currentVertexOnX[p];
  164. cout << " ";
  165. }
  166.  
  167. cout << endl;
  168.  
  169. if (!currentVertexOnX.empty()) {
  170. bool flagDraw = false;
  171. int counter = 0;
  172. for (GLfloat currentX = 0; currentX < scrW; currentX++) {
  173. if (currentX >= currentVertexOnX[counter]) {
  174. flagDraw = !flagDraw;
  175. counter++;
  176. }
  177. if (flagDraw) {
  178. cout << "x: " << currentX << " ";
  179. glColor3f(1.0, 1.0, 1.0);
  180. glBegin(GL_POINTS);
  181. glVertex2f(currentX, currentY);
  182. glEnd();
  183. }
  184. // if (!flagDraw) {
  185. // cout << "NET x: " << currentX << "NET y: " << currentY << " ";
  186. // }
  187. }
  188. }
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. currentVertexOnX.clear();
  197. }
  198.  
  199.  
  200.  
  201. }
  202.  
  203.  
  204.  
  205.  
  206. }
  207.  
  208.  
  209. void drawEdges() {
  210. if (flag) {
  211. glBegin(GL_LINE_LOOP);
  212. } else {
  213. glBegin(GL_LINE_STRIP);
  214. }
  215. glColor3f(1, 1, 1);
  216. for (int i = 0; i < mas.size(); i++) {
  217. glVertex2f(mas[i].x, mas[i].y);
  218. }
  219.  
  220. if (flag){
  221. drawhui();
  222. }
  223. }
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230. void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
  231. if (action == GLFW_PRESS) {
  232. switch (key) {
  233. case GLFW_KEY_1:
  234. flag = 1;
  235. break;
  236. case GLFW_KEY_C:
  237. mas.clear();
  238. flag = 0;
  239. break;
  240. }
  241. }
  242. }
  243.  
  244. void mouseCallback(GLFWwindow *window, int button, int action, int mods) {
  245. if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
  246. double x, y;
  247. glfwGetCursorPos(window, &x, &y);
  248. vertex vertexLeftBut;
  249. vertexLeftBut.x = x;
  250. vertexLeftBut.y = y;
  251. mas.push_back(vertexLeftBut);
  252. }
  253. }
  254.  
  255. int main() {
  256. GLFWwindow *window;
  257.  
  258.  
  259.  
  260. if (!glfwInit()) {
  261. return -1;
  262. }
  263.  
  264. window = glfwCreateWindow(scrW, scrH, "Lab4", NULL, NULL);
  265.  
  266. glfwSetKeyCallback(window, keyCallback);
  267. glfwSetMouseButtonCallback(window, mouseCallback);
  268.  
  269. glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
  270.  
  271. int screenW, screenH;
  272. glfwGetFramebufferSize(window, &screenW, &screenH);
  273.  
  274. if (!window) {
  275. glfwTerminate();
  276. return -1;
  277. }
  278.  
  279. glfwMakeContextCurrent(window);
  280. glMatrixMode(GL_PROJECTION);
  281. glLoadIdentity();
  282. glMatrixMode(GL_MODELVIEW);
  283. glLoadIdentity();
  284.  
  285. centerScreenW = scrW / 2;
  286. centerScreenH = scrH / 2;
  287.  
  288. while (!glfwWindowShouldClose(window)) {
  289.  
  290. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  291. glPushMatrix();
  292. glOrtho(0.0, scrW, scrH, 0.0, -1.0, 1.0);
  293. glViewport(0, 0, screenW, screenH);
  294.  
  295. drawEdges();
  296.  
  297. glEnd();
  298. glPopMatrix();
  299.  
  300.  
  301. glfwSwapBuffers(window);
  302. glfwPollEvents();
  303. }
  304.  
  305. glfwTerminate();
  306.  
  307. return 0;
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement