0

I'm in the process of creating a DS algorithm in python but I believe I'm having trouble correctly populating the Matrix. I'm getting 0's in every-other index. My code is as follows and a sample output is provided after. I'm using this to generate terrain data. From looking at other examples I believe that my indices should be closer together instead of hopping from (20,-8)

import random
class DiamondSq:

def __init__(self, k):
    self.size = (2 ** k) + 1
    self.max = self.size - 1
    self.r = 1
    self.grid = self.createGrid(self.size)
    self.diamondSquareAlgorithm(self.max)

def diamondSquareAlgorithm(self, size):

    x = size / 2
    y = size / 2
    halfStep = size / 2
    scale = self.r * size

    if (half < 1):
        return None

    # Squares
    for y in range(halfStep, self.max, size):
        for x in range(halfStep, self.max, size):
            s_scale = random.uniform(0, 1) * scale * 2 - scale
            self.squareStep(x, y, halfStep, s_scale)

    # Diamonds
    for y in range(0, self.max, halfStep):
        for x in range((y + halfStep) % size, self.max, size):
            d_scale = random.uniform(0, 1) * scale * 2 - scale
            self.diamondStep(x, y, halfStep, d_scale)

    self.diamondSquareAlgorithm(size / 2)

def squareStep(self, x, y, size, scale):

    topLeft = self.grid[x - size][y - size]
    topRight = self.grid[x + size][y - size]
    botLeft = self.grid[x + size][y + size]
    botRight = self.grid[x - size][y + size]

    average = ((topLeft + topRight + botLeft + botRight) / 4)
    self.grid[x][y] = average + scale

def diamondStep(self, x, y, size, scale):

    top = self.grid[x][y - size]
    right = self.grid[x + size][y]
    bottom = self.grid[x][y + size]
    left = self.grid[x - size][y + size]

    average = ((top + right + bottom + left) / 4)
    self.grid[x][y] = average + scale

def createGrid(self, size):

    grid = [[-1 for x in range(size)] for y in range(size)] 

    grid[0][0] = random.randint(50,70) #self.max
    grid[self.max][0] = random.randint(50,70) #self.max / 2
    grid[self.max][self.max] = random.randint(50,70) # 0
    grid[0][self.max] = random.randint(50,70) #self.max / 2


    for index, i in enumerate(grid):
        for index2, b in enumerate(i):
            result = '{0:.3g}'.format(list1[index][index2]*100)
            print str(result),
    print()
    return grid

def getGrid(self):
    return self.grid

OUTPUT

-3.5        0       0.692       0       1.03        0       0.818       0       -4.56       ()
0       12.4        0       19.6        0       17.2        0       18.9        0       
-4.84       0       1.69        0       -3.46       0       -1.86       0       -7.43       ()
0       25.7        0       18.7        0       20.8        0       22.3        0   
2.41        0       -4.43       0       -8.42       0       6.36        0       -0.151      ()
0       23.4        0       29.5        0       26.7        0       23.6        0   
-0.0232     0       8.08        0       5.07        0       -7.31       0       5.78
0       16.7        0       25.4        0       21.5        0       14.9        0
-5.65       0       -7.61       0       -5.09       0       0.282       0       2.49
Infiniti
  • 339
  • 1
  • 2
  • 7
  • too lazy to debug your code but here [Diamond-square algorithm not working](https://stackoverflow.com/a/36258843/2521214) is mine C++ iterative approach so you have something working to compare with. Also this [simple Island map generator with biomes](https://stackoverflow.com/a/36647622/2521214) might be interesting for you – Spektre Nov 10 '17 at 07:17
  • The code as posted won't run, because you have instances of global variables inside your class definition. For example when printing, you use `list1` and when deciding whether to recurse, you check for `half < 1`. When I use the local variable `halfStep`, I get reasonable results. (The right and bottom borders are not generated properly, but I don't get the gaps in your example.) – M Oehm Nov 10 '17 at 07:38

0 Answers0