Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.33 KB | None | 0 0
  1. #include<GL/glut.h>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<map>
  5. #include<vector>
  6. #include<iostream>
  7. #include<algorithm>
  8. using namespace std;
  9.  
  10. typedef struct
  11. {
  12. int h,k;
  13. int selected;
  14. } data;
  15.  
  16.  
  17. typedef struct
  18. {
  19.  
  20. int downX,downY,upX,upY;
  21. int buttontype;
  22.  
  23. } SQBUTTONS;
  24. #define MARGIN 140
  25. #define VERTEX_MODE_INSIDE_AREA atX>MARGIN && vertex_mode
  26. #define EDGE_MODE_INSIDE_AREA atX>MARGIN && edge_mode
  27. #define BUTTON_AREA atX<MARGIN
  28.  
  29. #define VERTEX_BUTTON 1
  30. #define EDGE_BUTTON 2
  31. #define ALGOWIN_BUTTON 3
  32. #define PANEL_MATRIX_BUTTON 1
  33.  
  34. #define COLOR_RED glColor3f(255.0f, 0.0f, 0.0f);
  35. #define COLOR_BLACK glColor3f(0.0f, 0.0f, 0.0f);
  36. #define COLOR_WHITE glColor3f(1.0f, 1.0f, 1.0f);
  37. #define COLOR_YELLOW glColor3f(255.0f, 110.0f, 0.0f);
  38. vector<data>centers;
  39. vector<int>edges[100];
  40. vector<SQBUTTONS>v_buttons;
  41. vector<SQBUTTONS>v_panelbuttons;
  42. map<int,pair<int,int> >co_ordinate_map;
  43. int adj_mat[100][100];
  44.  
  45. int WIDTH = 1000;
  46. int HEIGHT = 800;
  47. int width = WIDTH;
  48. int height = HEIGHT;
  49.  
  50. //////
  51.  
  52. int nodes=0;
  53. int vertex_mode=1;
  54. int edge_mode=0;
  55. int algo_panelmode=0;
  56.  
  57. int rad=6;
  58. int connection_V1;
  59. int connection_V2;
  60. int atX,atY;
  61. int selected_node;
  62. //////
  63.  
  64. /////////
  65. bool incircle(int x,int y);
  66. void draw_circle(int H,int K,int r);
  67. int inbutton(int x,int y,int where_i_clicked);
  68. void draw_button(void);
  69. void DATAENTRY();
  70. void draw_text(int tx,int ty,string s,void *font);
  71. void drawpanel();
  72. /////////
  73.  
  74.  
  75.  
  76. void display ()
  77. {
  78. // printf("calling display(%d)\n",++noOfDisplayCalls);
  79. // Function that clears the window. If you don't want to clear the view window, don't call glClear function. make it as a comment statement.
  80. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  81.  
  82. // --------------
  83. // YOUR CODE ...
  84. // --------------
  85.  
  86. COLOR_RED
  87. glLineWidth(5);
  88. glBegin(GL_LINE_STRIP);
  89. glVertex2f(120,0);
  90. glVertex2f(120,HEIGHT);
  91. glEnd();
  92.  
  93.  
  94. ////button
  95. draw_button();
  96. ////////
  97.  
  98.  
  99. glLineWidth(1);
  100.  
  101. vector<data>::iterator it;
  102. vector<int>::iterator Lit;
  103. int nodenumbers=1;
  104.  
  105. for(int i=1;i<=nodes;i++)
  106. {
  107.  
  108. double x=co_ordinate_map[i].first;
  109. double y=co_ordinate_map[i].second;
  110. for(Lit=edges[i].begin();Lit<edges[i].end();Lit++)
  111. {
  112.  
  113. double x1=co_ordinate_map[*Lit].first;
  114. double y1=co_ordinate_map[*Lit].second;
  115.  
  116. COLOR_WHITE
  117. glBegin(GL_LINE_STRIP);
  118. glVertex2f(x,y);
  119. glVertex2f(x1,y1);
  120. glEnd();
  121.  
  122. }
  123. }
  124.  
  125.  
  126. for(it=centers.begin();it<centers.end();it++)
  127. {
  128. if((*it).selected==0)
  129. glColor3f(3.5f, 2.5f, 1.5f);
  130. if((*it).selected==1)
  131. COLOR_RED
  132. if((*it).selected==2)
  133. COLOR_YELLOW
  134. draw_circle((*it).h,(*it).k,rad);
  135. char temp[10];
  136. sprintf(temp,"%d",nodenumbers);
  137. COLOR_RED
  138.  
  139. void * font = GLUT_BITMAP_HELVETICA_10;
  140. draw_text((*it).h-10,(*it).k-17,temp,font);
  141. nodenumbers++;
  142. }
  143.  
  144. if(algo_panelmode)
  145. {
  146.  
  147. drawpanel();
  148.  
  149. }
  150.  
  151. glutSwapBuffers();
  152. }
  153.  
  154.  
  155. void mouseMovement(int mx, int my)
  156. {
  157.  
  158. atX=mx;
  159. atY=my;
  160. atY=height-atY;
  161. }
  162.  
  163.  
  164. void mouseAction(int button, int state, int x, int z)
  165. // Record cursor position in response to mouse button action
  166. {
  167. switch(button)
  168. {
  169. case GLUT_LEFT_BUTTON:
  170. if (state == GLUT_DOWN)
  171. {
  172. if(VERTEX_MODE_INSIDE_AREA )
  173. {
  174.  
  175. int too_close=0;
  176. vector<data>::iterator it;
  177. for(it=centers.begin();it<centers.end();it++)
  178. {
  179. int H=(*it).h;
  180. int K=(*it).k;
  181. int dist=(atX-H)*(atX-H)+(atY-K)*(atY-K);
  182.  
  183. if(dist<2*4*rad*rad){too_close=1; break;}
  184.  
  185. }
  186. if(!too_close)
  187. {
  188. data temp;
  189. temp.h=atX;
  190. temp.k=atY;
  191. printf("(%d %d)\n",atX,atY);
  192. temp.selected=0;
  193. centers.push_back(temp);
  194. nodes++;
  195. co_ordinate_map[nodes].first=atX;
  196. co_ordinate_map[nodes].second=atY;
  197. }
  198.  
  199. glutPostRedisplay();
  200. }
  201.  
  202. if(EDGE_MODE_INSIDE_AREA )
  203. {
  204. int click_on_node=0,a_node_before=0,new_edge_now=0;
  205. if(selected_node) a_node_before=selected_node;
  206. if(selected_node)new_edge_now=1;
  207.  
  208. click_on_node=incircle(atX,atY);
  209.  
  210. if(!new_edge_now) connection_V1=selected_node;
  211.  
  212. if(!click_on_node)a_node_before=0;
  213.  
  214. if(!click_on_node)puts("RESET");
  215. else if(new_edge_now)
  216. {
  217.  
  218. connection_V2=selected_node;
  219. edges[connection_V1].push_back(connection_V2);
  220. adj_mat[connection_V1][connection_V2]=1;
  221. printf(">%d - %d now have a connection\n",connection_V1,connection_V2);
  222.  
  223. }
  224.  
  225. glutPostRedisplay();
  226. }
  227. if(algo_panelmode)
  228. {
  229.  
  230. puts("PANEL MODE");
  231. int onbutton=inbutton(atX,atY,2);
  232. if(onbutton==PANEL_MATRIX_BUTTON)
  233. {
  234. for(int i=1;i<=nodes;i++)
  235. {
  236. for(int j=1;j<=nodes;j++)
  237. printf("%d",adj_mat[i][j]);
  238. puts("");
  239. }
  240. }
  241.  
  242.  
  243. }
  244. if(BUTTON_AREA)
  245. {
  246. int onbutton=inbutton(atX,atY,1);
  247.  
  248. if(onbutton==VERTEX_BUTTON)
  249. {
  250. vertex_mode=1;
  251. edge_mode=0;
  252. algo_panelmode=0;
  253. glutPostRedisplay();
  254. }
  255. else if(onbutton==EDGE_BUTTON)
  256. {
  257. vertex_mode=0;
  258. edge_mode=1;
  259. algo_panelmode=0;
  260. glutPostRedisplay();
  261.  
  262. }
  263. else if(onbutton==ALGOWIN_BUTTON)
  264. {
  265.  
  266. vertex_mode=0;
  267. edge_mode=0;
  268. algo_panelmode=1;
  269. glutPostRedisplay();
  270. }
  271. }
  272. }
  273.  
  274. break;
  275. }
  276. } // end mouse
  277.  
  278. // Respond to window resizing, preserving proportions.
  279. // Parameters give new window size in pixels.
  280. void reshapeMainWindow (int newWidth, int newHeight)
  281. {
  282.  
  283. // ----------------------------
  284. // DO NOT ALTER THIS FUNCTION ...
  285. // ----------------------------
  286.  
  287. glMatrixMode( GL_PROJECTION );
  288. glLoadIdentity();
  289. glOrtho( 0, newWidth, 0, newHeight, 0, 1 );
  290. glViewport( 0, 0, newWidth, newHeight );
  291. HEIGHT = newWidth;
  292. WIDTH = newHeight;
  293. glMatrixMode(GL_MODELVIEW);
  294. glutPostRedisplay();
  295. }
  296. // Respond to function keys.
  297. // Parameters give key code and mouse coordinates.
  298. int main(int argc, char *argv[])
  299. {
  300. DATAENTRY();
  301. // GLUT initialization.
  302. glutInit(&argc, argv);
  303. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  304. glutInitWindowSize(width, height);
  305. glutCreateWindow("GLUT Skeleton Program");
  306.  
  307. // Register call backs.
  308. glutDisplayFunc(display);
  309. glutReshapeFunc(reshapeMainWindow);
  310.  
  311. glutPassiveMotionFunc(mouseMovement);
  312. glutMouseFunc(mouseAction);
  313.  
  314.  
  315.  
  316. // Enter GLUT loop.
  317. glutMainLoop();
  318. return 0;
  319. }
  320.  
  321.  
  322.  
  323. bool incircle(int x,int y)
  324. {
  325. int H,K;
  326.  
  327. int dist,flag=0,count=0;
  328. vector<data>::iterator it;
  329.  
  330. for(it=centers.begin();it<centers.end();it++)
  331. {
  332. if((*it).selected==1 && !selected_node )
  333. (*it).selected=0;
  334.  
  335.  
  336. count++;
  337. H=(*it).h;
  338. K=(*it).k;
  339. dist=(H-x)*(H-x)+(K-y)*(K-y);
  340. if(dist<=rad*rad)
  341. {
  342.  
  343. if(selected_node && !(*it).selected) (*it).selected=2;
  344. else (*it).selected=1;
  345. selected_node=count;
  346. flag=1;
  347. }
  348.  
  349. }
  350.  
  351. if(!flag)
  352. {
  353. for(it=centers.begin();it<centers.end();it++)
  354. (*it).selected=0;
  355. selected_node=0;
  356. }
  357.  
  358. return flag;
  359. }
  360.  
  361. int inbutton(int x,int y,int where_i_clicked)
  362. {
  363. if(where_i_clicked==1)
  364. {
  365. int H,K,flag=0;
  366. H=52;
  367. K=90;
  368. int dist=(H-x)*(H-x)+(K-y)*(K-y);
  369. if(dist<=5*5)
  370. flag=VERTEX_BUTTON;
  371.  
  372. vector<SQBUTTONS>::iterator it;
  373. for(it=v_buttons.begin();it<v_buttons.end();it++)
  374. {
  375.  
  376. if(x<=(*it).upX && x>=(*it).downX && y<=(*it).upY && y>=(*it).downY){flag=(*it).buttontype;break;}
  377. }
  378.  
  379. return flag;
  380.  
  381. }
  382. else
  383. {
  384. int flag=0;
  385. vector<SQBUTTONS>::iterator it;
  386. for(it=v_panelbuttons.begin();it<v_panelbuttons.end();it++)
  387. if(x<=(*it).upX && x>=(*it).downX && y<=(*it).upY && y>=(*it).downY){flag=(*it).buttontype;break;}
  388.  
  389.  
  390.  
  391. return flag;
  392.  
  393.  
  394. }
  395.  
  396. }
  397.  
  398. void draw_circle(int H,int K,int r)
  399. {
  400. glBegin(GL_LINE_LOOP);
  401. for(int rr=1;rr<=r;rr++)
  402. for (int angle= 0; angle<=360; angle+=1)
  403. glVertex2f(H+ sin(angle) * rr, K + cos(angle) * rr);
  404. glEnd();
  405. }
  406.  
  407. void draw_button(void)
  408. {
  409. glLineWidth(2);
  410. glBegin(GL_LINE_STRIP);
  411. glVertex2f(95,150);
  412. glVertex2f(95,110);
  413. glVertex2f(20,110);
  414. glVertex2f(20,150);
  415. glVertex2f(95,150);
  416. glEnd();
  417.  
  418.  
  419. string s = "Algo panel";
  420. void * font = GLUT_BITMAP_HELVETICA_12;
  421. draw_text(25,130,s,font);
  422.  
  423. if(edge_mode || algo_panelmode)
  424. COLOR_WHITE
  425. else
  426. COLOR_RED
  427.  
  428.  
  429. draw_circle(52,90,5);
  430.  
  431. glLineWidth(1);
  432.  
  433. if(edge_mode)
  434. glColor3f(255.0f, 0.0f, 0.0f);
  435. else
  436. glColor3f(1.0f, 1.0f, 1.0f);
  437.  
  438. if(algo_panelmode)
  439. glColor3f(1.0f, 1.0f, 1.0f);
  440. glBegin(GL_LINES);
  441.  
  442. for(int i=65;i<=70;i++)
  443. {
  444. glVertex2f(72,i);
  445. glVertex2f(32,i);
  446. }
  447. glEnd();
  448.  
  449.  
  450.  
  451.  
  452. }
  453.  
  454. void draw_text(int tx,int ty,string s,void *font)
  455. {
  456. glRasterPos2i(tx,ty);
  457.  
  458. for (string::iterator i = s.begin(); i != s.end(); ++i)
  459. {
  460. char c = *i;
  461. glutBitmapCharacter(font, c);
  462. }
  463.  
  464.  
  465. }
  466.  
  467.  
  468. void drawpanel()
  469. {
  470. COLOR_WHITE
  471.  
  472.  
  473. glBegin(GL_QUADS);
  474. glVertex2f(MARGIN,HEIGHT);
  475. glVertex2f(MARGIN,400);
  476. glVertex2f(MARGIN+850,400);
  477. glVertex2f(MARGIN+850,HEIGHT);
  478. glEnd();
  479.  
  480. COLOR_RED
  481. void * font = GLUT_BITMAP_HELVETICA_18;
  482. draw_text(420,780,"ALGORITHM CONTROL PANEL",font);
  483.  
  484. COLOR_BLACK
  485. font = GLUT_BITMAP_HELVETICA_12;
  486. draw_text(MARGIN+35,720,"Show Matrix",font);
  487.  
  488.  
  489. glBegin(GL_LINE_STRIP);
  490. glVertex2f(MARGIN+15,740);
  491. glVertex2f(MARGIN+15,700);
  492. glVertex2f(MARGIN+125,700);
  493. glVertex2f(MARGIN+125,740);
  494. glVertex2f(MARGIN+15,740);
  495. glEnd();
  496.  
  497.  
  498. }
  499.  
  500.  
  501. void DATAENTRY(void)
  502. {
  503. SQBUTTONS B;
  504. B.downX=32; B.upX=72; B.downY=65; B.upY=70; B.buttontype=EDGE_BUTTON;
  505. v_buttons.push_back(B);
  506. B.downX=20; B.upX=95; B.downY=110; B.upY=150; B.buttontype=ALGOWIN_BUTTON;
  507. v_buttons.push_back(B);
  508. B.downX=MARGIN+15; B.upX=MARGIN+125; B.downY=700; B.upY=740; B.buttontype=PANEL_MATRIX_BUTTON;
  509. v_panelbuttons.push_back(B);
  510.  
  511. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement