Advertisement
podsolnyxxx

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

Mar 26th, 2024 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 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. void RenderScene(void);
  12. void ChangeSize(int, int);
  13. void TimerFunction(int);
  14.  
  15.  
  16. int size = 10;
  17. int iter = 0;
  18. GLfloat windowWidth = 500;
  19. GLfloat windowHeight = 500;
  20. int generatedPointsCount = 0;
  21. int pointsInFigureCount = 0;
  22.  
  23. struct Point
  24. {
  25. float x;
  26. float y;
  27. Point() : x(0), y(0) {};
  28. Point(float x, float y) : x(x), y(y) {};
  29.  
  30. };
  31. vector <Point> Points(10);
  32.  
  33. void DrawPoint(Point& a, int size, float fillFlag)
  34. {
  35.  
  36. glColor3f(fillFlag, 0.0, 0.0);
  37. glPointSize(size);
  38. glBegin(GL_POINTS);
  39. glVertex2f(a.x, a.y);
  40. glEnd();
  41. }
  42.  
  43. void DrawCoordinateSystem()
  44. {
  45. glColor3f(0.0, 0.0, 0.0); // Чёрный цвет для осей
  46.  
  47. // Рисуем ось X
  48. glBegin(GL_LINES);
  49. glVertex2f(-3.14f, 0.0f); // Начало оси X
  50. glVertex2f(3.14f, 0.0f); // Конец оси X
  51. glEnd();
  52.  
  53. // Рисуем ось Y
  54. glBegin(GL_LINES);
  55. glVertex2f(0.0f, -3.14f); // Начало оси Y
  56. glVertex2f(0.0f, 3.14f); // Конец оси Y
  57. glEnd();
  58.  
  59. // Рисуем деления на осях
  60. for (float i = -3.14f; i <= 3.14f; i += 0.5f)
  61. {
  62. // Деления по оси X
  63. glBegin(GL_LINES);
  64. glVertex2f(i, 0.05f);
  65. glVertex2f(i, -0.05f);
  66. glEnd();
  67.  
  68. // Деления по оси Y
  69. glBegin(GL_LINES);
  70. glVertex2f(0.05f, i);
  71. glVertex2f(-0.05f, i);
  72. glEnd();
  73. }
  74. }
  75.  
  76. bool is_in_figure(Point a) {
  77. float result = (exp(a.x) * sin(a.x));
  78.  
  79. // Если x < 0 и точка находится выше кривой до x=0
  80. if (a.y < 0 && a.y > result) {
  81. return true;
  82. }
  83. // Если x > 0 и точка находится ниже кривой до x=0
  84. else if (a.y > 0 && a.y < result) {
  85. return true;
  86. }
  87. // Если ни одно из условий не выполнено, точка не попадает под кривую
  88. return false;
  89. }
  90.  
  91. void RenderScene(void)
  92. {
  93. glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
  94. glClear(GL_COLOR_BUFFER_BIT);
  95.  
  96. DrawCoordinateSystem(); // Рисуем систему координат
  97.  
  98. for (long long i = 0; i < Points.size(); i++)
  99. {
  100. if (is_in_figure(Points[i]))
  101. {
  102. DrawPoint(Points[i], 1, 1);
  103. }
  104. else
  105. {
  106. DrawPoint(Points[i], 1, 0);
  107. }
  108. }
  109.  
  110. glutSwapBuffers();
  111. }
  112.  
  113. float countResult = (5.56896);
  114.  
  115. void TimerFunction(int value) {
  116. float minX = -2 * 3.14 / 3; // минимальное значение x
  117. float maxX = 2 * 3.14 / 3; // максимальное значение x
  118. float minY = -2 * 3.14 / 3; // минимальное значение y
  119. float maxY = 2 * 3.14 / 3; // максимальное значение y
  120.  
  121. // Генерация случайных координат в указанном диапазоне
  122. float randX = minX + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (maxX - minX)));
  123. float randY = minY + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (maxY - minY)));
  124.  
  125. Point a(randX, randY);
  126.  
  127. generatedPointsCount++;
  128. if (is_in_figure(a)) {
  129. pointsInFigureCount++;
  130. }
  131. Points.push_back(a);
  132. if (generatedPointsCount == 10 || generatedPointsCount == 100 || generatedPointsCount == 1000 || generatedPointsCount == 10000 || generatedPointsCount == 100000)
  133. {
  134. float result = (float)pointsInFigureCount / (float)generatedPointsCount;
  135. float square = result * 17.6;
  136. cout << "Ploshad Monte-Carlo " << fixed << setprecision(6) << square << endl;
  137. float pogreshnost = abs(square - countResult) / countResult * 100;
  138. cout << "Pogreshnost v % " << fixed << setprecision(2) << pogreshnost << endl;
  139. }
  140. if (generatedPointsCount == 10000)
  141. {
  142. cout << "Analiticheski = " << fixed << setprecision(6) << countResult << endl;
  143. }
  144. glutPostRedisplay();
  145. glutTimerFunc(10, TimerFunction, 1);
  146. }
  147.  
  148. void ChangeSize(int width, int height)
  149. {
  150. GLfloat aspectRatio;
  151.  
  152. glViewport(0, 0, width, height);
  153.  
  154. glMatrixMode(GL_PROJECTION);
  155. glLoadIdentity();
  156. glOrtho(-0.07, 0.07, -0.07, 0.07, 1.0, -1.0);
  157.  
  158. aspectRatio = (GLfloat)width / (GLfloat)height;
  159. if (width <= height)
  160. {
  161. windowWidth = 100;
  162. windowHeight = 100 / aspectRatio;
  163. glOrtho(-100.0, 100.0,
  164. -windowHeight, windowHeight, 1.0, -1.0);
  165. }
  166. else
  167. {
  168. windowWidth = 100 * aspectRatio;
  169. windowHeight = 100;
  170. glOrtho(-windowWidth, windowWidth,
  171. -100.0, 100.0, 1.0, -1.0);
  172. }
  173.  
  174. glMatrixMode(GL_MODELVIEW);
  175. glLoadIdentity();
  176. }
  177.  
  178.  
  179. int main(int argc, char* argv[])
  180. {
  181. glutInit(&argc, argv);
  182. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  183. glutInitWindowSize(1000, 1000); // Размер окна
  184. glutCreateWindow("Template"); // Создание окна, его название
  185. glutDisplayFunc(RenderScene);
  186. glutReshapeFunc(ChangeSize);
  187. setlocale(LC_ALL, "rus");
  188. for (int i = 0; i < 1; i++) {
  189. glutTimerFunc(1, TimerFunction, 0);
  190. }
  191. srand(time(NULL));
  192.  
  193. glutMainLoop();
  194. }
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement