Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 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. bool oriantation(std::vector<vertex*> a){
  143. double S=0;
  144. a.push_back(a[0]);
  145. for (std::vector<vertex*>::iterator i = a.begin(); i!=a.end()-1; ++i){
  146. vertex *a = *i, *b = *(i+1);
  147. S+= a->y*b->x - a->x*b->y;
  148. }
  149. a.pop_back();
  150. return S>0;
  151. }
  152.  
  153.  
  154. void build(){
  155.  
  156. if (oriantation(shape)){
  157. std::reverse(shape.begin(), shape.end());
  158. }
  159.  
  160. shape.push_back(shape[0]);
  161. for (std::vector<pair*>::iterator i = lines.L.begin(); i!= lines.L.end(); ++i){
  162. bool end = false;
  163. pair *cur_line = *i;
  164. double t_start = 0, t_final = 1;
  165. vertex D {cur_line->arr[1]->x - cur_line->arr[0]->x, cur_line->arr[1]->y - cur_line->arr[0]->y};
  166.  
  167. for (std::vector<vertex*>::iterator i= shape.begin(); i!=shape.end()-1; ++i){
  168. pair cur_edge;
  169. cur_edge.arr[0] = *i;
  170. cur_edge.arr[1] = *(i+1);
  171.  
  172.  
  173. vertex edge_D {cur_edge.arr[1]->x - cur_edge.arr[0]->x, cur_edge.arr[1]->y - cur_edge.arr[0]->y};
  174. vertex W {cur_line->arr[0]->x - cur_edge.arr[0]->x, cur_line->arr[0]->y - cur_edge.arr[0]->y};
  175. vertex N {edge_D.y, -edge_D.x};
  176.  
  177. double Ws = scalar_product(W,N), Ds = scalar_product(D, N);
  178. std::cout << Ds << " " << Ws << std::endl;
  179.  
  180. double t =-Ws/Ds;
  181. if (Ds == 0){
  182. if (Ws < 0){
  183. end = true;
  184. break;
  185. } else{
  186. continue;
  187. }
  188. } else if (Ds < 0){
  189. if (t<=1)
  190. t_start = std::max(t,t_start);
  191. else
  192. end = true;
  193. } else{
  194. if (t>=0)
  195. t_final = std::min(t, t_final);
  196. else
  197. end = true;
  198. }
  199.  
  200. }
  201. if (end || t_start>t_final) {
  202. continue;
  203. }
  204. pair *pair_t =new pair;
  205. 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) };
  206. 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) };
  207. res.push_back(pair_t);
  208. }
  209. shape.pop_back();
  210. return;
  211. }
  212.  
  213. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
  214. if (action==0 && mods==0){
  215. vertex *V = new vertex{px, py};
  216. if (make_shape){
  217. shape.push_back(V);
  218. } else {
  219. if (lines.point != NULL){
  220. lines.point->arr[1] = V;
  221. lines.L.push_back(lines.point);
  222. lines.point = NULL;
  223. } else{
  224.  
  225. lines.point = new pair;
  226. lines.point->arr[0] = V;
  227. }
  228. }
  229. }
  230. return;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement