Advertisement
podsolnyxxx

Лаба 7, задание 3

Mar 26th, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. #include <iostream>
  2. #include "glut.h"
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <ctime>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. void display(void)
  11. {
  12. glClear(GL_COLOR_BUFFER_BIT);
  13. glColor3f(0.0, 1.0, 0.0);
  14.  
  15. int num_points = 10000;
  16. int points_inside = 0;
  17.  
  18. for (int i = 0; i < num_points; i++)
  19. {
  20. double x = -2 + 4 * (rand() / (double)RAND_MAX);
  21. double y = -2 + 4 * (rand() / (double)RAND_MAX);
  22.  
  23. if (-pow(x, 3) - pow(y, 4) < 2 && 3 * x + pow(y, 2) < 2 && (x > -2 && x < 2) && (y > -2 && y < 2))
  24. {
  25. points_inside++;
  26. glColor3f(1.0, 0.0, 0.0); // Красный цвет для точек внутри фигуры
  27. }
  28. else
  29. {
  30. glColor3f(0.0, 1.0, 0.0); // Зеленый цвет для точек снаружи фигуры
  31. }
  32.  
  33. glBegin(GL_POINTS);
  34. glVertex2f(x, y);
  35. glEnd();
  36. // Выводим оси координат белым цветом
  37. glColor3f(1.0, 1.0, 1.0);
  38. glBegin(GL_LINES);
  39. // Ось x
  40. glVertex2f(-2.0, 0.0);
  41. glVertex2f(2.0, 0.0);
  42. // Ось y
  43. glVertex2f(0.0, -2.0);
  44. glVertex2f(0.0, 2.0);
  45.  
  46. // Добавляем деления через каждые 0.5 по осям x и y
  47. glBegin(GL_LINES);
  48. // Деления по оси x
  49. for (double i = -2.0; i <= 2.0; i += 0.5)
  50. {
  51. glVertex2f(i, -0.025);
  52. glVertex2f(i, 0.025);
  53. }
  54. // Деления по оси y
  55. for (double i = -2.0; i <= 2.0; i += 0.5)
  56. {
  57. glVertex2f(-0.025, i);
  58. glVertex2f(0.025, i);
  59. }
  60. glEnd();
  61.  
  62. glutSwapBuffers();
  63. }
  64.  
  65. double area = 16 * (double)points_inside / num_points;
  66. cout << "Estimated area:" << fixed << setprecision(8) << area << endl;
  67. }
  68.  
  69. void init(void)
  70. {
  71. glClearColor(0.0, 0.0, 0.0, 0.0);
  72. glMatrixMode(GL_PROJECTION);
  73. gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
  74. }
  75.  
  76. int main(int argc, char** argv)
  77. {
  78. // Инициализация
  79. glutInit(&argc, argv);
  80. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  81. glutInitWindowSize(500, 500);
  82. glutCreateWindow("Monte-Carlo Method for Area Calculation");
  83.  
  84. srand(time(NULL));
  85.  
  86. glutDisplayFunc(display);
  87. init();
  88. glutMainLoop();
  89.  
  90. return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement