Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <cmath>
- #include <random>
- #include <chrono>
- #include <thread>
- #include "glut.h"
- const int N = 3;
- const int M = 15;
- const float R = 0.25;
- const int K = 1000000;
- std::random_device rd;
- std::mt19937 gen(rd());
- std::uniform_real_distribution<float> dis(0.0, 1.0);
- struct Point {
- float x;
- float y;
- };
- std::vector<Point> generatePoints() {
- std::vector<Point> points;
- for (int i = 0; i < N; ++i) {
- points.push_back({ dis(gen), dis(gen) });
- }
- return points;
- }
- Point nextPoint(Point current, const std::vector<Point>& points) {
- int roll = std::uniform_int_distribution<int>(1, M)(gen);
- int index = (roll - 1) / (M / N);
- Point next = points[index];
- float delta_x = next.x - current.x;
- float delta_y = next.y - current.y;
- float distance = sqrt(delta_x * delta_x + delta_y * delta_y);
- float ratio = R / distance;
- return { current.x + ratio * delta_x, current.y + ratio * delta_y };
- }
- std::vector<Point> computeRandomWalk(const std::vector<Point>& points) {
- std::vector<Point> randomWalk;
- Point current = { dis(gen), dis(gen) };
- for (int i = 0; i < K; ++i) {
- current = nextPoint(current, points);
- randomWalk.push_back(current);
- }
- return randomWalk;
- }
- std::vector<Point> points;
- std::vector<Point> randomWalk;
- int currentStep = 0;
- void update(int value) {
- if (currentStep < K) {
- randomWalk.push_back(nextPoint(randomWalk.back(), points));
- currentStep++;
- }
- glutPostRedisplay();
- glutTimerFunc(10, update, 0);
- }
- void display() {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0, 0.0, 0.0);
- glBegin(GL_POINTS);
- for (const auto& point : points) {
- glVertex2f(point.x, point.y);
- }
- glEnd();
- glColor3f(0.0, 0.0, 1.0);
- glBegin(GL_POINTS);
- for (size_t i = 0; i <= currentStep; ++i) {
- glVertex2f(randomWalk[i].x, randomWalk[i].y);
- }
- glEnd();
- glFlush();
- }
- void init() {
- glClearColor(1.0, 1.0, 1.0, 0.0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0, 1.0, 0.0, 1.0);
- }
- int main(int argc, char** argv) {
- points = generatePoints();
- randomWalk.push_back({ dis(gen), dis(gen) });
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(800, 600);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("Random Walk");
- init();
- glutDisplayFunc(display);
- glutTimerFunc(10, update, 0);
- glutMainLoop();
- return 0;
- }
- #include <vector>
- #include <cmath>
- #include <random>
- #include <chrono>
- #include <thread>
- #include "glut.h"
- const int N = 3;
- const int M = 15;
- const float R = 0.25;
- const int K = 1000000;
- std::random_device rd;
- std::mt19937 gen(rd());
- std::uniform_real_distribution<float> dis(0.0, 1.0);
- struct Point {
- float x;
- float y;
- };
- std::vector<Point> generatePoints() {
- std::vector<Point> points;
- for (int i = 0; i < N; ++i) {
- points.push_back({ dis(gen), dis(gen) });
- }
- return points;
- }
- Point nextPoint(Point current, const std::vector<Point>& points) {
- int roll = std::uniform_int_distribution<int>(1, M)(gen);
- int index = (roll - 1) / (M / N);
- Point next = points[index];
- float delta_x = next.x - current.x;
- float delta_y = next.y - current.y;
- float distance = sqrt(delta_x * delta_x + delta_y * delta_y);
- float ratio = R / distance;
- return { current.x + ratio * delta_x, current.y + ratio * delta_y };
- }
- std::vector<Point> computeRandomWalk(const std::vector<Point>& points) {
- std::vector<Point> randomWalk;
- Point current = { dis(gen), dis(gen) };
- for (int i = 0; i < K; ++i) {
- current = nextPoint(current, points);
- randomWalk.push_back(current);
- }
- return randomWalk;
- }
- std::vector<Point> points;
- std::vector<Point> randomWalk;
- int currentStep = 0;
- void update(int value) {
- if (currentStep < K) {
- randomWalk.push_back(nextPoint(randomWalk.back(), points));
- currentStep++;
- }
- glutPostRedisplay();
- glutTimerFunc(10, update, 0);
- }
- void display() {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0, 0.0, 0.0);
- glBegin(GL_POINTS);
- for (const auto& point : points) {
- glVertex2f(point.x, point.y);
- }
- glEnd();
- glColor3f(0.0, 0.0, 1.0);
- glBegin(GL_POINTS);
- for (size_t i = 0; i <= currentStep; ++i) {
- glVertex2f(randomWalk[i].x, randomWalk[i].y);
- }
- glEnd();
- glFlush();
- }
- void init() {
- glClearColor(1.0, 1.0, 1.0, 0.0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0, 1.0, 0.0, 1.0);
- }
- int main(int argc, char** argv) {
- points = generatePoints();
- randomWalk.push_back({ dis(gen), dis(gen) });
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(800, 600);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("Random Walk");
- init();
- glutDisplayFunc(display);
- glutTimerFunc(10, update, 0);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement