随机迷宫C++

在这里插入图片描述

#include <opencv2/opencv.hpp>
#include <math.h>
using namespace cv;

Size u_resolution(500, 500);
Point2f u_mouse;
float u_time;

float fract(float value) {
    return value - floor(value);
}

float dot(const float x, const float y, const Point2f& v2) {
    return v2.dot(Point2f(x,y));
}

float random(Point2f st) {
    return fract(sin(dot(st.x, st.y, Point2f(12.9898, 78.233))) * 43758.5453123);
}

Point2f truchetPattern(Point2f st, float index) {

    index = fract((index - 0.5) * 2.0);

    if (index > 0.75) {
        st = Point2f(1.0, 1.0) - st;
    }
    else if (index > 0.5) {
        st = Point2f(1.0 - st.x, st.y);
    }
    else if (index > 0.25) {
        st = Point2f(1.0 - st.x, st.y);
    }

    return st;
}
float smoothstep(float t1, float t2, float x) {
    // Scale, bias and saturate x to 0..1 range
    x = std::clamp((double)(x - t1) / (t2 - t1), 0.0, 1.0);
    // Evaluate polynomial
    return x * x * (3 - 2 * x);
}

int main() {

    Mat img(u_resolution, CV_32FC1);

    Point2f st;

    for (int i = 0; i < img.rows; i++) {
        for (int j = 0; j < img.cols; j++) {

            st.x = (float)j / u_resolution.width;
            st.y = (float)i / u_resolution.height;

            st *= 10.0;

            Point2i ipos = st;
            Point2f fpos = Point2f{ fract(st.x) ,fract(st.y)};

            Point2f tile = truchetPattern(fpos, random(ipos));

            float color = 0.0;
            color = smoothstep(tile.x - 0.3, tile.x, tile.y) -
                smoothstep(tile.x, tile.x + 0.3, tile.y);

            img.at<float>(i, j) = color;
        }
    }

    imshow("Truchet Pattern", img);
    waitKey(0);

    return 0;
}

在这里插入图片描述

void arcMaze() {

    Mat img(u_resolution, CV_32FC1);

    Point2f st;

    for (int i = 0; i < img.rows; i++) {
        for (int j = 0; j < img.cols; j++) {

            st.x = (float)j / u_resolution.width;
            st.y = (float)i / u_resolution.height;

            st *= 16;

            Point2i ipos = st;
            Point2f fpos = Point2f{ fract(st.x) ,fract(st.y) };

            Point2f tile = truchetPattern(fpos, random(ipos));

            float color = 0.0;
            float dis = std::sqrt(tile.x * tile.x + tile.y * tile.y);

            Point2f tile2 = tile - Point2f{1,1};
            float dis2 = std::sqrt(tile2.x * tile2.x + tile2.y * tile2.y);
            //color = smoothstep(0.45, 0.5, dis) - smoothstep(0.5, 0.55, dis) +
            //    smoothstep(0.45, 0.5, dis2) - smoothstep(0.5, 0.55, dis2);

            color = smoothstep(0.45, 0.49, dis) - smoothstep(0.51, 0.55, dis)+
                smoothstep(0.45, 0.49, dis2) - smoothstep(0.51, 0.55, dis2);

            img.at<float>(i, j) = color;
        }
    }

    imshow("Truchet Pattern", img);
    waitKey(0);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,生成迷宫游戏可以使用深度优先搜索(DFS)算法来实现。以下是一个简单的C++实现: ```c++ #include <iostream> #include <stack> #include <vector> #include <random> using namespace std; // 定义迷宫大小 const int ROW = 10; const int COL = 10; // 定义迷宫类 class Maze { public: Maze() : maze_(ROW, vector<int>(COL, 1)) {} // 生成迷宫 void generate() { // 随机数生成器 random_device rd; mt19937 gen(rd()); uniform_int_distribution<> dis(0, 3); // DFS生成迷宫 stack<pair<int, int>> s; s.push(make_pair(0, 0)); while (!s.empty()) { auto p = s.top(); s.pop(); int x = p.first, y = p.second; maze_[x][y] = 0; // 标记为已访问 int dirs[4] = {0, 1, 2, 3}; // 随机打乱方向 for (int i = 0; i < 4; ++i) { int pos = dis(gen) % 4; swap(dirs[i], dirs[pos]); } // 按照打乱后的方向遍历 for (int i = 0; i < 4; ++i) { int dx = 0, dy = 0; if (dirs[i] == 0 && x > 0 && maze_[x-1][y]) { dx = -1; } else if (dirs[i] == 1 && x < ROW-1 && maze_[x+1][y]) { dx = 1; } else if (dirs[i] == 2 && y > 0 && maze_[x][y-1]) { dy = -1; } else if (dirs[i] == 3 && y < COL-1 && maze_[x][y+1]) { dy = 1; } if (dx || dy) { s.push(make_pair(x+dx, y+dy)); } } } // 标记起点和终点 maze_[0][0] = 2; maze_[ROW-1][COL-1] = 3; } // 打印迷宫 void print() const { for (const auto& row : maze_) { for (int cell : row) { if (cell == 0) { cout << " "; } else if (cell == 1) { cout << "██"; } else if (cell == 2) { cout << "口"; } else if (cell == 3) { cout << "出"; } } cout << endl; } } private: vector<vector<int>> maze_; }; int main() { Maze maze; maze.generate(); maze.print(); return 0; } ``` 在这个实现中,我们使用了深度优先搜索算法来生成迷宫,同时使用随机数生成器来打乱方向,使得迷宫更加随机。我们使用一个二维数组来表示迷宫,其中0表示可以通过的通道,1表示墙,2表示起点,3表示终点。在打印迷宫时,我们使用空格表示通道,`██`表示墙,`口`表示起点,`出`表示终点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值