python绘制随机地形地图

👽发现宝藏

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。【点击进入巨牛的人工智能学习网站】。

当我们谈论计算机编程中的地图生成时,通常会想到游戏开发、仿真模拟或者数据可视化等领域。Python 作为一门功能强大的编程语言,在地图生成方面有着丰富的资源和库。本文将介绍如何使用 Python 中的一些工具和库来绘制随机地形地图。

准备工作

在开始之前,我们需要确保安装了 Python 和一些必要的库。这里我们将使用 matplotlib 库来绘制地图,以及 numpy 库来生成随机地形。你可以通过以下命令来安装这些库:

pip install matplotlib numpy

生成随机地形

首先,我们需要生成随机的地形数据。这里我们将使用 numpy 库中的随机数生成函数来生成一个二维数组,代表地形的高度。

import numpy as np

def generate_terrain(width, height, scale=20, octaves=6, persistence=0.5, lacunarity=2.0, seed=None):
    if seed is not None:
        np.random.seed(seed)
    
    terrain = np.zeros((height, width))
    for y in range(height):
        for x in range(width):
            amplitude = 1
            frequency = 1
            for o in range(octaves):
                sampleX = x / scale * frequency
                sampleY = y / scale * frequency

                noise = np.interp([sampleX], [0, 1], [-1, 1]) * 2 - 1
                terrain[y][x] += noise * amplitude

                amplitude *= persistence
                frequency *= lacunarity
    return terrain

这段代码使用了 Perlin 噪声算法来生成随机地形数据。通过调整参数,我们可以控制生成地形的复杂程度。

绘制地图

接下来,我们将使用 matplotlib 库来绘制生成的地形数据。

import matplotlib.pyplot as plt

def plot_terrain(terrain):
    plt.figure(figsize=(10, 5))
    plt.imshow(terrain, cmap='terrain', origin='lower')
    plt.colorbar(label='Elevation')
    plt.title('Terrain Map')
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.show()

# 生成地形数据
width = 100
height = 100
terrain = generate_terrain(width, height)

# 绘制地图
plot_terrain(terrain)

这段代码将生成的地形数据以热图的形式展示出来。你可以看到地形的高低起伏,颜色越亮代表海拔越高,颜色越暗代表海拔越低。

添加地形特征

除了简单的随机地形外,我们还可以添加一些地形特征,如山脉、河流等,使地图更加生动。

生成山脉

我们可以通过在地形中添加高度较高的区域来模拟山脉。

def add_mountains(terrain, num_mountains=5, mountain_height=10, seed=None):
    if seed is not None:
        np.random.seed(seed)
    
    height, width = terrain.shape
    for _ in range(num_mountains):
        peak_x = np.random.randint(0, width)
        peak_y = np.random.randint(0, height)
        radius = np.random.randint(10, 30)
        for y in range(height):
            for x in range(width):
                distance_to_peak = np.sqrt((x - peak_x)**2 + (y - peak_y)**2)
                if distance_to_peak < radius:
                    terrain[y][x] += mountain_height * np.exp(-distance_to_peak / (radius / 3))
生成河流

河流是地形中常见的地形特征之一,我们可以在地图中模拟出河流的路径。

def add_river(terrain, river_width=3, river_depth=5, seed=None):
    if seed is not None:
        np.random.seed(seed)
    
    height, width = terrain.shape
    start_x = np.random.randint(0, width)
    start_y = 0
    end_x = np.random.randint(0, width)
    end_y = height - 1
    
    current_x = start_x
    current_y = start_y
    
    while current_y < end_y:
        next_x = np.clip(current_x + np.random.randint(-1, 2), 0, width - 1)
        current_y += 1
        for x in range(max(0, next_x - river_width // 2), min(width, next_x + river_width // 2)):
            for y in range(max(0, current_y - river_width // 2), min(height, current_y + river_width // 2)):
                terrain[y][x] -= river_depth
        current_x = next_x

完整代码

将上述的地形特征生成函数与前文的地形生成函数结合起来,并进行绘图:

terrain = generate_terrain(width, height)
add_mountains(terrain)
add_river(terrain)

plot_terrain(terrain)

通过这些地形特征的添加,我们可以生成更加多样化和丰富的地形地图。这些地图不仅可以用于游戏开发中的世界地图生成,还可以用于模拟实验中的地理环境,或者作为数据可视化的一部分呈现地形信息。 Python 的强大库和灵活性使得地图生成变得轻而易举。

自定义地形特征

除了山脉和河流之外,我们还可以添加其他类型的地形特征,比如湖泊、峡谷等,来使地图更加多样化。

生成湖泊

湖泊是由于低洼地区积水而形成的地形特征,我们可以在地图中随机选择一些低洼的区域并将其填充成湖泊。

def add_lakes(terrain, num_lakes=3, lake_size=10, lake_depth=5, seed=None):
    if seed is not None:
        np.random.seed(seed)
    
    height, width = terrain.shape
    for _ in range(num_lakes):
        lake_x = np.random.randint(0, width)
        lake_y = np.random.randint(0, height)
        for y in range(max(0, lake_y - lake_size), min(height, lake_y + lake_size)):
            for x in range(max(0, lake_x - lake_size), min(width, lake_x + lake_size)):
                terrain[y][x] -= lake_depth
生成峡谷

峡谷是由于地质构造而形成的地形特征,我们可以模拟出峡谷两侧的陡峭山壁。

def add_canyons(terrain, num_canyons=2, canyon_width=5, canyon_depth=10, seed=None):
    if seed is not None:
        np.random.seed(seed)
    
    height, width = terrain.shape
    for _ in range(num_canyons):
        start_x = np.random.randint(0, width)
        start_y = np.random.randint(0, height)
        direction_x = np.random.choice([-1, 1])
        direction_y = np.random.choice([-1, 1])
        
        for step in range(canyon_width):
            current_x = start_x + step * direction_x
            current_y = start_y + step * direction_y
            for y in range(max(0, current_y - canyon_width), min(height, current_y + canyon_width)):
                for x in range(max(0, current_x - canyon_width), min(width, current_x + canyon_width)):
                    terrain[y][x] -= canyon_depth * (1 - step / canyon_width)

完整代码

将上述的地形特征生成函数与前文的地形生成函数结合起来,并进行绘图:

terrain = generate_terrain(width, height)
add_mountains(terrain)
add_river(terrain)
add_lakes(terrain)
add_canyons(terrain)

plot_terrain(terrain)

通过添加不同的地形特征,我们可以生成更加多样化和复杂的地形地图,从而满足不同应用场景下的需求。这些地形地图不仅可以提供视觉上的享受,还可以用于模拟实验、游戏开发、数据可视化等各种用途。 Python 的灵活性和丰富的库使得地图生成变得简单而有趣。

自定义地形特征

除了山脉和河流之外,我们还可以添加其他类型的地形特征,比如湖泊、峡谷等,来使地图更加多样化。

生成湖泊

湖泊是由于低洼地区积水而形成的地形特征,我们可以在地图中随机选择一些低洼的区域并将其填充成湖泊。

def add_lakes(terrain, num_lakes=3, lake_size=10, lake_depth=5, seed=None):
    if seed is not None:
        np.random.seed(seed)
    
    height, width = terrain.shape
    for _ in range(num_lakes):
        lake_x = np.random.randint(0, width)
        lake_y = np.random.randint(0, height)
        for y in range(max(0, lake_y - lake_size), min(height, lake_y + lake_size)):
            for x in range(max(0, lake_x - lake_size), min(width, lake_x + lake_size)):
                terrain[y][x] -= lake_depth
生成峡谷

峡谷是由于地质构造而形成的地形特征,我们可以模拟出峡谷两侧的陡峭山壁。

def add_canyons(terrain, num_canyons=2, canyon_width=5, canyon_depth=10, seed=None):
    if seed is not None:
        np.random.seed(seed)
    
    height, width = terrain.shape
    for _ in range(num_canyons):
        start_x = np.random.randint(0, width)
        start_y = np.random.randint(0, height)
        direction_x = np.random.choice([-1, 1])
        direction_y = np.random.choice([-1, 1])
        
        for step in range(canyon_width):
            current_x = start_x + step * direction_x
            current_y = start_y + step * direction_y
            for y in range(max(0, current_y - canyon_width), min(height, current_y + canyon_width)):
                for x in range(max(0, current_x - canyon_width), min(width, current_x + canyon_width)):
                    terrain[y][x] -= canyon_depth * (1 - step / canyon_width)

完整代码

将上述的地形特征生成函数与前文的地形生成函数结合起来,并进行绘图:

terrain = generate_terrain(width, height)
add_mountains(terrain)
add_river(terrain)
add_lakes(terrain)
add_canyons(terrain)

plot_terrain(terrain)

通过添加不同的地形特征,我们可以生成更加多样化和复杂的地形地图,从而满足不同应用场景下的需求。这些地形地图不仅可以提供视觉上的享受,还可以用于模拟实验、游戏开发、数据可视化等各种用途。 Python 的灵活性和丰富的库使得地图生成变得简单而有趣。

进一步优化地形生成算法

在前面的代码中,我们使用了简单的 Perlin 噪声算法来生成随机地形数据。虽然这种方法可以生成较为自然的地形,但在一些情况下可能会出现连续性不够好、地形过于平滑等问题。为了进一步优化地形生成的质量,我们可以考虑使用更复杂的地形生成算法,例如 Diamond-Square 算法。

Diamond-Square 算法是一种递归的地形生成算法,它通过不断地对方形区域进行分割和计算来生成地形。这种算法生成的地形具有更好的连续性和细节性,能够更好地模拟真实世界中的地形特征。

下面是一个简化版的 Diamond-Square 算法的实现示例:

def diamond_square_algorithm(size, roughness=0.5, seed=None):
    if seed is not None:
        np.random.seed(seed)
    
    terrain = np.zeros((size, size))
    step_size = size - 1

    # 初始化角点高度
    terrain[0][0] = np.random.uniform(0, 1)
    terrain[0][size - 1] = np.random.uniform(0, 1)
    terrain[size - 1][0] = np.random.uniform(0, 1)
    terrain[size - 1][size - 1] = np.random.uniform(0, 1)

    while step_size > 1:
        half_step = step_size // 2

        # Diamond 步骤
        for y in range(0, size - 1, step_size):
            for x in range(0, size - 1, step_size):
                average = (terrain[y][x] + terrain[y][x + step_size] + terrain[y + step_size][x] + terrain[y + step_size][x + step_size]) / 4
                terrain[y + half_step][x + half_step] = average + np.random.uniform(-roughness, roughness)

        # Square 步骤
        for y in range(0, size - 1, half_step):
            for x in range((y + half_step) % step_size, size - 1, step_size):
                average = 0
                count = 0
                if y - half_step >= 0:
                    average += terrain[y - half_step][x]
                    count += 1
                if y + half_step < size:
                    average += terrain[y + half_step][x]
                    count += 1
                if x - half_step >= 0:
                    average += terrain[y][x - half_step]
                    count += 1
                if x + half_step < size:
                    average += terrain[y][x + half_step]
                    count += 1
                terrain[y][x] = average / count + np.random.uniform(-roughness, roughness)

                if x == 0:
                    terrain[y][size - 1] = average / count + np.random.uniform(-roughness, roughness)
                if y == 0:
                    terrain[size - 1][x] = average / count + np.random.uniform(-roughness, roughness)

        step_size //= 2
        roughness /= 2

    return terrain

使用 Diamond-Square 算法生成地形

现在,我们可以使用 Diamond-Square 算法来生成地形,并绘制地图:

terrain = diamond_square_algorithm(size=128, roughness=0.7, seed=42)
plot_terrain(terrain)

通过 Diamond-Square 算法生成的地形具有更多的细节和更好的连续性,能够更好地模拟真实世界中的地形特征。这使得我们可以生成更加真实和多样化的地形地图,满足不同应用场景下的需求。

自定义地形特征

在生成地形之后,我们可以进一步增强地图的真实感和趣味性,通过添加自定义的地形特征,比如树木、建筑物等。

生成树木

树木是地图中常见的地形特征,我们可以随机在地图的某些位置生成树木,使得地图更加生动。

def add_trees(terrain, num_trees=50, min_height=0.3, max_height=0.8, seed=None):
    if seed is not None:
        np.random.seed(seed)
    
    height, width = terrain.shape
    for _ in range(num_trees):
        tree_x = np.random.randint(0, width)
        tree_y = np.random.randint(0, height)
        if terrain[tree_y][tree_x] > min_height:
            terrain[tree_y][tree_x] += np.random.uniform(0, max_height - min_height)
生成建筑物

建筑物是地图中的人工结构,可以通过随机在地图上生成一些方块状的结构来模拟建筑物的存在。

def add_buildings(terrain, num_buildings=10, min_height=0.5, max_height=0.9, building_size=5, seed=None):
    if seed is not None:
        np.random.seed(seed)
    
    height, width = terrain.shape
    for _ in range(num_buildings):
        building_x = np.random.randint(0, width - building_size)
        building_y = np.random.randint(0, height - building_size)
        building_height = np.random.uniform(min_height, max_height)
        terrain[building_y:building_y+building_size, building_x:building_x+building_size] += building_height

完整代码

将上述的地形特征生成函数与地形生成函数结合起来,并进行绘图:

terrain = diamond_square_algorithm(size=128, roughness=0.7, seed=42)
add_mountains(terrain)
add_river(terrain)
add_lakes(terrain)
add_canyons(terrain)
add_trees(terrain)
add_buildings(terrain)

plot_terrain(terrain)

通过添加树木、建筑物等自定义地形特征,我们可以使地图更加丰富多彩,增加了地图的真实感和趣味性。这样的地图不仅可以用于游戏开发中的场景设计,还可以用于模拟实验、教学演示等多种应用场景。

总结

总的来说,本文介绍了如何使用 Python 来生成随机地形地图,并通过添加不同的地形特征来增强地图的真实感和趣味性。首先,我们使用了 Perlin 噪声算法和 Diamond-Square 算法来生成随机地形数据,这些算法能够生成具有不同形状和复杂度的地形。然后,我们介绍了如何通过添加山脉、河流、湖泊、峡谷等地形特征来丰富地图内容,使地图更加多样化。接着,我们进一步讨论了如何添加自定义地形特征,比如树木、建筑物等,从而增强地图的视觉效果和趣味性。最后,通过综合运用这些技术,我们可以生成出各种形式的地形地图,满足不同应用场景下的需求,包括游戏开发、模拟实验、教学演示等。 Python 的丰富库和灵活性使得地图生成变得简单而有趣,同时也为我们提供了广阔的想象空间,可以创造出更加丰富多彩的地图作品。

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/570005.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

vue3 修改路由中的meta属性

有些时候可能需要在路由跳转前后修改meta里面的相关属性值&#xff0c;这个时候就需要使用钩子函数&#xff08;路由守卫&#xff09;&#xff0c;钩子函数有全局钩子&#xff0c;局部组件钩子函数以及路由配置里面的钩子函数 &#xff08;这些也叫路由守卫&#xff09; 1.全局…

python数字验证码自动识别

&#x1f47d;发现宝藏 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。【点击进入巨牛的人工智能学习网站】。 在网络上&#xff0c;许多网站和应用程序使用验证码&#xff08;Completely Automated Publ…

Ubuntu系统开机长

Ubuntu系统开机长 1. 检查开机自启动软件的所占时间2. 将耗时最高的禁止开机自启动 1. 检查开机自启动软件的所占时间 systemd-analyze blame2. 将耗时最高的禁止开机自启动 sudo systemctl disable networking.service这个耗时是有阈值的&#xff0c;一般大于15s的算&#x…

【数据结构与算法】8.二叉树的基本概念|前序遍历|中序遍历|后序遍历

&#x1f4da;博客主页&#xff1a;爱敲代码的小杨. ✨专栏&#xff1a;《Java SE语法》 | 《数据结构与算法》 | 《C生万物》 |《MySQL探索之旅》 |《Web世界探险家》 ❤️感谢大家点赞&#x1f44d;&#x1f3fb;收藏⭐评论✍&#x1f3fb;&#xff0c;您的三连就是我持续更…

vue封装请求、合并js、合并多个js

vue封装请求、合并js、合并多个js 作为一个后端开发&#xff0c;写前端时发现&#xff0c;每次导入api接口都会有一堆代码&#xff0c;像下面这样&#xff1a; import {footprintList, footprintDelete} from /api/userApi.js import {addressList} from /api/userApi.js impor…

设置Linux开发板开机自启动QT程序的报错解决办法

设置Linux开发板开机自启动QT程序报错解决办法 设置开发板开机自启动QT 打开 /etc/init.d/rsC 文件&#xff0c;添加以下内容 cd / ./my_start_run.shmy_start_run.sh 是自己编写的自启动脚本&#xff0c;内容例如下&#xff1a;(也可以将这些直接写到 /etc/init.d/rsC 文件…

【算法刷题 | 贪心算法02】4.24(摆动序列)

文章目录 3.摆动序列3.1题目3.2解法&#xff1a;贪心3.2.1贪心思路3.2.2代码实现 3.摆动序列 3.1题目 如果连续数字之间的差严格地在正数和负数之间交替&#xff0c;则数字序列称为 摆动序列 。 第一个差&#xff08;如果存在的话&#xff09;可能是正数或负数。仅有一个元素…

嵌入式总线协议基础教学

在嵌入式系统设计中&#xff0c;总线协议&#xff08;bus protocols&#xff09;扮演着至关重要的角色&#xff0c;它们定义了设备如何在共享通信路径上交换数据。 本文将介绍两种常见的嵌入式总线协议&#xff1a;IC&#xff08;Inter-Integrated Circuit&#xff09;和SPI&a…

BFS解决FloodFill算法:(Leetcode:733. 图像渲染)

题目链接&#xff1a;733. 图像渲染 - 力扣&#xff08;LeetCode&#xff09; 使用广度优先遍历算法解决该问题&#xff1a; 从初始位置开始搜索&#xff0c;初始位置符合条件就入栈&#xff0c;并修改初始位置值。初始位置出栈。 再从初始位置开始广度优先搜索&#xff08;…

IDM下载器安装cmd注册

一、下载注册 安装包去IDM官网下载最新的试用版即可 或者直达百度网盘下载&#xff08;担心被河蟹&#xff0c;放在txt中了&#xff09;包含IDM下载器安装包和注册软件 IDM下载器安装包和注册软件下载地址链接 https://download.csdn.net/download/qq_31237581/89215452 如果…

sscanf和scanf区别

sscandf 从字符串中提取数据 scanf 标准输入流读取数据 int num; sscanf("42", "%d", &num);float f; sscanf("3.14", "%f", &f);char str[20]; sscanf("Hello, World!", "%s", str);int a, b; sscanf(…

vue3 引入@tsparticles/vue3和@tsparticles/slim 实现粒子特效

1.安装&#xff1a; yarn add tsparticles/vue3 tsparticles/slim2.main.ts 引入 import Particles from "tsparticles/vue3"; import { loadSlim } from "tsparticles/slim";app.use(Particles as any, {init: async (engine: any) > {await loadSli…

POJO,Entity,model,domain,view,DTO,VO,Param这些分别都是什么含义?怎样理解?

目录 1. 前言 2. POJO的含义 3. entity(实体) 4. model(模型) 5. domain(域) 6. view(视图) 7. DTO(数据传输对象) 8. VO(真正视图层) 9. Param(参数) 10. 总结 1. 前言 在日常开发的过程中&#xff0c;如果我们接手一个新的项目之后&#xff0c;通常会有各种各样的…

edu邮箱官方购买渠道手把手选购指南记录

教育优惠&#xff0c;是一项针对于在校大学生和教职员工推出的特殊优惠活动。一些公司会将旗下产品或服务以一定的折扣&#xff0c;甚至免费提供给高校师生。想想自己上大学的时候啥都不知道,毕业后才发现浪费了这么多优秀的资源.如果你还是一名在校大学生&#xff0c;那么就不…

40. 【Android教程】AsyncTask:异步任务

在前面的章节有提到过&#xff0c;Android 系统默认会在主线程&#xff08;UI 线程&#xff09;执行任务&#xff0c;但是如果有耗时程序就会阻塞 UI 线程&#xff0c;导致页面卡顿。这时候我们通常会将耗时任务放在独立的线程&#xff0c;然后通过 Handler 等线程间通信机制完…

扁平与圆形Cat6网线:区别与选择指南

&#x1f335;在构建家庭或办公室网络时&#xff0c;选择合适的网线类型至关重要。Cat6网线因其高速传输性能而广受欢迎&#xff0c;但在购买时&#xff0c;您可能会发现市场上有两种不同形状的Cat6网线&#xff1a;扁平和圆形。本文将探讨这两种网线的区别&#xff0c;并提供选…

用html写一个旋转菜单

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>旋转菜单</title><link relstylesheet href"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"&…

百兆集成网络链接器911105A

百兆集成网络链接器&#xff08;有时也称为百兆网卡&#xff09;是一种硬件设备&#xff0c;主要用于计算机与计算机网络之间的高速数据传输。它的主要功能包括&#xff1a; 1. 高速数据传输&#xff1a;百兆集成网络链接器支持100Mbps的数据传输速率&#xff0c;比之前的以太…

河道采砂执法监管信息化平台:科技赋能,智慧监管

随着信息技术的飞速发展&#xff0c;信息化平台已经成为提升行业监管效率和水平的重要工具。河道采砂作为水利资源管理的重要环节&#xff0c;其执法监管同样需要与时俱进&#xff0c;利用先进技术手段提升监管效能。河道采砂执法监管信息化平台便是这一背景下的产物&#xff0…

某酒业集团数字化转型规划(169页附下载)

某酒业集团数字化转型项目实施方案建议书(P169).rar是一个极具参考价值的资料&#xff0c;它详细地阐述了如何利用数字化技术来推动企业转型。这份建议书以IBM的先进技术和某酒业集团的实际应用需求为基础&#xff0c;提出了一套全面、系统的数字化转型解决方案。该方案首先对某…