以下的噪音地形生成程序生成的图片保存在C盘中

Perlin噪音

#include <iostream>
#include <vector>
#include <cmath>
#include <random>
#include <fstream>
#include <algorithm> // for std::min and std::max

// Vec2结构体
struct Vec2 {
    float x, y;
    Vec2() : x(0), y(0) {}
    Vec2(float x, float y) : x(x), y(y) {}
};

// 生成随机梯度向量
std::vector<Vec2> generateGradients(int width, int height) {
    std::vector<Vec2> gradients(width * height);
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<float> dist(0.0f, 1.0f);

    for (int i = 0; i < width * height; ++i) {
        float angle = dist(gen) * 2 * M_PI;
        gradients[i] = Vec2(std::cos(angle), std::sin(angle));
    }
    return gradients;
}

// 点积
float dot(const Vec2& a, const Vec2& b) {
    return a.x * b.x + a.y * b.y;
}

// 插值
float lerp(float a, float b, float t) {
    return a + t * (b - a);
}

// Perlin噪音
float perlin(float x, float y, const std::vector<Vec2>& gradients, int width, int height) {
    int x0 = static_cast<int>(x) % width;
    int x1 = (x0 + 1) % width;
    int y0 = static_cast<int>(y) % height;
    int y1 = (y0 + 1) % height;

    float sx = x - static_cast<int>(x);
    float sy = y - static_cast<int>(y);

    Vec2 g00 = gradients[y0 * width + x0];
    Vec2 g01 = gradients[y1 * width + x0];
    Vec2 g10 = gradients[y0 * width + x1];
    Vec2 g11 = gradients[y1 * width + x1];

    float n00 = dot(g00, Vec2(sx, sy));
    float n01 = dot(g01, Vec2(sx, sy - 1));
    float n10 = dot(g10, Vec2(sx - 1, sy));
    float n11 = dot(g11, Vec2(sx - 1, sy - 1));

    float u = sx * sx * (3 - 2 * sx);
    float v = sy * sy * (3 - 2 * sy);

    float nx0 = lerp(n00, n10, u);
    float nx1 = lerp(n01, n11, u);
    return lerp(nx0, nx1, v);
}

// 分形噪音(多层Perlin噪音)
float fractalPerlin(float x, float y, const std::vector<Vec2>& gradients, int width, int height, int octaves, float persistence) {
    float total = 0.0f;
    float frequency = 1.0f;
    float amplitude = 1.0f;
    float maxValue = 0.0f; // 用于归一化

    for (int i = 0; i < octaves; ++i) {
        total += perlin(x * frequency, y * frequency, gradients, width, height) * amplitude;
        maxValue += amplitude;
        amplitude *= persistence;
        frequency *= 2.0f;
    }

    return total / maxValue; // 归一化
}

// 生成地形
std::vector<float> generateTerrain(int width, int height, float scale, const std::vector<Vec2>& gradients, int octaves, float persistence) {
    std::vector<float> terrain(width * height);
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            float nx = x / scale;
            float ny = y / scale;
            terrain[y * width + x] = fractalPerlin(nx, ny, gradients, width, height, octaves, persistence);
        }
    }
    return terrain;
}

// 保存为PGM图像
void saveTerrainAsPGM(const std::vector<float>& terrain, int width, int height, const std::string& filename) {
    std::ofstream file(filename);
    file << "P2\n" << width << " " << height << "\n255\n";
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            float value = terrain[y * width + x];
            value = (value + 1) * 127.5f; // 将值映射到0-255
            value = std::min(std::max(value, 0.0f), 255.0f); // 限制范围
            file << static_cast<int>(value) << " ";
        }
        file << "\n";
    }
}

int main() {
    int width = 1024;      // 增加分辨率
    int height = 1024;
    float scale = 32.0f;   // 调整噪音尺度
    int octaves = 10;       // 噪音层数
    float persistence = 0.5f; // 每层的振幅衰减系数

    // 生成梯度向量
    std::vector<Vec2> gradients = generateGradients(width, height);

    // 生成地形
    std::vector<float> terrain = generateTerrain(width, height, scale, gradients, octaves, persistence);

    // 保存为PGM图像
    saveTerrainAsPGM(terrain, width, height, "C:/terrain2.pgm");

    std::cout << "Terrain saved to terrain.pgm" << std::endl;

    return 0;
}

Simplex噪音(Perlin噪音改进版)

#include <iostream>
#include <vector>
#include <cmath>
#include <random>
#include <fstream>
#include <algorithm>

// Simplex噪音的梯度向量
struct Vec2 {
    float x, y;
    Vec2() : x(0), y(0) {}
    Vec2(float x, float y) : x(x), y(y) {}
};

// 梯度向量表
std::vector<Vec2> gradients = {
    {1, 1}, {-1, 1}, {1, -1}, {-1, -1},
    {1, 0}, {-1, 0}, {0, 1}, {0, -1}
};

// 随机排列表
std::vector<int> perm;

// 初始化随机排列表
void initPermutationTable(int seed) {
    perm.resize(256);
    for (int i = 0; i < 256; ++i) {
        perm[i] = i;
    }
    std::shuffle(perm.begin(), perm.end(), std::default_random_engine(seed));
    perm.insert(perm.end(), perm.begin(), perm.end()); // 扩展到512
}

// 点积
float dot(const Vec2& a, const Vec2& b) {
    return a.x * b.x + a.y * b.y;
}

// Simplex噪音的偏斜因子
const float F2 = 0.5f * (std::sqrt(3.0f) - 1.0f);
const float G2 = (3.0f - std::sqrt(3.0f)) / 6.0f;

// 2D Simplex噪音
float simplexNoise(float x, float y) {
    // 偏斜输入空间到Simplex网格
    float s = (x + y) * F2;
    int i = static_cast<int>(x + s);
    int j = static_cast<int>(y + s);

    float t = (i + j) * G2;
    float x0 = x - (i - t);
    float y0 = y - (j - t);

    // 确定Simplex三角形的其他两个顶点
    int i1, j1;
    if (x0 > y0) {
        i1 = 1;
        j1 = 0;
    } else {
        i1 = 0;
        j1 = 1;
    }

    float x1 = x0 - i1 + G2;
    float y1 = y0 - j1 + G2;
    float x2 = x0 - 1.0f + 2.0f * G2;
    float y2 = y0 - 1.0f + 2.0f * G2;

    // 计算梯度索引
    int ii = i & 255;
    int jj = j & 255;
    int gi0 = perm[ii + perm[jj]] % 8;
    int gi1 = perm[ii + i1 + perm[jj + j1]] % 8;
    int gi2 = perm[ii + 1 + perm[jj + 1]] % 8;

    // 计算贡献值
    float n0, n1, n2;
    float t0 = 0.5f - x0 * x0 - y0 * y0;
    if (t0 < 0) {
        n0 = 0.0f;
    } else {
        t0 *= t0;
        n0 = t0 * t0 * dot(gradients[gi0], Vec2(x0, y0));
    }

    float t1 = 0.5f - x1 * x1 - y1 * y1;
    if (t1 < 0) {
        n1 = 0.0f;
    } else {
        t1 *= t1;
        n1 = t1 * t1 * dot(gradients[gi1], Vec2(x1, y1));
    }

    float t2 = 0.5f - x2 * x2 - y2 * y2;
    if (t2 < 0) {
        n2 = 0.0f;
    } else {
        t2 *= t2;
        n2 = t2 * t2 * dot(gradients[gi2], Vec2(x2, y2));
    }

    // 合并贡献值
    return 70.0f * (n0 + n1 + n2);
}

// 分形噪音(多层Simplex噪音)
float fractalSimplexNoise(float x, float y, int octaves, float persistence) {
    float total = 0.0f;
    float frequency = 1.0f;
    float amplitude = 1.0f;
    float maxValue = 0.0f; // 用于归一化

    for (int i = 0; i < octaves; ++i) {
        total += simplexNoise(x * frequency, y * frequency) * amplitude;
        maxValue += amplitude;
        amplitude *= persistence;
        frequency *= 2.0f;
    }

    return total / maxValue; // 归一化
}

// 生成地形
std::vector<float> generateTerrain(int width, int height, float scale, int octaves, float persistence) {
    std::vector<float> terrain(width * height);
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            float nx = x / scale;
            float ny = y / scale;
            terrain[y * width + x] = fractalSimplexNoise(nx, ny, octaves, persistence);
        }
    }
    return terrain;
}

// 保存为PGM图像
void saveTerrainAsPGM(const std::vector<float>& terrain, int width, int height, const std::string& filename) {
    std::ofstream file(filename);
    file << "P2\n" << width << " " << height << "\n255\n";
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            float value = terrain[y * width + x];
            value = (value + 1) * 127.5f; // 将值映射到0-255
            value = std::min(std::max(value, 0.0f), 255.0f); // 限制范围
            file << static_cast<int>(value) << " ";
        }
        file << "\n";
    }
}

int main() {
    int width = 1024;      // 地图宽度
    int height = 1024;     // 地图高度
    float scale = 64.0f;   // 噪音尺度
    int octaves = 25;       // 噪音层数
    float persistence = 0.5f; // 每层的振幅衰减系数

    // 初始化随机排列表
    initPermutationTable(42); // 使用固定种子以确保可重复性

    // 生成地形
    std::vector<float> terrain = generateTerrain(width, height, scale, octaves, persistence);

    // 保存为PGM图像
    saveTerrainAsPGM(terrain, width, height, "C:/simplex_terrain.pgm");

    std::cout << "Simplex terrain saved to simplex_terrain.pgm" << std::endl;

    return 0;
}

3DSimplex噪音(注意:程序只能生成切片)

#include <iostream>
#include <vector>
#include <cmath>
#include <random>
#include <fstream>
#include <algorithm>

// 3D向量结构体
struct Vec3 {
    float x, y, z;
    Vec3() : x(0), y(0), z(0) {}
    Vec3(float x, float y, float z) : x(x), y(y), z(z) {}
};

// 3D梯度向量表
std::vector<Vec3> gradients3D = {
    {1, 1, 0}, {-1, 1, 0}, {1, -1, 0}, {-1, -1, 0},
    {1, 0, 1}, {-1, 0, 1}, {1, 0, -1}, {-1, 0, -1},
    {0, 1, 1}, {0, -1, 1}, {0, 1, -1}, {0, -1, -1},
    {1, 1, 0}, {-1, 1, 0}, {0, -1, 1}, {0, -1, -1}
};

// 随机排列表
std::vector<int> perm;

// 初始化随机排列表
void initPermutationTable(int seed) {
    perm.resize(256);
    for (int i = 0; i < 256; ++i) {
        perm[i] = i;
    }
    std::shuffle(perm.begin(), perm.end(), std::default_random_engine(seed));
    perm.insert(perm.end(), perm.begin(), perm.end()); // 扩展到512
}

// 点积
float dot(const Vec3& a, const Vec3& b) {
    return a.x * b.x + a.y * b.y + a.z * b.z;
}

// 3D Simplex噪音的偏斜因子
const float F3 = 1.0f / 3.0f;
const float G3 = 1.0f / 6.0f;

