Alex9090

Untitled

Mar 19th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. // sunt mentionate fisiere (biblioteci) care urmeaza sa fie incluse
  2. #include <GL/freeglut.h> // nu trebuie uitat freeglut.h (sau glut.h sau gl.h & glu.h)
  3. #include <iostream>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. struct vect
  9. {
  10. float x, y, z;
  11. };
  12.  
  13.  
  14. int nr_pct;
  15. vect coord[100];
  16.  
  17. GLint winWidth = 1024, winHeight = 768;
  18.  
  19. vect vectpro(vect u, vect, v) {
  20.  
  21. vect p;
  22. p.x = v.x - u.x;
  23. p.y = v.y - u.y;
  24. p.z = v.z - u.z;
  25.  
  26. return p;
  27. }
  28.  
  29.  
  30. vect Produs(vect p1, vect p2, vect p3) {
  31.  
  32. vect vector12 = vectpro(p1, p2);
  33. vect vector23 = vectpro(p2, p3);
  34. vect rezultat_produs;
  35.  
  36. rezultat_produs.x = vector12.y * vector23.z - vector12.z * vector23.y;
  37. rezultat_produs.y = vector12.x * vector23.z - vector12.z * vector23.x;
  38. rezultat_produs.z = vector12.x * vector23.y - vector12.y * vector23.x;
  39.  
  40. return rezultat_produs;
  41. }
  42.  
  43.  
  44. void afisare(vect vector1) {
  45. cout << vector1.x << "*i + " << vector1.y << "*j + " << vector1.z << "*k" << endl;
  46. }
  47.  
  48. bool CheckAngle(vect point1, vect point2, vect point3, int i) {
  49.  
  50. vect produs_vectori = Produs(point1, point2, point3);
  51. double f.produs_vectori = produs_vectori.x + produs_vectori.y + produs_vectori.z;
  52.  
  53. if (produs_vectori.x < 0 || produs_vectori.y < 0 || produs_vectori.z < 0) {
  54. cout << "Punct: I = " << coord[i + 1].x << " / J = " << coord[i + 1].y << " / K = " << coord[i + 1].z << endl;
  55. return true;
  56. }
  57. else {
  58. cout << "Punct: I = " << coord[i + 1].x << " / J = " << coord[i + 1].y << " / K = " << coord[i + 1].z << endl;
  59. return false;
  60. }
  61.  
  62.  
  63. }
  64.  
  65. void init(void) // initializare fereastra de vizualizare
  66. {
  67. glClearColor(1.0, 1.0, 1.0, 1.0); // precizeaza culoarea de fond a ferestrei de vizualizare
  68. glMatrixMode(GL_PROJECTION); // se precizeaza este vorba de o reprezentare 2D, realizata prin proiectie ortogonala
  69. gluOrtho2D(0.0, 1024.0, 0.0, 768.0); // sunt indicate coordonatele extreme ale ferestrei de vizualizare
  70. }
  71.  
  72.  
  73. void mouse2(int button, int state, int x, int y)
  74. {
  75. static int i = 0;
  76. switch (button) {
  77. case GLUT_LEFT_BUTTON:
  78. if (state == GLUT_DOWN) {
  79. coord[i].x = x;
  80. coord[i].y = y;
  81. i++;
  82. }
  83. break;
  84.  
  85. }
  86. }
  87.  
  88. void reshapeFcn(GLint newWidth, GLint newHeight)
  89. {
  90. glViewport(0, 0, newWidth, newHeight);
  91.  
  92. winWidth = newWidth;
  93. winHeight = newHeight;
  94. }
  95.  
  96. //Punct rotund = concav
  97. //Punct patrat = convex
  98.  
  99. void desen(void) // procedura desenare
  100. {
  101. system("cls");
  102. glClear(GL_COLOR_BUFFER_BIT);
  103. glColor3f(0.0, 0.0, 0.0);
  104. glBegin(GL_POLYGON);
  105. for (int i = 0; i < coordNr; i++) {
  106. glVertex2i(coord[i].x, coord[i].y);
  107. }
  108. glEnd();
  109.  
  110. glcoordize(10.0);
  111.  
  112. for (int i = 0; i < coordNr; i++) {
  113. if (i == coordNr - 2) {
  114. if (CheckAngle(coord[i], coord[i + 1], coord[0], i)) {
  115. glColor3f(1.0, 0.0, 0.0);
  116. glEnable(GL_POINT_SMOOTH);
  117. glBegin(GL_coord);
  118. glVertex2i(coord[i + 1].x, coord[i + 1].y);
  119. glEnd();
  120. glDisable(GL_POINT_SMOOTH);
  121. }
  122. else {
  123. glColor3f(0.0, 0.0, 1.0);
  124. glBegin(GL_coord);
  125. glVertex2i(coord[i + 1].x, coord[i + 1].y);
  126. glEnd();
  127. }
  128. }
  129. else
  130. if (i == coordNr - 1) {
  131. if (CheckAngle(coord[i], coord[0], coord[1], i)) {
  132. glColor3f(1.0, 0.0, 0.0);
  133. glEnable(GL_POINT_SMOOTH);
  134. glBegin(GL_coord);
  135. glVertex2i(coord[0].x, coord[0].y);
  136. glEnd();
  137. glDisable(GL_POINT_SMOOTH);
  138. }
  139. else {
  140. glColor3f(0.0, 0.0, 1.0);
  141. glBegin(GL_coord);
  142. glVertex2i(coord[0].x, coord[0].y);
  143. glEnd();
  144. }
  145. }
  146. else {
  147. if (CheckAngle(coord[i], coord[i + 1], coord[i + 2], i)) {
  148. glColor3f(1.0, 0.0, 0.0);
  149. glEnable(GL_POINT_SMOOTH);
  150. glBegin(GL_coord);
  151. glVertex2i(coord[i + 1].x, coord[i + 1].y);
  152. glEnd();
  153. glDisable(GL_POINT_SMOOTH);
  154. }
  155. else {
  156. glColor3f(0.0, 0.0, 1.0);
  157. glBegin(GL_coord);
  158. glVertex2i(coord[i + 1].x, coord[i + 1].y);
  159. glEnd();
  160. }
  161. }
  162. }
  163. glFlush(); // proceseaza procedurile OpenGL cat mai rapid
  164. }
  165.  
  166.  
  167. //100 100
  168. //700 100
  169. //350 350
  170. //700 700
  171. //100 700
  172.  
  173. int main(int argc, char** argv)
  174. {
  175. glutInit(&argc, argv); // initializare GLUT
  176. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // se utilizeaza un singur buffer | modul de colorare RedGreenBlue (= default)
  177. glutInitWindowPosition(500, 500); // pozitia initiala a ferestrei de vizualizare (in coordonate ecran)
  178. glutInitWindowSize(1024, 768); // dimensiunile ferestrei
  179. cout << "Numarul de puncte: ";
  180. cin >> coordNr;
  181. //for (int i = 0; i < coordNr; i++) {
  182. // cout << "Punctul: " << i + 1 << " Coordonata I: ";
  183. // cin >> coord[i].x;
  184. // cout << "Punctul: " << i + 1 << " Coordonata J: ";
  185. // cin >> coord[i].y;
  186. // coord[i].z = 0; //Pentru ca reprezentarea se face in 2D, nu mai are rost sa iau si a treia coordonata.
  187. //}
  188.  
  189. for (int i = 0; i < coordNr; i++) {
  190.  
  191. }
  192.  
  193. glutCreateWindow("ConvexConcavePolygonEdges "); // creeaza fereastra, indicand numele ferestrei de vizualizare - apare in partea superioara
  194.  
  195.  
  196. init(); // executa procedura de initializare
  197.  
  198.  
  199. glClear(GL_COLOR_BUFFER_BIT); // reprezentare si colorare fereastra de vizualizare
  200. glutMouseFunc(mouse2);
  201. glutDisplayFunc(desen); // procedura desen este invocata ori de cate ori este nevoie
  202. glutMainLoop(); // ultima instructiune a programului, asteapta (eventuale) noi date de intrare
  203. _getch();
  204.  
  205. return 0;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment