Advertisement
podsolnyxxx

Лаба 7, задание 2, без линий

Mar 27th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1.  
  2. #include <vector>
  3. #include <cmath>
  4. #include <random>
  5. #include <chrono>
  6. #include <thread>
  7. #include "glut.h"
  8. const int N = 3;
  9. const int M = 15;
  10. const float R = 0.25;
  11. const int K = 1000000;
  12.  
  13. std::random_device rd;
  14. std::mt19937 gen(rd());
  15. std::uniform_real_distribution<float> dis(0.0, 1.0);
  16.  
  17. struct Point {
  18. float x;
  19. float y;
  20. };
  21.  
  22. std::vector<Point> generatePoints() {
  23. std::vector<Point> points;
  24. for (int i = 0; i < N; ++i) {
  25. points.push_back({ dis(gen), dis(gen) });
  26. }
  27. return points;
  28. }
  29.  
  30. Point nextPoint(Point current, const std::vector<Point>& points) {
  31. int roll = std::uniform_int_distribution<int>(1, M)(gen);
  32. int index = (roll - 1) / (M / N);
  33. Point next = points[index];
  34.  
  35. float delta_x = next.x - current.x;
  36. float delta_y = next.y - current.y;
  37. float distance = sqrt(delta_x * delta_x + delta_y * delta_y);
  38. float ratio = R / distance;
  39.  
  40. return { current.x + ratio * delta_x, current.y + ratio * delta_y };
  41. }
  42.  
  43. std::vector<Point> computeRandomWalk(const std::vector<Point>& points) {
  44. std::vector<Point> randomWalk;
  45. Point current = { dis(gen), dis(gen) };
  46. for (int i = 0; i < K; ++i) {
  47. current = nextPoint(current, points);
  48. randomWalk.push_back(current);
  49. }
  50. return randomWalk;
  51. }
  52.  
  53. std::vector<Point> points;
  54. std::vector<Point> randomWalk;
  55. int currentStep = 0;
  56.  
  57. void update(int value) {
  58. if (currentStep < K) {
  59. randomWalk.push_back(nextPoint(randomWalk.back(), points));
  60. currentStep++;
  61. }
  62. glutPostRedisplay();
  63. glutTimerFunc(10, update, 0);
  64. }
  65.  
  66. void display() {
  67. glClear(GL_COLOR_BUFFER_BIT);
  68. glColor3f(1.0, 0.0, 0.0);
  69.  
  70. glBegin(GL_POINTS);
  71. for (const auto& point : points) {
  72. glVertex2f(point.x, point.y);
  73. }
  74. glEnd();
  75.  
  76. glColor3f(0.0, 0.0, 1.0);
  77. glBegin(GL_POINTS);
  78. for (size_t i = 0; i <= currentStep; ++i) {
  79. glVertex2f(randomWalk[i].x, randomWalk[i].y);
  80. }
  81. glEnd();
  82.  
  83. glFlush();
  84. }
  85.  
  86. void init() {
  87. glClearColor(1.0, 1.0, 1.0, 0.0);
  88. glMatrixMode(GL_PROJECTION);
  89. glLoadIdentity();
  90. gluOrtho2D(0.0, 1.0, 0.0, 1.0);
  91. }
  92.  
  93. int main(int argc, char** argv) {
  94. points = generatePoints();
  95. randomWalk.push_back({ dis(gen), dis(gen) });
  96.  
  97. glutInit(&argc, argv);
  98. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  99. glutInitWindowSize(800, 600);
  100. glutInitWindowPosition(100, 100);
  101. glutCreateWindow("Random Walk");
  102.  
  103. init();
  104. glutDisplayFunc(display);
  105. glutTimerFunc(10, update, 0);
  106. glutMainLoop();
  107.  
  108. return 0;
  109. }
  110.  
  111. #include <vector>
  112. #include <cmath>
  113. #include <random>
  114. #include <chrono>
  115. #include <thread>
  116. #include "glut.h"
  117. const int N = 3;
  118. const int M = 15;
  119. const float R = 0.25;
  120. const int K = 1000000;
  121.  
  122. std::random_device rd;
  123. std::mt19937 gen(rd());
  124. std::uniform_real_distribution<float> dis(0.0, 1.0);
  125.  
  126. struct Point {
  127. float x;
  128. float y;
  129. };
  130.  
  131. std::vector<Point> generatePoints() {
  132. std::vector<Point> points;
  133. for (int i = 0; i < N; ++i) {
  134. points.push_back({ dis(gen), dis(gen) });
  135. }
  136. return points;
  137. }
  138.  
  139. Point nextPoint(Point current, const std::vector<Point>& points) {
  140. int roll = std::uniform_int_distribution<int>(1, M)(gen);
  141. int index = (roll - 1) / (M / N);
  142. Point next = points[index];
  143.  
  144. float delta_x = next.x - current.x;
  145. float delta_y = next.y - current.y;
  146. float distance = sqrt(delta_x * delta_x + delta_y * delta_y);
  147. float ratio = R / distance;
  148.  
  149. return { current.x + ratio * delta_x, current.y + ratio * delta_y };
  150. }
  151.  
  152. std::vector<Point> computeRandomWalk(const std::vector<Point>& points) {
  153. std::vector<Point> randomWalk;
  154. Point current = { dis(gen), dis(gen) };
  155. for (int i = 0; i < K; ++i) {
  156. current = nextPoint(current, points);
  157. randomWalk.push_back(current);
  158. }
  159. return randomWalk;
  160. }
  161.  
  162. std::vector<Point> points;
  163. std::vector<Point> randomWalk;
  164. int currentStep = 0;
  165.  
  166. void update(int value) {
  167. if (currentStep < K) {
  168. randomWalk.push_back(nextPoint(randomWalk.back(), points));
  169. currentStep++;
  170. }
  171. glutPostRedisplay();
  172. glutTimerFunc(10, update, 0);
  173. }
  174.  
  175. void display() {
  176. glClear(GL_COLOR_BUFFER_BIT);
  177. glColor3f(1.0, 0.0, 0.0);
  178.  
  179. glBegin(GL_POINTS);
  180. for (const auto& point : points) {
  181. glVertex2f(point.x, point.y);
  182. }
  183. glEnd();
  184.  
  185. glColor3f(0.0, 0.0, 1.0);
  186. glBegin(GL_POINTS);
  187. for (size_t i = 0; i <= currentStep; ++i) {
  188. glVertex2f(randomWalk[i].x, randomWalk[i].y);
  189. }
  190. glEnd();
  191.  
  192. glFlush();
  193. }
  194.  
  195. void init() {
  196. glClearColor(1.0, 1.0, 1.0, 0.0);
  197. glMatrixMode(GL_PROJECTION);
  198. glLoadIdentity();
  199. gluOrtho2D(0.0, 1.0, 0.0, 1.0);
  200. }
  201.  
  202. int main(int argc, char** argv) {
  203. points = generatePoints();
  204. randomWalk.push_back({ dis(gen), dis(gen) });
  205.  
  206. glutInit(&argc, argv);
  207. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  208. glutInitWindowSize(800, 600);
  209. glutInitWindowPosition(100, 100);
  210. glutCreateWindow("Random Walk");
  211.  
  212. init();
  213. glutDisplayFunc(display);
  214. glutTimerFunc(10, update, 0);
  215. glutMainLoop();
  216.  
  217. return 0;
  218. }
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement