1

我正在尝试重新实现我不久前成功完成的一些事情,但我只是做得不太对..

我使用的分形高度图生成算法本质上是递归菱形正方形算法。它似乎可以正常运行,但生成的地图“不太正确”....似乎没有成功访问网格中的每个点来确定颜色,并且地图中存在残留的“结构”这似乎与网格的递归方式有关。我不确定问题究竟在哪里/如何产生我所看到的。

我到目前为止的代码是,

import matplotlib.pyplot as plt
import matplotlib.cm as cm
from math import sqrt
from collections import namedtuple
import random

Coord=namedtuple('Coord','x y')

class Grid(object):
    '''grid handedness, 0,0=topleft  max,max=bottomr right'''    

    def __init__(self,x,y):
        self.size_x=x
        self.size_y=y
        self.data=[ [0 for _ in xrange(x)] for _ in xrange(y) ]

    def _render_to_text(self):
        print '\n\n'
        for row in self.data:
            print [ int(n) for n in row ]

    def _render_to_colormap(self):
        plt.imshow(self.data, interpolation='nearest',cmap=cm.gist_rainbow)
        plt.show()

    def render(self):
        self._render_to_colormap()
        #self._render_to_text()

    def make(self,coordinate,value):
        self.data[coordinate.x][coordinate.y]=value

    def make_new(self,coordinate,value):
        if self.data[coordinate.x][coordinate.y]==0:
            self.make(coordinate,value)

    def get(self,coordinate):
        return self.data[coordinate.x][coordinate.y]

class FractalHeightmap(object):
    '''populates a 'grid' with a fractal heightmap'''
    def __init__(self,grid,rng_seed,roughness,
                 corner_seeds=[(0,100),(0,100),(0,100),(0,100)],
                 max_depth=3):
        self.grid=grid
        self.max_depth=max_depth
        self._set_initial_corners(corner_seeds)
        self.roughness=roughness
        self.generate_heightmap([Coord(0,0),
                                 Coord(self.grid.size_x-1,0),
                                 Coord(0,self.grid.size_y-1),
                                 Coord(self.grid.size_x-1,self.grid.size_y-1)],1
                                )

    def _set_initial_corners(self,corner_seeds):
        tl,tr,bl,br=corner_seeds
        seeds=[[tl,tr],[bl,br]]
        for x_idx,x in enumerate([0,self.grid.size_x-1]):
            for y_idx,y in enumerate([0,self.grid.size_y-1]):
                try:
                    minval,maxval=seeds[x_idx][y_idx]
                    val=minval+(random.random()*(maxval-minval))
                except ValueError:
                    val=seeds[x_idx][y_idx]
                self.grid.make_new(Coord(x,y),val)

    def generate_heightmap(self,corners,depth):
        '''corners = (Coord(),Coord(),Coord(),Coord() / tl/tr/bl/br'''
        if depth>self.max_depth: return

        #
        tl,tr,bl,br=corners
        center=Coord((tr.x-tl.x)/2,(br.y-tr.y)/2)

        #define edge center coordinates
        top_c=Coord(tl.x+((tr.x-tl.x)/2),tl.y)
        left_c=Coord(tl.x,tl.y+((bl.y-tl.y)/2))
        right_c=Coord(tr.x,tr.y+((br.y-tr.y)/2))
        bot_c=Coord(bl.x+((br.x-bl.x)/2),bl.y)

        #calc the center and edge_center heights
        avg=sum([self.grid.get(tl),
                self.grid.get(tr),
                self.grid.get(bl),
                self.grid.get(br)]
                )/4.0  ###NOTE, we can choose to use the current corners, the new edge-centers, or all
                #currenty we use the current corners
                #then do the edge centers
        offset=((random.random())-.5)*self.roughness 
        self.grid.make_new(center,avg+offset)

        #top_c
        avg=sum([self.grid.get(tl),
                self.grid.get(tr)]
                )/2.0
        offset=((random.random())-.5)*self.roughness
        self.grid.make_new(top_c,avg+offset)

        #left_c
        avg=sum([self.grid.get(tl),
                 self.grid.get(bl)]
                )/2.0
        offset=((random.random())-.5)*self.roughness
        self.grid.make_new(left_c,avg+offset)

        #right_c        
        avg=sum([self.grid.get(tr),
                 self.grid.get(br)]
                )/2.0
        offset=((random.random())-.5)*self.roughness
        self.grid.make_new(right_c,avg+offset)

        #bot_c
        avg=sum([self.grid.get(bl),
                 self.grid.get(br)]
                )/2.0
        offset=((random.random())-.5)*self.roughness
        self.grid.make_new(bot_c,avg+offset)

        self.generate_heightmap((tl,top_c,left_c,center),depth+1)
        self.generate_heightmap((top_c,tr,center,right_c),depth+1)
        self.generate_heightmap((left_c,center,bl,bot_c),depth+1)
        self.generate_heightmap((center,right_c,bot_c,br),depth+1)



if __name__ == '__main__':
    g_size=32 #//must be power of 2
    g=Grid(g_size+1,g_size+1)
    f=FractalHeightmap(g,1,10,max_depth=sqrt(g_size))
    g.render()

如果您按原样运行它,您应该看到颜色图并了解它为什么不正确,将深度更改为 2 的不同幂以不同的方式显示它 - 值 256 及以上需要一段时间才能生成

非常感谢任何帮助。

4

2 回答 2

3

很抱歉话题外,但我想分享另一种生成地形的好算法,在我意识到我不喜欢菱形和正方形后开始使用。这里是一个描述,这里是一个实现:

#/usr/bin/python
#coding=UTF-8

import random,math

class HillGrid:

    def __init__(self,KRADIUS =(1.0/5.0),ITER=200,SIZE=40):
        self.KRADIUS = KRADIUS
        self.ITER = ITER
        self.SIZE = SIZE

        self.grid = [[0 for x in range(self.SIZE)] for y in range(self.SIZE)]

        self.MAX = self.SIZE * self.KRADIUS
        for i in range(self.ITER):
            self.step()

    def dump(self):
        for ele in self.grid:
            s = ''
            for alo in ele:
                s += '%s ' % str(alo)
            print s

    def __getitem__(self,n):
        return self.grid[n]

    def step(self):
        point = [random.randint(0,self.SIZE-1),random.randint(0,self.SIZE-1)]
        radius = random.uniform(0,self.MAX)

        x2 = point[0]
        y2 = point[1]    

        for x in range(self.SIZE):
            for y in range(self.SIZE):

                z = (radius**2) - ( math.pow(x2-x,2) + math.pow(y2-y,2) )
                if z >= 0:
                    self.grid[x][y] += int(z)


if __name__ == '__main__':
    h = HillGrid(ITER=50,SIZE = 20)
    h.dump()
于 2014-07-19T23:55:32.887 回答
0

在逐步构建网格之后,我回答了我自己的问题。

中心点的计算不正确,

中心=坐标((tr.x-tl.x)/2,(br.y-tr.y)/2)

本来应该

中心=坐标(tl.x+((tr.x-tl.x)/2),tr.y+((br.y-tr.y)/2))

第一个版本忘记将子网格原点的 x/y 位置添加到计算中点坐标的结果中。

于 2014-07-19T19:01:50.660 回答