// 3D Simplex噪音
float simplexNoise3D(float x, float y, float z) {
    // 偏斜输入空间到Simplex网格
    float s = (x + y + z) * F3;
    int i = static_cast<int>(x + s);
    int j = static_cast<int>(y + s);
    int k = static_cast<int>(z + s);

    float t = (i + j + k) * G3;
    float x0 = x - (i - t);
    float y0 = y - (j - t);
    float z0 = z - (k - t);

    // 确定Simplex四面体的其他三个顶点
    int i1, j1, k1, i2, j2, k2;
    if (x0 >= y0) {
        if (y0 >= z0) {
            i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 1; k2 = 0;
        } else if (x0 >= z0) {
            i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 0; k2 = 1;
        } else {
            i1 = 0; j1 = 0; k1 = 1; i2 = 1; j2 = 0; k2 = 1;
        }
    } else {
        if (y0 < z0) {
            i1 = 0; j1 = 0; k1 = 1; i2 = 0; j2 = 1; k2 = 1;
        } else if (x0 < z0) {
            i1 = 0; j1 = 1; k1 = 0; i2 = 0; j2 = 1; k2 = 1;
        } else {
            i1 = 0; j1 = 1; k1 = 0; i2 = 1; j2 = 1; k2 = 0;
        }
    }

    float x1 = x0 - i1 + G3;
    float y1 = y0 - j1 + G3;
    float z1 = z0 - k1 + G3;
    float x2 = x0 - i2 + 2.0f * G3;
    float y2 = y0 - j2 + 2.0f * G3;
    float z2 = z0 - k2 + 2.0f * G3;
    float x3 = x0 - 1.0f + 3.0f * G3;
    float y3 = y0 - 1.0f + 3.0f * G3;
    float z3 = z0 - 1.0f + 3.0f * G3;

    // 计算梯度索引
    int ii = i & 255;
    int jj = j & 255;
    int kk = k & 255;
    int gi0 = perm[ii + perm[jj + perm[kk]] % 12];
    int gi1 = perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]] % 12;
    int gi2 = perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]] % 12;
    int gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]] % 12;

    // 计算贡献值
    float n0, n1, n2, n3;
    float t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0;
    if (t0 < 0) {
        n0 = 0.0f;
    } else {
        t0 *= t0;
        n0 = t0 * t0 * dot(gradients3D[gi0], Vec3(x0, y0, z0));
    }

    float t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1;
    if (t1 < 0) {
        n1 = 0.0f;
    } else {
        t1 *= t1;
        n1 = t1 * t1 * dot(gradients3D[gi1], Vec3(x1, y1, z1));
    }

    float t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2;
    if (t2 < 0) {
        n2 = 0.0f;
    } else {
        t2 *= t2;
        n2 = t2 * t2 * dot(gradients3D[gi2], Vec3(x2, y2, z2));
    }

    float t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3;
    if (t3 < 0) {
        n3 = 0.0f;
    } else {
        t3 *= t3;
        n3 = t3 * t3 * dot(gradients3D[gi3], Vec3(x3, y3, z3));
    }

    // 合并贡献值
    return 32.0f * (n0 + n1 + n2 + n3);
}

// 分形噪音(多层3D Simplex噪音)
float fractalSimplexNoise3D(float x, float y, float z, int octaves, float persistence) {
    float total = 0.0f;
    float frequency = 1.0f;
    float amplitude = 1.0f;
    float maxValue = 0.0f; // 用于归一化

    for (int i = 0; i < octaves; ++i) {
        total += simplexNoise3D(x * frequency, y * frequency, z * frequency) * amplitude;
        maxValue += amplitude;
        amplitude *= persistence;
        frequency *= 2.0f;
    }

    return total / maxValue; // 归一化
}

// 生成三维地形并保存为PGM切片
void generateAndSave3DTerrain(int width, int height, int depth, float scale, int octaves, float persistence) {
    std::vector<std::vector<std::vector<float>>> terrain(width, std::vector<std::vector<float>>(height, std::vector<float>(depth)));

    for (int z = 0; z < depth; ++z) {
        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                float nx = x / scale;
                float ny = y / scale;
                float nz = z / scale;
                terrain[x][y][z] = fractalSimplexNoise3D(nx, ny, nz, octaves, persistence);
            }
        }
    }

    // 保存每个Z层的切片为PGM图像
    for (int z = 0; z < depth; ++z) {
        std::string filename = "C:/terrain_slice_z" + std::to_string(z) + ".pgm";
        std::ofstream file(filename);
        file << "P2\n" << width << " " << height << "\n255\n";
        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                float value = terrain[x][y][z];
                value = (value + 1) * 127.5f; // 将值映射到0-255
                value = std::min(std::max(value, 0.0f), 255.0f); // 限制范围
                file << static_cast<int>(value) << " ";
            }
            file << "\n";
        }
    }
}

int main() {
    int width = 256;      // 地图宽度
    int height = 256;     // 地图高度
    int depth = 256;      // 地图深度
    float scale = 64.0f;  // 噪音尺度
    int octaves = 6;      // 噪音层数
    float persistence = 0.5f; // 每层的振幅衰减系数

    // 初始化随机排列表
    initPermutationTable(42); // 使用固定种子以确保可重复性

    // 生成并保存三维地形
    generateAndSave3DTerrain(width, height, depth, scale, octaves, persistence);

    std::cout << "3D Simplex terrain slices saved as PGM images." << std::endl;

    return 0;
}

使用Simplex噪声生成二维高度图

#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#define int long long
using namespace std;

// Simplex噪声的梯度表
static const int grad3[12][3] = {
    {1, 1, 0}, {-1, 1, 0}, {1, -1, 0}, {-1, -1, 0},
    {1, 0, 1}, {-1, 0, 1}, {1, 0, -1}, {-1, 0, -1},
    {0, 1, 1}, {0, -1, 1}, {0, 1, -1}, {0, -1, -1}
};

// Simplex噪声的置换表
static const int perm[512] = {
    151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142,
    8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117,
    35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71,
    134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41,
    55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89,
    18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226,
    250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182,
    189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43,
    172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97,
    228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107,
    49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138,
    236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,
    // 重复一遍
    151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142,
    8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117,
    35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71,
    134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41,
    55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89,
    18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226,
    250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182,
    189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43,
    172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97,
    228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107,
    49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138,
    236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180
};

// 计算梯度点积
double dot(const int* g, double x, double y) {
    return g[0] * x + g[1] * y;
}

// Simplex噪声核心函数
double simplexNoise(double xin, double yin) {
    double n0, n1, n2; // 三个角的噪声贡献

    // 倾斜输入空间以确定我们所在的单纯形单元
    double F2 = 0.5 * (sqrt(3.0) - 1.0);
    double s = (xin + yin) * F2;
    int i = floor(xin + s);
    int j = floor(yin + s);

    double G2 = (3.0 - sqrt(3.0)) / 6.0;
    double t = (i + j) * G2;
    double X0 = i - t; // 将单元原点反倾斜回 (x,y) 空间
    double Y0 = j - t;
    double x0 = xin - X0; // 从单元原点到 (x,y) 的距离
    double y0 = yin - Y0;

    // 确定我们在哪个单纯形中
    int i1, j1;
    if (x0 > y0) { i1 = 1; j1 = 0; } // 下三角形,XY 顺序
    else { i1 = 0; j1 = 1; } // 上三角形,YX 顺序

    // 计算中间角和最后一个角的偏移量
    double x1 = x0 - i1 + G2;
    double y1 = y0 - j1 + G2;
    double x2 = x0 - 1.0 + 2.0 * G2;
    double y2 = y0 - 1.0 + 2.0 * G2;

    // 计算三个角的哈希梯度索引
    int ii = i & 255;
    int jj = j & 255;
    int gi0 = perm[ii + perm[jj]] % 12;
    int gi1 = perm[ii + i1 + perm[jj + j1]] % 12;
    int gi2 = perm[ii + 1 + perm[jj + 1]] % 12;

    // 计算三个角的贡献
    double t0 = 0.5 - x0 * x0 - y0 * y0;
    if (t0 < 0) n0 = 0.0;
    else {
        t0 *= t0;
        n0 = t0 * t0 * dot(grad3[gi0], x0, y0);
    }

    double t1 = 0.5 - x1 * x1 - y1 * y1;
    if (t1 < 0) n1 = 0.0;
    else {
        t1 *= t1;
        n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
    }

    double t2 = 0.5 - x2 * x2 - y2 * y2;
    if (t2 < 0) n2 = 0.0;
    else {
        t2 *= t2;
        n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
    }

    // 将三个角的贡献相加,得到最终的噪声值
    return 70.0 * (n0 + n1 + n2);
}

// 生成二维高度图
vector<vector<double>> generateHeightMap(int width, int height, double scale) {
    vector<vector<double>> heightMap(height, vector<double>(width));

    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            double nx = x / scale;
            double ny = y / scale;
            heightMap[y][x] = simplexNoise(nx, ny);
        }
    }

    return heightMap;
}

// 主函数
signed main() {
    int width = 256;
    int height = 256;
    double scale = 32.0;

    vector<vector<double>> heightMap = generateHeightMap(width, height, scale);

    // 将高度图保存到 C 盘文件
    ofstream outFile("C:\\height_map.txt");
    if (outFile.is_open()) {
        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                outFile << heightMap[y][x] << " ";
            }
            outFile << endl;
        }
        outFile.close();
        cout << "高度图已保存到 C:\\height_map.txt" << endl;
    }else{
        cerr << "无法打开文件!" << endl;
    }
    return 0;
}