Advertisement
podsolnyxxx

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

Mar 26th, 2024 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 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.  
  27. vector <Point> Points(10);
  28.  
  29. void DrawLine(float x1, float y1, float x2, float y2)
  30. {
  31. glBegin(GL_LINES);
  32. glColor3f(0.0, 0.0, 0.0);
  33. glVertex2f(x1, y1);
  34. glVertex2f(x2, y2);
  35.  
  36. glEnd();
  37. }
  38.  
  39. void DrawLine(Point a, Point b)
  40. {
  41. glBegin(GL_LINES);
  42. glColor3f(0.0, 0.0, 0.0);
  43. glVertex2f(a.x, a.y);
  44. glVertex2f(b.x, b.y);
  45.  
  46. glEnd();
  47. }
  48.  
  49. void DrawPoint(Point& a, int size, float fillFlag)
  50. {
  51.  
  52. glColor3f(fillFlag, 0.0, 0.0);
  53. glPointSize(size);
  54. glBegin(GL_POINTS);
  55. glVertex2f(a.x, a.y);
  56. glEnd();
  57. }
  58.  
  59. void DrawOs()
  60. {
  61. DrawLine(1000, 0, -1000, 0);
  62. DrawLine(0, 1000, 0, -1000);
  63. DrawLine(1000, 0, 950, 30);
  64. DrawLine(1000, 0, 950, -30);
  65. DrawLine(0, 1000, -30, 950);
  66. DrawLine(0, 1000, 30, 950);
  67. for (int i = -950; i <= 950; i += 50)
  68. {
  69. DrawLine(i, 15, i, -15);
  70. }
  71. for (int i = -950; i <= 950; i += 50)
  72. {
  73. DrawLine(15, i, -15, i);
  74. }
  75. }
  76.  
  77. void DrawSmalOs()
  78. {
  79. DrawLine(10, 0, -10, 0);
  80. DrawLine(0, 10, 0, -10);
  81. DrawLine(10, 0, 9.5, 0.3);
  82. DrawLine(10, 0, 9.5, -0.3);
  83. DrawLine(0, 10, -0.3, 9.5);
  84. DrawLine(0, 10, 0.3, 9.5);
  85. for (float i = -9.5; i <= 9.6; i += 0.5) {
  86. DrawLine(i, 0.15, i, -0.15);
  87. }
  88. for (float i = -9.5; i <= 9.5; i += 0.5) {
  89. DrawLine(0.15, i, -0.15, i);
  90. }
  91. }
  92.  
  93. bool is_in_figure(Point a)
  94. {
  95. float result = (1 / (sqrt(4 * a.x - 3 + a.x * a.x)));
  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. glClear(GL_COLOR_BUFFER_BIT);
  116. DrawSmalOs();
  117. DrawLine(-1, 0.0, 1, 0.0);
  118. for (long long i = 0; i < Points.size(); i++)
  119. {
  120. if (is_in_figure(Points[i]))
  121. {
  122. DrawPoint(Points[i], 1, 1);
  123. }
  124. else { DrawPoint(Points[i], 1, 0);
  125. }
  126. }
  127.  
  128. glutSwapBuffers();
  129.  
  130. }
  131.  
  132. float countResult = (0.947481);
  133.  
  134. void TimerFunction(int value)
  135. {
  136. float minX = 1; // минимальное значение x
  137. float maxX = 4; // максимальное значение x
  138. float minY = 0; // минимальное значение y
  139. float maxY = 3; // максимальное значение y
  140.  
  141. // Генерация случайных координат в указанном диапазоне
  142. float randX = minX + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (maxX - minX)));
  143. float randY = minY + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (maxY - minY)));
  144.  
  145. Point a(randX, randY);
  146.  
  147. generatedPointsCount++;
  148. if (is_in_figure(a))
  149. {
  150. pointsInFigureCount++;
  151. }
  152. Points.push_back(a);
  153. if (generatedPointsCount == 10 || generatedPointsCount == 100 || generatedPointsCount == 1000 || generatedPointsCount == 10000 || generatedPointsCount == 100000)
  154. {
  155. float result = (float)pointsInFigureCount / (float)generatedPointsCount;
  156. float square = result * 3;
  157. cout << "Ploshad Monte-Carlo " << fixed << setprecision(6) << square << endl;
  158. float pogreshnost = abs(square - countResult) / countResult * 100;
  159. cout << "Pogreshnost v % " << fixed << setprecision(2) << pogreshnost << endl;
  160. }
  161. if (generatedPointsCount == 10000)
  162. {
  163. cout << "Analiticheski = " << fixed << setprecision(6) << countResult << endl;
  164. }
  165. glutPostRedisplay();
  166. glutTimerFunc(10, TimerFunction, 1);
  167. }
  168.  
  169. void ChangeSize(int width, int height)
  170. {
  171. GLfloat aspectRatio;
  172.  
  173. glViewport(0, 0, width, height);
  174.  
  175.  
  176. glMatrixMode(GL_PROJECTION);
  177. glLoadIdentity();
  178. glOrtho(-0.07, 0.07, -0.07, 0.07, 1.0, -1.0);
  179.  
  180. aspectRatio = (GLfloat)width / (GLfloat)height;
  181. if (width <= height)
  182. {
  183. windowWidth = 100;
  184. windowHeight = 100 / aspectRatio;
  185. glOrtho(-100.0, 100.0,
  186. -windowHeight, windowHeight, 1.0, -1.0);
  187. }
  188. else
  189. {
  190. windowWidth = 100 * aspectRatio;
  191. windowHeight = 100;
  192. glOrtho(-windowWidth, windowWidth,
  193. -100.0, 100.0, 1.0, -1.0);
  194. }
  195.  
  196. glMatrixMode(GL_MODELVIEW);
  197. glLoadIdentity();
  198. }
  199.  
  200. int main(int argc, char* argv[])
  201. {
  202. glutInit(&argc, argv);
  203. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  204. glutInitWindowSize(1000, 1000);
  205. glutCreateWindow("Template");
  206. glutReshapeFunc(ChangeSize);
  207. setlocale(LC_ALL, "rus");
  208. for (int i = 0; i < 1; i++) {
  209. glutTimerFunc(1, TimerFunction, 0);
  210. }
  211. srand(time(NULL));
  212.  
  213. glutMainLoop();
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement