Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.73 KB | None | 0 0
  1. #include <GL/freeglut.h>
  2. #include <iostream>
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <mmsystem.h>
  7. #include <ctype.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11.  
  12.  
  13. #define DIM 5 //massimo numero di linee rappresentabili, cambiare questo numero per più linee
  14. #define FIN 500
  15. #pragma comment(lib, "Winmm.lib")
  16.  
  17. float x_1[DIM], y_1[DIM], x_2[DIM], y_2[DIM], v_y, m, q;
  18. float centro_x[DIM], centro_y[DIM], raggio[DIM], v_a, v_b, v_c;
  19. int pos = 0, win_x, win_y, pos_x, pos_y, pos_x_1, pos_y_1, pause, cerchio = 0;
  20. char fine = 'z', f;
  21. int color, g_x = (FIN / 2), g_y = (FIN / 2), zoom = 0, sound;
  22. float scale = 1.0f;
  23. int temp = 0, c, sorg = 0;
  24.  
  25. void myInit() {
  26. glClearColor(color, color, color, 1.0);
  27. glClear(GL_COLOR_BUFFER_BIT);
  28. glMatrixMode(GL_PROJECTION);
  29. gluOrtho2D(0, FIN, 0, FIN);
  30. }
  31.  
  32. void draw_line(float x_1, float x_2, float y_1, float y_2) {
  33. glVertex2f(x_1, y_1);
  34. glVertex2f(x_2, y_2);
  35. }
  36.  
  37. void DrawCircle(float cx, float cy, float r, int num_segments) {
  38. glBegin(GL_LINE_LOOP);
  39. for (int ii = 0; ii < num_segments; ii++) {
  40. float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);//get the current angle
  41. float x = r * cosf(theta);//calculate the x component
  42. float y = r * sinf(theta);//calculate the y component
  43. glVertex2f(x + cx, y + cy);//output vertex
  44. }
  45. glEnd();
  46. }
  47.  
  48. void myDisplay() {
  49. glClear(GL_COLOR_BUFFER_BIT);
  50. glClearColor(color, color, color, 1.0);
  51.  
  52. /* source and dest are arbitrary rectangles.
  53. float scaleX = dest.width / source.width;
  54. float scaleY = dest.height / source.height;
  55. Point sourceCenter = centerPointOfRect(source);
  56. Point destCenter = centerPointOfRect(dest);
  57.  
  58. glTranslatef(destCenter.x, destCenter.y, 0.0);
  59. glScalef(scaleX, scaleY, 0.0);
  60. glTranslatef(sourceCenter.x * -1.0, sourceCenter.y * -1.0, 0.0);
  61. Draw geometry in question with its normal verts*/
  62.  
  63. win_x = glutGet(GLUT_WINDOW_WIDTH);
  64. win_y = glutGet(GLUT_WINDOW_HEIGHT);
  65. //printf("\n%d --- %d\n", win_x, win_y);
  66. g_y = win_y - g_y;
  67. pos_x = g_x * FIN / win_x;
  68. pos_y = g_y * FIN / win_y;
  69. //printf("\n%d --- %d\n", pos_x, pos_y);
  70. glTranslatef(pos_x, pos_y, 0.0);
  71. glScalef(scale, scale, 0.0);
  72. glTranslatef((pos_x * -1.0), (pos_y * -1.0), 0.0);
  73. printf("\n-----------------------------------------------------\n");
  74. if (sorg==1)
  75. {
  76. sorg = 0;
  77. scale = 1.0;
  78. }
  79.  
  80.  
  81. if (color == 0)
  82. {
  83. glColor3f(1, 1, 1);
  84. }
  85. else {
  86. glColor3f(0, 0, 0);
  87. }
  88.  
  89. glBegin(GL_LINES);
  90.  
  91. draw_line(-100000, FIN*100, (FIN / 2), (FIN / 2));
  92. draw_line((FIN / 2), (FIN / 2), -100000, FIN*100);
  93. for (int i = -100000; i < FIN*35; i += 10)
  94. {
  95. draw_line(i, i, 255, 245);
  96. draw_line(255, 245, i, i);
  97. }
  98. /*draw_line((100 / FIN * 48), (FIN / 2), (98%FIN), FIN);
  99. draw_line((FIN / 2), 520, FIN, 980);
  100. draw_line(980, FIN, 480, (FIN / 2));
  101. draw_line(FIN, 980, (FIN / 2), 520);*/
  102. for (int r = 0; r < pos; r++)
  103. {
  104. switch (r)
  105. {
  106. case 0:
  107. glColor3f(1, 0, 0);
  108. break;
  109. case 1:
  110. glColor3f(0, 1, 0);
  111. break;
  112. case 2:
  113. glColor3f(0, 0, 1);
  114. break;
  115. case 3:
  116. glColor3f(0, 1, 1);
  117. break;
  118. case 4:
  119. glColor3f(1, 1, 0);
  120. break;
  121. default:
  122. if (color == 0)
  123. {
  124. glColor3f(1, 1, 1);
  125. }
  126. else {
  127. glColor3f(0, 0, 0);
  128. }
  129. break;
  130. }
  131. draw_line(x_1[r], x_2[r], y_1[r], y_2[r]);
  132. }
  133. glEnd();
  134. srand(time(NULL));
  135. for (int r = 0; r < cerchio; r++)
  136. {
  137.  
  138. switch (r)
  139. {
  140. case 0:
  141. glColor3f(1.0, 0.8, 0.6);
  142. break;
  143. case 1:
  144. glColor3f(0.5, 1, 1);
  145. break;
  146. case 2:
  147. glColor3f(1, 1, 0.6);
  148. break;
  149. case 3:
  150. glColor3f(0.2, 1, 0.1);
  151. break;
  152. case 4:
  153. glColor3f(0.5, 0.8, 1);
  154. break;
  155. default:
  156. if (color == 0)
  157. {
  158. glColor3f(1, 1, 1);
  159. }
  160. else {
  161. glColor3f(0, 0, 0);
  162. }
  163. break;
  164. }
  165. DrawCircle(centro_x[r] + (FIN / 2), centro_y[r] + (FIN / 2), raggio[r], 500);
  166. }
  167. glFlush();
  168. }
  169.  
  170. void music() {
  171. bool played = PlaySound(TEXT("unity.wav"), NULL, SND_FILENAME | SND_ASYNC);
  172. }
  173.  
  174. char isNumber(char* text)
  175. {
  176. int j;
  177. j = strlen(text);
  178. while (j--)
  179. {
  180. if (text[j] >= '-9' || text[j] <= '9')
  181. continue;
  182.  
  183. return 0;
  184. }
  185. return 1;
  186. }
  187.  
  188. float readParameter(const char* input) {
  189. char value[20];
  190.  
  191. do
  192. {
  193. printf("\t\t%s: ", input);
  194. scanf("%s", value);
  195. } while (!isNumber(value));
  196.  
  197. return atof(value);
  198.  
  199. }
  200.  
  201. float readParameter_figura(const char* input) {
  202. char value[20];
  203.  
  204. do
  205. {
  206. printf("\t%s ", input);
  207. scanf("%s", value);
  208. } while (!isNumber(value));
  209.  
  210. return atof(value);
  211.  
  212. }
  213.  
  214. void ask() {
  215. printf("\033[1;31m"); //bold red
  216. printf("<SETTINGS>\n\n");
  217. printf("\033[1;33m"); //bold yellow
  218. printf("#Choose a theme:\n\n");
  219. printf("\033[0m"); //reset
  220. printf("\t0 >>> Dark mode\n");
  221. printf("\t1 >>> Light mode\n\n");
  222. do
  223. {
  224. printf("\tEnter mode --> ");
  225. color = getchar();
  226. if (color == 2)
  227. continue;
  228.  
  229. // clean buffer
  230.  
  231. while ((c = getchar()) != '\n' && c != EOF)
  232. {
  233.  
  234. }
  235.  
  236. } while (!isdigit(color) || (color != '1' && color != '0'));
  237.  
  238. if (color == 48) {
  239. color = 0;
  240. }
  241. else if (color == 49) {
  242. color = 1;
  243. }
  244. printf("\033[1;36m"); //bold cyan
  245. if (color == 0)
  246. {
  247. printf("\n\tDark mode set!\n\n");
  248. }
  249. else if (color == 1) {
  250. printf("\n\tLight mode set!\n\n");
  251. }
  252. printf("\033[1;33m"); //bold yellow
  253. printf("#Sound Music:\n\n");
  254. printf("\033[0m"); //reset
  255. printf("\t0 >>> OFF\n");
  256. printf("\t1 >>> On\n\n");
  257. do
  258. {
  259. printf("\tEnter value --> ");
  260. sound = getchar();
  261. if (sound == 2)
  262. continue;
  263.  
  264. // clean buffer
  265.  
  266. while ((c = getchar()) != '\n' && c != EOF)
  267. {
  268.  
  269. }
  270.  
  271. } while (!isdigit(sound) || (sound != '1' && sound != '0'));
  272.  
  273. if (sound == 48) {
  274. sound = 0;
  275. }
  276. else if (sound == 49) {
  277. sound = 1;
  278. }
  279. printf("\033[1;36m"); //bold cyan
  280. if (sound == 0)
  281. {
  282. printf("\n\tSound Music OFF!\n\n");
  283. }
  284. else if (sound == 1) {
  285. printf("\n\tSound Music ON!\n\n");
  286. music();
  287. printf("\tMusic runnin: Unity - TheFatRat\n");
  288.  
  289. }
  290. printf("\033[1;31m"); //bold red
  291. printf("\n</SETTINGS>\n\n");
  292. printf("\033[0m"); //reset
  293. printf("--------------------------------------------------\n\n");
  294. int figura;
  295. do
  296. {
  297. printf("\033[1;33m"); //bold yellow
  298. printf("\n#Che figura vuoi inserire?\n\n");
  299. printf("\033[0m"); //reset
  300. printf("\t1 >>> Retta\n");
  301. printf("\t2 >>> circonferenza\n");
  302. printf("\t3 >>> Summary\n");
  303. printf("\t4 >>> Stop\n\n");
  304. do
  305. {
  306. //printf("\tEnter value --> ");
  307. figura = readParameter_figura("Enter value -->");
  308. } while (figura != 1 && figura != 2 && figura != 3 && figura !=4);
  309. if (figura == 1)
  310. {
  311. if (pos == DIM)
  312. {
  313. char decision = 'k';
  314. printf("\033[1;31m"); //bold red
  315. printf("\n\t#ATTENZIONE!\n\t\033[0;31mLE RETTE DISPONIBILI SONO FINITE, VUOI INSERIRE UNA RETTA COMUNQUE?\n\t\033[0m(Scegliendo si tutte le rette inserite precedentemente verranno cancellate)\n\t[s/n]\n");
  316. do
  317. {
  318. printf("\n\t-->");
  319. scanf_s(" %c", &decision);
  320. } while (decision != 's' && decision != 'n' && decision != 'S' && decision != 'N');
  321. if (decision == 's' || decision == 'S')
  322. {
  323. pos = 0;
  324. }
  325. }
  326. fine = 'p';
  327. while (fine != 'n' && fine != 'N' && pos != DIM)
  328. {
  329. printf("\033[1;32m"); //bold green
  330. printf("\n\t[RETTA]");
  331. printf("\033[0m"); //reset
  332. printf("\n\t-Enter a number(ny = mx+q)\n\n");
  333. v_y = readParameter("n");
  334. m = readParameter("m");
  335. q = readParameter("q");
  336.  
  337. if (v_y < 0)
  338. {
  339. v_y = -v_y;
  340. m = -m;
  341. q = -q;
  342. }
  343. x_1[pos] = (FIN / 2);
  344. y_1[pos] = (FIN / 2);
  345. if (v_y > 0)
  346. {
  347. y_1[pos] += ((float)q / (float)v_y * 10);
  348. }
  349.  
  350. x_2[pos] = x_1[pos];
  351. y_2[pos] = y_1[pos];
  352. for (int t = 0; t < 1000; t++)
  353. {
  354. x_2[pos] += (v_y * 10);
  355. y_2[pos] += (m * 10);
  356. x_1[pos] -= (v_y * 10);
  357. y_1[pos] -= (m * 10);
  358. }
  359. if (v_y == 0 && m != 0)
  360. {
  361. x_1[pos] -= ((float)q / (float)m * 10);
  362. x_2[pos] -= ((float)q / (float)m * 10);
  363. }
  364. pos++;
  365. if (pos != DIM)
  366. {
  367. printf("\033[1;32m"); //bold green
  368. printf("\n\t-Vuoi inserire un'altra retta? [s/n]\n\n");
  369. printf("\033[0m"); //reset
  370. do
  371. {
  372. printf("\t\t--> ");
  373. scanf_s(" %c", &fine);
  374. } while (fine != 's' && fine != 'n' && fine != 'S' && fine != 'N');
  375. }
  376. }
  377. }
  378. else if (figura == 2)
  379. {
  380. fine = 'p';
  381. while (fine != 'n' && fine != 'N' && cerchio != DIM)
  382. {
  383. printf("\033[1;32m"); //bold green
  384. printf("\n\t[CIRCONFERENZA]");
  385. printf("\033[0m"); //reset
  386. printf("\n\t-Enter a number(x^2+y^2+ax+by+c=0)\n\n");
  387. v_a = readParameter("a");
  388. v_b = readParameter("b");
  389. v_c = readParameter("c");
  390.  
  391. centro_x[cerchio] = ((-v_a) / 2) * 10;
  392. centro_y[cerchio] = ((-v_b) / 2) * 10;
  393. if (pow(centro_x[cerchio], 2) + pow(centro_y[cerchio], 2) - v_c < 0)
  394. {
  395. printf("\033[1;31m"); //bold red
  396. printf("\n\t\t-IL LUOGO GEOMETRICO INSERITO NON E UNA CIRCONFERENZA\n");
  397. printf("\033[0m"); //reset
  398. cerchio--;
  399. }
  400. else
  401. {
  402. raggio[cerchio] = sqrtf(pow(centro_x[cerchio], 2) + pow(centro_y[cerchio], 2) - v_c);
  403. }
  404. printf("\nCIRCO: %f, %f , %f , %f", centro_x[cerchio], centro_y[cerchio], v_c, raggio[cerchio]);
  405. cerchio++;
  406.  
  407. if (cerchio != DIM)
  408. {
  409. printf("\033[1;32m"); //bold green
  410. printf("\n\t-Vuoi inserire un'altra circonferenza? [s/n]\n\n");
  411. printf("\033[0m"); //reset
  412. do
  413. {
  414. printf("\t\t--> ");
  415. scanf_s(" %c", &fine);
  416. } while (fine != 's' && fine != 'n' && fine != 'S' && fine != 'N');
  417. }
  418. }
  419. }
  420. else if(figura == 3)
  421. {
  422. printf("\033[1;32m"); //bold green
  423. printf("\n\t-[SUMMARY]:\n\n");
  424. printf("\033[0m"); //reset
  425. if (pos == 0 && cerchio == 0)
  426. {
  427. printf("\tNothing has been entered!\n\n\n");
  428. }
  429. else
  430. {
  431. printf("\t%d lines has been entered\n", pos);
  432. printf("\t%d circumferences has been entered\n\n\n", cerchio);
  433. }
  434. }
  435. else
  436. {
  437. continue;
  438. }
  439.  
  440. } while (figura != 4);
  441.  
  442. }
  443. /*
  444. void MyKeyboardFunc(unsigned char Key, int x, int y)
  445. {
  446. switch (Key)
  447. {
  448. case '+':
  449. scale += 0.01;
  450. //printf("+\n");
  451. break;
  452. case '-':
  453. scale -= 0.01;
  454. //printf("-\n");
  455. break;
  456. }
  457. }
  458. */
  459.  
  460. void MouseHandler(int button, int state, int x, int y) {
  461.  
  462. if (button == GLUT_MIDDLE_BUTTON
  463. && state == GLUT_DOWN) {
  464. g_x = x; g_y = y;
  465. double b = pow(1.1, zoom);
  466. scale = (1 / b);
  467. zoom = 0;
  468. //printf("%d,%d \n", x, y);
  469.  
  470. //printf("\n\n%d\n\n", zoom);
  471. //printf("%d,%d \n", button, state);
  472. glutPostRedisplay();
  473. }
  474.  
  475. }
  476.  
  477. void look(int lx, int ly) {
  478. glutPostRedisplay();
  479. win_x = glutGet(GLUT_WINDOW_WIDTH);
  480. win_y = glutGet(GLUT_WINDOW_HEIGHT);
  481. //printf("\n%d --- %d\n", win_x, win_y);
  482. ly = win_y - ly;
  483. pos_x = lx * FIN / win_x;
  484. pos_y = ly * FIN / win_y;
  485. //printf("\n%d --- %d\n", pos_x, pos_y);
  486. glBegin(GL_LINES);
  487. if (color == 0)
  488. {
  489. glColor3f(1, 1, 1);
  490. }
  491. else {
  492. glColor3f(0, 0, 0);
  493. }
  494. draw_line(pos_x, pos_x, FIN, 0);
  495. draw_line(FIN, 0, pos_y, pos_y);
  496. glEnd();
  497. glFlush();
  498. }
  499.  
  500. void animate() {
  501.  
  502. }
  503.  
  504. void mouseWheel(int button, int dir, int x, int y)
  505. {
  506. if (button == 0 && dir == 1)
  507. {
  508. g_x = x; g_y = y;
  509. if (zoom > 35)
  510. {
  511. scale = 1.02;
  512. }
  513. else if (zoom > 28)
  514. {
  515. scale = 1.05;
  516. }
  517. else
  518. {
  519. scale = 1.1;
  520. }
  521. zoom++;
  522. printf("\nMousewheel X Y: %d,%d \n", x, y);
  523. }
  524. else if (button == 0 && dir == -1)
  525. {
  526. g_x = x; g_y = y;
  527. if (zoom > -4)
  528. {
  529. if (zoom > 35)
  530. {
  531. scale = (1 / 1.02);
  532. zoom--;
  533. }
  534. else if (zoom > 28)
  535. {
  536. scale = (1 / 1.05);
  537. zoom--;
  538. }
  539. else
  540. {
  541. scale = (1 / 1.1);
  542. zoom--;
  543. }
  544. }
  545. else if (zoom <= 0)
  546. {
  547. scale = 1.0;
  548. }
  549. printf("\nMousewheel X Y: %d,%d \n", x, y);
  550. }
  551. else
  552. {
  553. scale = 1.0;
  554. }
  555. printf("\n\nMousewheel Zoom: %d\n", zoom);
  556. printf("\nMousewheel Scale: %d\n", scale);
  557. glutPostRedisplay();
  558. sorg = 1;
  559. }
  560.  
  561. void move(int bx, int by) {
  562.  
  563. if (temp == 0)
  564. {
  565. pos_x_1 = bx;
  566. pos_y_1 = by;
  567. temp++;
  568. }
  569. printf_s("\nMove X Y: %d ____ %d\n",bx,by);
  570. printf_s("\nSpostamento effettivo: %d ____ %d\n", (bx - pos_x_1 /* (zoom + 1)*/), (pos_y_1 - by /* (zoom + 1)*/));
  571. if ((bx - pos_x_1) > 20 || (pos_y_1 - by) > 20 || (bx - pos_x_1) < -20 || (pos_y_1 - by) < -20)
  572. {
  573. pos_x_1 = bx;
  574. pos_y_1 = by;
  575. }
  576. if (zoom > 20)
  577. {
  578. glTranslatef((bx - pos_x_1) / 3, (pos_y_1 - by) / 3, 0.0);
  579. }
  580. else if (zoom > 10)
  581. {
  582. glTranslatef((bx - pos_x_1) / 2, (pos_y_1 - by) / 2, 0.0);
  583. }
  584. else
  585. {
  586. glTranslatef((bx - pos_x_1), (pos_y_1 - by), 0.0);
  587. }
  588. scale = 1.0;
  589. glutPostRedisplay();
  590. pos_x_1 = bx;
  591. pos_y_1 = by;
  592. }
  593.  
  594. int main(int argc, char** argv) {
  595.  
  596. for (int j = 0; j < DIM; j++)
  597. {
  598. x_1[j] = 0;
  599. x_2[j] = 0;
  600. y_1[j] = 0;
  601. y_2[j] = 0;
  602. }
  603. ask();
  604.  
  605. glutInit(&argc, argv);
  606. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  607. glutInitWindowSize(500, 500);
  608. glutInitWindowPosition(50, 50);
  609. glutCreateWindow("Graphic Project");
  610. myInit();
  611. glutDisplayFunc(myDisplay);
  612. //glutKeyboardFunc(MyKeyboardFunc);
  613. //glutMouseFunc(MouseHandler);
  614. glutMouseWheelFunc(mouseWheel);
  615. /*glutMotionFunc(motion_func);
  616. glutReshapeFunc(change_viewport); // when window is resized
  617. glutDisplayFunc(render); // when window needs to be drawn
  618. glutIdleFunc(animate); // when there is nothing else to do
  619. glutMouseFunc(callback_mouse_button); // when mouse is clicked
  620. glutKeyboardFunc(keyboard_down); // when a key is down
  621. glutKeyboardUpFunc(keyboard_up); // when the key goes up
  622. glutPassiveMotionFunc(look); // when mouse moves*/
  623. glutMotionFunc(move); // when mouse drags around
  624. //glutPassiveMotionFunc(look);
  625. glutIdleFunc(animate);
  626. glutMainLoop();
  627. return 0;
  628. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement