te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>python - Why do most seeds result in the perlin noise become vertical lines? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Why do most seeds result in the perlin noise become vertical lines? - Stack Overflow

programmeradmin3浏览0评论

I'm trying my hand at perlin noise so that i could make dnd maps, but my python project seems to always come up with either perfectly vertical lines or noise that comes out extremely vertical.Typical generation Typical half generation


from asyncio import WindowsProactorEventLoopPolicy
import numpy as np
import matplotlib.pyplot as plt
from noise import pnoise2
import random
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

def update_plot():
    shuffle_and_plot()

def generate_perlin_map(width, height, scaling, octaves=6, persistence=0.6, lacunarity=5, threshold=0.7, seed=None):
    """Generates a Perlin noise heat map with thresholding and an optional random seed."""
    if seed is not None:
        random.seed(seed)
    
    noise_map = np.zeros((height, width))
    for y in range(height):
        for x in range(width):
            noise_map[y][x] = pnoise2(x / width, y / height, octaves=10, persistence=.8, lacunarity=2, base=seed)
    
    # Normalize values to range 0-1
    noise_map = (noise_map - noise_map.min()) / (noise_map.max() - noise_map.min())
    
    # Apply thresholding
    #noise_map = np.where(noise_map > threshold, .5, 0)
    return noise_map

def plot_heatmap(noise_map):
    """Displays the generated heat map."""
    plt.imshow(noise_map, cmap='gray', interpolation='nearest')
    plt.title("Perlin Noise Heat Map")
    plt.show()

def shuffle_and_plot():
    """Regenerates the noise map with a new seed and plots it."""
    global noise_map
    new_seed = random.randint(0, 10000)  # Randomize seed for different patterns
    noise_map = generate_perlin_map(width = 1000, height = 1000, scaling = .02, seed=new_seed)
    plot_heatmap(noise_map)

root = tk.Tk()
root.title("Perlin Noise Generator")
button = tk.Button(root, text="Shuffle", command=update_plot)
button.pack()
root.mainloop()

I have tried increasing and decreasing the width, height, octaves, persistence and lacunarity, as well as adding a scaling variable for the width and heights, but it always results in the vertical lines (at least on most seeds).

I'm trying my hand at perlin noise so that i could make dnd maps, but my python project seems to always come up with either perfectly vertical lines or noise that comes out extremely vertical.Typical generation Typical half generation


from asyncio import WindowsProactorEventLoopPolicy
import numpy as np
import matplotlib.pyplot as plt
from noise import pnoise2
import random
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

def update_plot():
    shuffle_and_plot()

def generate_perlin_map(width, height, scaling, octaves=6, persistence=0.6, lacunarity=5, threshold=0.7, seed=None):
    """Generates a Perlin noise heat map with thresholding and an optional random seed."""
    if seed is not None:
        random.seed(seed)
    
    noise_map = np.zeros((height, width))
    for y in range(height):
        for x in range(width):
            noise_map[y][x] = pnoise2(x / width, y / height, octaves=10, persistence=.8, lacunarity=2, base=seed)
    
    # Normalize values to range 0-1
    noise_map = (noise_map - noise_map.min()) / (noise_map.max() - noise_map.min())
    
    # Apply thresholding
    #noise_map = np.where(noise_map > threshold, .5, 0)
    return noise_map

def plot_heatmap(noise_map):
    """Displays the generated heat map."""
    plt.imshow(noise_map, cmap='gray', interpolation='nearest')
    plt.title("Perlin Noise Heat Map")
    plt.show()

def shuffle_and_plot():
    """Regenerates the noise map with a new seed and plots it."""
    global noise_map
    new_seed = random.randint(0, 10000)  # Randomize seed for different patterns
    noise_map = generate_perlin_map(width = 1000, height = 1000, scaling = .02, seed=new_seed)
    plot_heatmap(noise_map)

root = tk.Tk()
root.title("Perlin Noise Generator")
button = tk.Button(root, text="Shuffle", command=update_plot)
button.pack()
root.mainloop()

I have tried increasing and decreasing the width, height, octaves, persistence and lacunarity, as well as adding a scaling variable for the width and heights, but it always results in the vertical lines (at least on most seeds).

Share Improve this question asked Feb 18 at 0:17 AnonAnon 191 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You stepwidth of x / width with a width of 10000 is causing this problem, which is "too zoomed into the perlin noise". You are basically seeing just a part of a hill instead of the whole landscape.

Adding an additional scale factor helps to get into a good "perlin distance".

Here's a minimal example to get you going:

import numpy as np
import matplotlib.pyplot as plt
from noise import pnoise2

# vary this scale to change the "zoom factor"
scale = 10

n_y = 1000
n_x = 1000

perlin_map = np.zeros((n_x, n_y))

max_n = max(n_y, n_x)
for i in range(n_y):
    for j in range(n_x):
        perlin_map[i, j] = pnoise2(
            j / max_n * scale,
            i / max_n * scale,
        )
plt.imshow(perlin_map)
plt.show()
发布评论

评论列表(0)

  1. 暂无评论