Advertisement
podsolnyxxx

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

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