Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <stdio.h>
  6. #include <vector>
  7. #include <GLFW/glfw3.h>
  8. #include <cmath>
  9. #include <math.h>
  10. #include <map>
  11.  
  12. struct vertex{
  13. double x, y;
  14. };
  15.  
  16. struct pair{
  17. struct vertex * arr[2];
  18. };
  19.  
  20. struct line{
  21. struct pair * point = NULL;
  22. std::vector<pair*> L;
  23. } lines;
  24.  
  25. static double px, py =0;
  26. static int a=1080;
  27. static int b=1080;
  28. static bool mod= false, make_shape = true;
  29. static std::vector<vertex*> shape;
  30. static std::vector<pair*> res;
  31.  
  32. void build();
  33. void game_loop_drow();
  34. void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos);
  35. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
  36. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
  37.  
  38. int main(int argc, const char *argv[]){
  39. if (!glfwInit()){
  40. std::cout << "faild\n";
  41. return 0;
  42. }
  43.  
  44. GLFWwindow* window = glfwCreateWindow(a, b, "lol", NULL, NULL);
  45.  
  46. if (!window){
  47. std::cout<< "faild\n";
  48. return 0;
  49. }
  50.  
  51. glfwMakeContextCurrent(window);
  52. glClearColor(1, 1, 1, 0.0f);
  53. glfwSetCursorPosCallback(window, cursor_pos_callback);
  54. glfwSetKeyCallback(window, key_callback);
  55. glfwSetMouseButtonCallback(window, mouse_button_callback);
  56. glEnable(GL_DEPTH_TEST);
  57.  
  58. while (!glfwWindowShouldClose(window)){
  59. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  60. game_loop_drow();
  61. glfwSwapBuffers(window);
  62. glfwWaitEvents();
  63. }
  64.  
  65. glfwDestroyWindow(window);
  66. glfwTerminate();
  67.  
  68. return 0;
  69.  
  70. }
  71.  
  72. void game_loop_drow(){
  73. glLineWidth(2.0);
  74.  
  75. glColor3f(0, 1, 0);
  76. glBegin(GL_LINE_LOOP);
  77. for (std::vector<vertex*>::iterator point = shape.begin(); point!= shape.end(); ++point)
  78. glVertex3f((*point)->x, (*point)->y, 0);
  79. glEnd();
  80.  
  81. if (!mod) {
  82.  
  83. glColor3f(1, 0, 1);
  84. for (std::vector<pair*>::iterator point = lines.L.begin(); point!= lines.L.end(); ++point){
  85. glBegin(GL_LINES);
  86. for (int i =0; i<2 ;i++)
  87. glVertex3f((*point)->arr[i]->x, (*point)->arr[i]->y, 0);
  88. glEnd();
  89. }
  90.  
  91. if (lines.point != NULL){
  92. glBegin(GL_LINES);
  93. glVertex3f(lines.point->arr[0]->x, lines.point->arr[0]->y, 0);
  94. glVertex3f(px, py, 0);
  95. glEnd();
  96. }
  97. return;
  98. } else {
  99. glColor3f(0, 0, 0);
  100. for (std::vector<pair*>::iterator point = res.begin(); point!= res.end(); ++point){
  101. glBegin(GL_LINES);
  102. for (int i =0; i<2 ;i++)
  103. glVertex3f((*point)->arr[i]->x, (*point)->arr[i]->y, 0);
  104. glEnd();
  105. }
  106.  
  107. }
  108. }
  109.  
  110. void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos){
  111. px= xpos/(a/2)-1;
  112. py= 1-(ypos+50)/(a/2);
  113. return;
  114. }
  115.  
  116. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
  117. // std::cout << key <<std::endl;
  118. if( action == 1) return;
  119. switch (key ){
  120. case 66: // Build
  121. mod ^= true;
  122. if (mod) build();
  123. break;
  124. case 78: // Next
  125. make_shape ^= true;
  126. break;
  127. case GLFW_KEY_ESCAPE:
  128. if (glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED){
  129. glfwSetWindowShouldClose(window, true);
  130. }
  131. break;
  132. default:
  133. break;
  134. }
  135. return;
  136. }
  137.  
  138. double scalar_product(vertex A, vertex B){
  139. return A.x*B.x + A.y*B.y;
  140. }
  141.  
  142. void build(){
  143. shape.push_back(shape[0]);
  144. for (std::vector<pair*>::iterator i = lines.L.begin(); i!= lines.L.end(); ++i){
  145. bool end = false;
  146. pair *cur_line = *i;
  147. double t_start = 0, t_final = 1;
  148. vertex D {cur_line->arr[1]->x - cur_line->arr[0]->x, cur_line->arr[1]->y - cur_line->arr[0]->y};
  149.  
  150. for (std::vector<vertex*>::iterator i= shape.begin(); i!=shape.end()-1; ++i){
  151. pair cur_edge;
  152. cur_edge.arr[0] = *i;
  153. cur_edge.arr[1] = *(i+1);
  154.  
  155.  
  156. vertex edge_D {cur_edge.arr[1]->x - cur_edge.arr[0]->x, cur_edge.arr[1]->y - cur_edge.arr[0]->y};
  157. vertex W {cur_line->arr[0]->x - cur_edge.arr[0]->x, cur_line->arr[0]->y - cur_edge.arr[0]->y};
  158. vertex N {edge_D.y, -edge_D.x};
  159.  
  160. double Ws = scalar_product(W,N), Ds = scalar_product(D, N);
  161. std::cout << Ds << " " << Ws << std::endl;
  162.  
  163. double t =-Ws/Ds;
  164. if (Ds == 0){
  165. if (Ws < 0){
  166. end = true;
  167. break;
  168. } else{
  169. continue;
  170. }
  171. } else if (Ds < 0){
  172. if (t<=1)
  173. t_start = std::max(t,t_start);
  174. else
  175. end = true;
  176. } else{
  177. if (t>=0)
  178. t_final = std::min(t, t_final);
  179. else
  180. end = true;
  181. }
  182.  
  183. }
  184. if (end || t_start>t_final) {
  185. continue;
  186. }
  187. pair *pair_t =new pair;
  188. pair_t->arr[0] = new vertex{ cur_line->arr[0]->x + t_start*(cur_line->arr[1]->x - cur_line->arr[0]->x), cur_line->arr[0]->y + t_start*(cur_line->arr[1]->y - cur_line->arr[0]->y) };
  189. pair_t->arr[1] = new vertex{ cur_line->arr[0]->x + t_final*(cur_line->arr[1]->x - cur_line->arr[0]->x), cur_line->arr[0]->y + t_final*(cur_line->arr[1]->y - cur_line->arr[0]->y) };
  190. res.push_back(pair_t);
  191. }
  192. shape.pop_back();
  193. return;
  194. }
  195.  
  196. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
  197. if (action==0 && mods==0){
  198. vertex *V = new vertex{px, py};
  199. if (make_shape){
  200. shape.push_back(V);
  201. } else {
  202. if (lines.point != NULL){
  203. lines.point->arr[1] = V;
  204. lines.L.push_back(lines.point);
  205. lines.point = NULL;
  206. } else{
  207.  
  208. lines.point = new pair;
  209. lines.point->arr[0] = V;
  210. }
  211. }
  212. }
  213. return;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement