Advertisement
podsolnyxxx

Лаба 7, задание 4, график D

Mar 26th, 2024 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include "glut.h"
  5. #include "math.h"
  6. #include <windows.h>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10.  
  11. int size = 10;
  12. int iter = 0;
  13. GLfloat windowWidth = 500;
  14. GLfloat windowHeight = 500;
  15. int generatedPointsCount = 0;
  16. int pointsInFigureCount = 0;
  17.  
  18. struct Point
  19. {
  20. float x;
  21. float y;
  22. Point() : x(0), y(0) {};
  23. Point(float x, float y) : x(x), y(y) {};
  24.  
  25. };
  26. vector <Point> Points(10);
  27.  
  28. void DrawLine(float x1, float y1, float x2, float y2)
  29. {
  30. glBegin(GL_LINES);
  31. glColor3f(0.0, 0.0, 0.0);
  32. glVertex2f(x1, y1);
  33. glVertex2f(x2, y2);
  34.  
  35. glEnd();
  36. }
  37.  
  38. void DrawLine(Point a, Point b)
  39. {
  40. glBegin(GL_LINES);
  41. glColor3f(0.0, 0.0, 0.0);
  42. glVertex2f(a.x, a.y);
  43. glVertex2f(b.x, b.y);
  44.  
  45. glEnd();
  46. }
  47.  
  48. void DrawPoint(Point& a, int size, float fillFlag)
  49. {
  50. glColor3f(fillFlag, 0.0, 0.0);
  51. glPointSize(size);
  52. glBegin(GL_POINTS);
  53. glVertex2f(a.x, a.y);
  54. glEnd();
  55. }
  56.  
  57. void DrawOs()
  58. {
  59. DrawLine(1000, 0, -1000, 0);
  60. DrawLine(0, 1000, 0, -1000);
  61. DrawLine(1000, 0, 950, 30);
  62. DrawLine(1000, 0, 950, -30);
  63. DrawLine(0, 1000, -30, 950);
  64. DrawLine(0, 1000, 30, 950);
  65. for (int i = -950; i <= 950; i += 50)
  66. {
  67. DrawLine(i, 15, i, -15);
  68. }
  69. for (int i = -950; i <= 950; i += 50)
  70. {
  71. DrawLine(15, i, -15, i);
  72. }
  73. }
  74.  
  75. void DrawSmalOs()
  76. {
  77. DrawLine(10, 0, -10, 0);
  78. DrawLine(0, 10, 0, -10);
  79. DrawLine(10, 0, 9.5, 0.3);
  80. DrawLine(10, 0, 9.5, -0.3);
  81. DrawLine(0, 10, -0.3, 9.5);
  82. DrawLine(0, 10, 0.3, 9.5);
  83. for (float i = -9.5; i <= 9.6; i += 0.5)
  84. {
  85. DrawLine(i, 0.15, i, -0.15);
  86. }
  87. for (float i = -9.5; i <= 9.5; i += 0.5)
  88. {
  89. DrawLine(0.15, i, -0.15, i);
  90. }
  91. }
  92.  
  93. bool is_in_figure(Point a)
  94. {
  95. float result = sin(2 * a.x) / (4 - pow(cos(2 * a.x), 2));
  96.  
  97. // Если x < 0 и точка находится выше кривой до x=0
  98. if (a.y < 0 && a.y > result)
  99. {
  100. return true;
  101. }
  102. // Если x > 0 и точка находится ниже кривой до x=0
  103. else if (a.y > 0 && a.y < result)
  104. {
  105. return true;
  106. }
  107. // Если ни одно из условий не выполнено, точка не попадает под кривую
  108. return false;
  109. }
  110.  
  111. void RenderScene(void)
  112. {
  113. setlocale(LC_ALL, "rus");
  114. glClearColor(1.0f, 1.0f, 1.0f, 0.0f); // Цвет фона окна
  115.  
  116. glClear(GL_COLOR_BUFFER_BIT);
  117. DrawSmalOs();
  118. DrawLine(-1, 0.0, 1, 0.0);
  119. for (long long i = 0; i < Points.size(); i++)
  120. {
  121.  
  122. if (is_in_figure(Points[i]))
  123. {
  124. DrawPoint(Points[i], 1, 1);
  125. }
  126. else
  127. {
  128. DrawPoint(Points[i], 1, 0);
  129. }
  130. }
  131.  
  132. glutSwapBuffers();
  133.  
  134. }
  135.  
  136. float countResult = (0.13732);
  137.  
  138. void TimerFunction(int value)
  139. {
  140. float minX = 3.14 / 4; // минимальное значение x
  141. float maxX = 3.14 / 2; // максимальное значение x
  142. float minY = 0; // минимальное значение y
  143. float maxY = 0.785; // максимальное значение y
  144.  
  145. // Генерация случайных координат в указанном диапазоне
  146. float randX = minX + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (maxX - minX)));
  147. float randY = minY + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (maxY - minY)));
  148.  
  149. Point a(randX, randY);
  150.  
  151. generatedPointsCount++;
  152. if (is_in_figure(a))
  153. {
  154. pointsInFigureCount++;
  155. }
  156. Points.push_back(a);
  157. if (generatedPointsCount == 10 || generatedPointsCount == 100 || generatedPointsCount == 1000 || generatedPointsCount == 10000 || generatedPointsCount == 100000)
  158. {
  159. float result = (float)pointsInFigureCount / (float)generatedPointsCount;
  160. float square = result * 0.616;
  161. cout << "Ploshad Monte-Carlo " << fixed << setprecision(6) << square << endl;
  162. float pogreshnost = abs(square - countResult) / countResult * 100;
  163. cout << "Pogreshnost v % " << fixed << setprecision(2) << pogreshnost << endl;
  164. }
  165. if (generatedPointsCount == 10000)
  166. {
  167. cout << "Analiticheski = " << fixed << setprecision(6) << countResult << endl;
  168. }
  169. glutPostRedisplay();
  170. glutTimerFunc(10, TimerFunction, 1);
  171. }
  172.  
  173. void SetupRC(void)
  174. {
  175.  
  176. // Устанавливаем синий цвет очистки
  177. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  178.  
  179. }
  180.  
  181. void ChangeSize(int width, int height)
  182. {
  183. GLfloat aspectRatio;
  184.  
  185. // Предотвращаем деление на нуль
  186. if (height == 0)
  187. height = 1;
  188.  
  189. // Устанавливаем поле просмотра с размерами окна
  190. glViewport(0, 0, width, height);
  191.  
  192.  
  193. glMatrixMode(GL_PROJECTION);
  194. glLoadIdentity();
  195. glOrtho(-0.07, 0.07, -0.07, 0.07, 1.0, -1.0);
  196.  
  197. aspectRatio = (GLfloat)width / (GLfloat)height;
  198. if (width <= height)
  199. {
  200. windowWidth = 100;
  201. windowHeight = 100 / aspectRatio;
  202. glOrtho(-100.0, 100.0,
  203. -windowHeight, windowHeight, 1.0, -1.0);
  204. }
  205. else
  206. {
  207. windowWidth = 100 * aspectRatio;
  208. windowHeight = 100;
  209. glOrtho(-windowWidth, windowWidth,
  210. -100.0, 100.0, 1.0, -1.0);
  211. }
  212.  
  213. // Восстанавливает матрицу преобразования в исходный режим вида
  214. glMatrixMode(GL_MODELVIEW);
  215. glLoadIdentity();
  216. }
  217.  
  218.  
  219. int main(int argc, char* argv[])
  220. {
  221. glutInit(&argc, argv);
  222. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  223. glutInitWindowSize(1000, 1000); // Размер окна
  224. glutCreateWindow("Graphic D"); // Создание окна, его название
  225. glutDisplayFunc(RenderScene);
  226. glutReshapeFunc(ChangeSize);
  227. setlocale(LC_ALL, "rus");
  228. for (int i = 0; i < 1; i++) {
  229. glutTimerFunc(1, TimerFunction, 0);
  230. }
  231. srand(time(NULL));
  232.  
  233. glutMainLoop();
  234. }
  235.  
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement