Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #ifdef __APPLE__
  5. #define GL_SILENCE_DEPRECATION
  6. #include <OpenGL/gl.h>
  7. #include <GLUT/glut.h>
  8. #else
  9. #include <GL/gl.h>
  10. #include <GL/glut.h>
  11. #endif
  12. #include <map>
  13.  
  14. using namespace std;
  15. unsigned char prevKey;
  16.  
  17.  
  18. class GrilaCarteziana {
  19. public:
  20. int numarLinii;
  21. int numarColoane;
  22. double cx;
  23. double cy;
  24. double dc;
  25. double de;
  26. double epsilon;
  27. double radius;
  28.  
  29. GrilaCarteziana(int numarLinii, int numarColoane) {
  30. this->numarLinii = numarLinii;
  31. this->numarColoane = numarColoane;
  32. this->epsilon = 0.1;
  33. this->cx = -1 + this->epsilon;
  34. this->cy = -1 + this->epsilon;
  35. this->dc = (2 - 2 * this->epsilon) / (numarColoane - 1);
  36. this->de = (2 - 2 * this->epsilon) / (numarLinii - 1);
  37.  
  38. this->radius = this->dc / 3;
  39. this->deseneazaColoane();
  40. this->deseneazaLinii();
  41. }
  42.  
  43. void deseneazaLinii() {
  44. glColor3f(0.0, 0.0, 0.0);
  45. glBegin(GL_LINES);
  46. double p1_x = this->cx;
  47. double p_y = this->cy;
  48. double p2_x = -this->cx;
  49.  
  50. for (int i = 1; i <= this->numarLinii; i++) {
  51. glVertex2f(p1_x, p_y);
  52. glVertex2f(p2_x, p_y);
  53. p_y += this->de;
  54. }
  55.  
  56. glEnd();
  57. }
  58.  
  59. void deseneazaColoane() {
  60. glColor3f(0.0, 0.0, 0.0);
  61. glBegin(GL_LINES);
  62. double p_x = this->cx;
  63. double p1_y = this->cy;
  64. double p2_y = 1 - this->epsilon;
  65.  
  66. for (int i = 1; i <= this->numarColoane; i++) {
  67. glVertex2f(p_x, p1_y);
  68. glVertex2f(p_x, p2_y);
  69. p_x += this->dc;
  70. }
  71.  
  72. glEnd();
  73. }
  74.  
  75. void writePixel(int i, int j) {
  76. double x = this->cx + i * this->dc;
  77. double y = this->cy + j * this->de;
  78. deseneazaCerc(x, y, radius, 10000);
  79. printf("[WritePixel]X:%f Y:%f\n", x, y);
  80. }
  81.  
  82. void afisareSegmentDreapta3(int x0, int y0, int xn, int yn) {
  83. double dx, dy;
  84. dx = abs(xn - x0);
  85. dy = abs(yn - y0);
  86.  
  87. int d = 2 * dy - dx;
  88. int dE = 2 * dy;
  89. int dNE = 2 * (dy - dx);
  90. int x = x0, y = y0;
  91. map<int, int> m;
  92. m[x] = y;
  93. printf("X:%d Y:%d\n", x, y);
  94.  
  95. if (y < this->numarLinii) {
  96. this->writePixel(x, y);
  97. }
  98.  
  99. while (x < xn) {
  100. printf("d:%d\n", d);
  101. if (d <= 0) {
  102. d += dE;
  103. x++;
  104. }
  105. else {
  106. d += dNE;
  107. x++;
  108. if (x0 < xn && y0 < yn) {
  109. y++;
  110. }
  111. else {
  112. y--;
  113. }
  114. }
  115. printf("X:%d Y:%d\n", x, y);
  116. if (y < this->numarLinii) {
  117. this->writePixel(x, y);
  118. }
  119. m[x] = y;
  120. }
  121. }
  122.  
  123. void afisareSegmentDreapta3_1(int x0, int y0, int xn, int yn) {
  124. double dx, dy;
  125.  
  126. if (x0 < xn && y0 < yn) {
  127. dx = xn - x0;
  128. dy = yn - y0;
  129. }
  130. else {
  131. dx = abs(xn - x0);
  132. dy = abs(yn - y0);
  133. }
  134.  
  135. int d = 2 * dy - dx;
  136. int dE = 2 * dy;
  137. int dNE = 2 * (dy - dx);
  138. int x = x0, y = y0;
  139. map<int, int> m;
  140. m[x] = y;
  141.  
  142. printf("X:%d Y:%d\n", x, y);
  143. this->writePixel(x, y);
  144.  
  145. while (x > xn) {
  146. printf("d:%d\n", d);
  147. if (d <= 0) {
  148. d += dE;
  149. x++;
  150. }
  151. else {
  152. d += dNE;
  153. x++;
  154. if (x0 < xn && y0 < yn) {
  155. y++;
  156. }
  157. else {
  158. y--;
  159. }
  160. }
  161. printf("X:%d Y:%d\n", x, y);
  162. this->writePixel(x, y);
  163. m[x] = y;
  164. }
  165.  
  166. glColor3f(1.0, 0.1, 0.1);
  167. glBegin(GL_LINES);
  168. double x1, y1;
  169. x1 = this->cx + x0 * this->dc;
  170. y1 = this->cy + y0 * this->de;
  171. glVertex2f(x1, y1);
  172.  
  173. x1 = this->cx + xn * this->dc;
  174. y1 = this->cy + yn * this->de;
  175. glVertex2f(x1, y1);
  176. glEnd();
  177. }
  178.  
  179. void deseneazaCerc(double x, double y, double r, int numberOfSegments) {
  180.  
  181. double pi = 4 * atan(1.0);
  182.  
  183. glColor3ub(71, 71, 71);
  184. glBegin(GL_TRIANGLE_FAN);
  185. glVertex2f(x, y);
  186.  
  187. for (int i = 0; i < numberOfSegments; i++) {
  188. float x_aux = x + (this->radius*cos(i * 2 * pi / numberOfSegments));
  189. float y_aux = y + (this->radius*sin(i * 2 * pi / numberOfSegments));
  190. glVertex2f(x_aux, y_aux);
  191. }
  192.  
  193. glEnd();
  194. }
  195.  
  196. void afisareCerc4() {
  197. int x = 0;
  198. float y = this->radius;
  199. int d = 1 - this->radius;
  200. int dE = 3, dSE = 2 - this->radius + 5;
  201.  
  202. map<double, double> m;
  203. m[x] = y;
  204. m[-x] = -y;
  205. m[-x] = y;
  206. m[x] = -y;
  207.  
  208. if (x != y) {
  209. m[y] = x;
  210. m[-y] = -x;
  211. m[-y] = x;
  212. m[y] = -x;
  213. }
  214.  
  215. while (y > x) {
  216. if (d < 0) {
  217. d += dE;
  218. dE += 2;
  219. dSE += 2;
  220. }
  221. else {
  222. d += dSE;
  223. dE += 2;
  224. dSE += 4;
  225. y--;
  226. }
  227. x++;
  228. m[x] = y;
  229. m[-x] = -y;
  230. m[-x] = y;
  231. m[x] = -y;
  232. if (x != y) {
  233. m[y] = x;
  234. m[-y] = -x;
  235. m[-y] = x;
  236. m[y] = -x;
  237. }
  238. }
  239.  
  240. glColor3f(10, 0.1, 0.1);
  241. glLineWidth(4);
  242. glBegin(GL_LINES);
  243. map<double, double> ::iterator it;
  244.  
  245. for (it = m.begin(); it != m.end(); it++) {
  246. glVertex2f(this->cx + it->first*this->dc, this->cy + it->second*this->de);
  247. }
  248.  
  249. glEnd();
  250. }
  251.  
  252. void afisarePuncteCerc3(double x, double y) {
  253. map<double, double> m;
  254. m[x] = y;
  255. m[-x] = -y;
  256. m[-x] = y;
  257. m[x] = -y;
  258.  
  259. if (x != y) {
  260. m[y] = x;
  261. m[-y] = -x;
  262. m[-y] = x;
  263. m[y] = -x;
  264. }
  265. }
  266.  
  267. void deseneazaSegmentMartor(int x0, int y0, int xn, int yn) {
  268. glColor3f(1.0, 0.1, 0.1);
  269. glBegin(GL_LINES);
  270. double x1, y1;
  271. x1 = this->cx + x0 * this->dc;
  272. y1 = this->cy + y0 * this->de;
  273. glVertex2f(x1, y1);
  274. x1 = this->cx + xn * this->dc;
  275. y1 = this->cy + yn * this->de;
  276. glVertex2f(x1, y1);
  277. glEnd();
  278. }
  279. };
  280.  
  281. void Init(void) {
  282. glClearColor(1.0, 1.0, 1.0, 1.0);
  283. glLineWidth(1);
  284. glPointSize(4);
  285. glPolygonMode(GL_FRONT, GL_LINE);
  286. }
  287.  
  288. void display1() {
  289. int numarLinii = 16;
  290. int numarColoane = 16;
  291. GrilaCarteziana* grilaCarteziana = new GrilaCarteziana(numarLinii, numarColoane);
  292. printf("Linia1\n");
  293. grilaCarteziana->afisareSegmentDreapta3(0, 15, 15, 10);
  294. grilaCarteziana->afisareSegmentDreapta3(0, 14, 15, 9);
  295. grilaCarteziana->afisareSegmentDreapta3(0, 16, 15, 11);
  296. grilaCarteziana->deseneazaSegmentMartor(0, 15, 15, 10);
  297. printf("Linia2\n");
  298. grilaCarteziana->afisareSegmentDreapta3(0, 0, 15, 7);
  299. grilaCarteziana->deseneazaSegmentMartor(0, 15, 15, 10);
  300. grilaCarteziana->deseneazaSegmentMartor(0, 0, 15, 7);
  301. }
  302.  
  303. void Display(void) {
  304. printf("Call Display\n");
  305. glClear(GL_COLOR_BUFFER_BIT);
  306.  
  307. switch (prevKey) {
  308. case '1':
  309. display1();
  310. default:
  311. break;
  312. }
  313.  
  314. glFlush();
  315. }
  316.  
  317. void Reshape(int w, int h) {
  318. printf("Call Reshape : latime = %d, inaltime = %d\n", w, h);
  319. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  320. }
  321.  
  322. void KeyboardFunc(unsigned char key, int x, int y) {
  323. printf("Ati tastat <%c>. Mouse-ul este in pozitia %d, %d.\n", key, x, y);
  324. prevKey = key;
  325. if (key == 27)
  326. exit(0);
  327.  
  328. glutPostRedisplay();
  329. }
  330.  
  331. void MouseFunc(int button, int state, int x, int y) {
  332. printf("Call MouseFunc : ati %s butonul %s in pozitia %d %d\n",
  333. (state == GLUT_DOWN) ? "apasat" : "eliberat",
  334. (button == GLUT_LEFT_BUTTON) ?
  335. "stang" :
  336. ((button == GLUT_RIGHT_BUTTON) ? "drept" : "mijlociu"),
  337. x, y);
  338. }
  339.  
  340. int main(int argc, char** argv) {
  341. glutInit(&argc, argv);
  342. glutInitWindowSize(300, 300);
  343. glutInitWindowPosition(100, 100);
  344. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  345.  
  346. glutCreateWindow(argv[0]);
  347.  
  348. Init();
  349. glutReshapeFunc(Reshape);
  350. glutKeyboardFunc(KeyboardFunc);
  351. glutMouseFunc(MouseFunc);
  352. glutDisplayFunc(Display);
  353. glutMainLoop();
  354.  
  355. return 0;
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement