Advertisement
podsolnyxxx

Лаба 7, задание 2, с линиями

Mar 26th, 2024 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include <vector>
  2. #include <cmath>
  3. #include <random>
  4. #include <chrono>
  5. #include <thread>
  6.  
  7. #include "glut.h"
  8.  
  9. const int N = 3;
  10. const int M = 15;
  11. const float R = 0.25;
  12. const int K = 1000000;
  13.  
  14. std::random_device rd;
  15. std::mt19937 gen(rd());
  16. std::uniform_real_distribution<float> dis(0.0, 1.0);
  17.  
  18. struct Point {
  19. float x;
  20. float y;
  21. };
  22.  
  23. std::vector<Point> generatePoints() {
  24. std::vector<Point> points;
  25. for (int i = 0; i < N; ++i) {
  26. points.push_back({ dis(gen), dis(gen) });
  27. }
  28. return points;
  29. }
  30.  
  31. Point nextPoint(Point current, const std::vector<Point>& points) {
  32. int roll = std::uniform_int_distribution<int>(1, M)(gen);
  33. int index = (roll - 1) / (M / N);
  34. Point next = points[index];
  35.  
  36. float delta_x = next.x - current.x;
  37. float delta_y = next.y - current.y;
  38. float distance = sqrt(delta_x * delta_x + delta_y * delta_y);
  39. float ratio = R / distance;
  40.  
  41. return { current.x + ratio * delta_x, current.y + ratio * delta_y };
  42. }
  43.  
  44. std::vector<Point> computeRandomWalk(const std::vector<Point>& points) {
  45. std::vector<Point> randomWalk;
  46. Point current = { dis(gen), dis(gen) };
  47. for (int i = 0; i < K; ++i) {
  48. current = nextPoint(current, points);
  49. randomWalk.push_back(current);
  50. }
  51. return randomWalk;
  52. }
  53.  
  54. std::vector<Point> points;
  55. std::vector<Point> randomWalk;
  56. int currentStep = 0;
  57.  
  58. void update(int value) {
  59. if (currentStep < K) {
  60. randomWalk.push_back(nextPoint(randomWalk.back(), points));
  61. currentStep++;
  62. }
  63. glutPostRedisplay();
  64. glutTimerFunc(10, update, 0);
  65. }
  66.  
  67. void display() {
  68. glClear(GL_COLOR_BUFFER_BIT);
  69. glColor3f(1.0, 0.0, 0.0);
  70.  
  71. glBegin(GL_POINTS);
  72. for (const auto& point : points) {
  73. glVertex2f(point.x, point.y);
  74. }
  75. glEnd();
  76.  
  77. glColor3f(0.0, 0.0, 1.0);
  78. glBegin(GL_POINTS);
  79. for (size_t i = 0; i <= currentStep; ++i) {
  80. glVertex2f(randomWalk[i].x, randomWalk[i].y);
  81. }
  82. glEnd();
  83.  
  84. glColor3f(0.0, 0.0, 0.0);
  85. glBegin(GL_LINE_STRIP);
  86. for (size_t i = 0; i <= currentStep; ++i) {
  87. glVertex2f(randomWalk[i].x, randomWalk[i].y);
  88. }
  89. glEnd();
  90.  
  91. glFlush();
  92. }
  93.  
  94. void init() {
  95. glClearColor(1.0, 1.0, 1.0, 0.0);
  96. glMatrixMode(GL_PROJECTION);
  97. glLoadIdentity();
  98. gluOrtho2D(0.0, 1.0, 0.0, 1.0);
  99. }
  100.  
  101. int main(int argc, char** argv) {
  102. points = generatePoints();
  103. randomWalk.push_back({ dis(gen), dis(gen) });
  104.  
  105. glutInit(&argc, argv);
  106. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  107. glutInitWindowSize(800, 600);
  108. glutInitWindowPosition(100, 100);
  109. glutCreateWindow("Random Walk");
  110.  
  111. init();
  112. glutDisplayFunc(display);
  113. glutTimerFunc(10, update, 0);
  114. glutMainLoop();
  115.  
  116. return 0;
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement