def diamond_square(m, x1, y1, x2, y2, r): """ Using standard python array. No numpy Now x and y are backwards, though. m = any empty 2d array x1, y1, x2, y2 = range on m to apply calculations r = value range """ i = ((m[x1][y1] + m[x1][y2] + m[x2][y1] + m[x2][y2]) // 4) + random.randint(-r,r) m[(x1+x2)//2] [(y1+y2)//2] = i # Center m[x1] [(y1+y2)//2] = i + random.randint(-r,r) # Up m[x2] [(y1+y2)//2] = i + random.randint(-r,r) # Left m[(x1+x2)//2] [y1] = i + random.randint(-r,r) # Right m[(x1+x2)//2] [y2] = i + random.randint(-r,r) # Down if x2-x1 > 1 and y2-y1 > 1: diamond_square(m, x1, y1, (x1+x2)//2, (y1+y2)//2, r//2) diamond_square(m, (x1+x2)//2, y1, x2, (y1+y2)//2, r//2) diamond_square(m, x1, (y1+y2)//2, (x1+x2)//2, y2, r//2) diamond_square(m, (x1+x2)//2, (y1+y2)//2, x2, y2, r//2)
Monday, February 21, 2011
Testing Code
Will this format the code?
Subscribe to:
Posts (Atom)