• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Oct 17th, 2019 154 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def square(surface, x, y, edge_length, displacement_statement):
2.     displacement = eval(displacement_statement)
3.     height_sum = 0
4.     for dx in [-edge_length // 2, edge_length // 2]:
5.         for dy in [-edge_length // 2, edge_length // 2]:
6.             height_sum += surface[x + dx, y + dy]
7.     height_average = height_sum / 4
8.     surface[x, y] = height_average + displacement
9.
10.
11. def diamond(surface, x, y, edge_length, displacement_statement):
12.     dimension = len(surface)
13.     displacement = eval(displacement_statement)
14.     height_sum = 0
15.     points_count = 0
16.     for dx in [-edge_length // 2, 0, edge_length // 2]:
17.         for dy in [-edge_length // 2, 0, edge_length // 2]:
18.             if (dx == 0 or dy == 0) and not (dx == 0 and dy == 0):
19.                 if 0 <= x + dx < dimension and 0 <= y + dy < dimension:
20.                     height_sum += surface[x + dx, y + dy]
21.                     points_count += 1
22.                 elif not 0 <= x + dx < dimension:
23.                     height_sum -= surface[x - dx, y + dy]
24.                     points_count -= 1
25.                 else:
26.                     height_sum -= surface[x + dx, y - dy]
27.                     points_count -= 1
28.     height_average = height_sum / points_count
29.     surface[x, y] = height_average + displacement
30.
31.
32. def main():
33.     surfaces = []
34.     dimension = 2**dimension_exponent + 1
35.     surface = numpy.zeros((dimension, dimension))
36.     surface[0, 0] = bottom_left
37.     surface[dimension - 1, 0] = bottom_right
38.     surface[0, dimension - 1] = top_left
39.     surface[dimension - 1, dimension - 1] = top_right
40.     edge_length = dimension - 1
41.     surfaces.append(surface[::edge_length, ::edge_length].copy())
42.     while edge_length > 1:
43.         # Square step
44.         for x in range(edge_length // 2, dimension - 1, edge_length):
45.             for y in range(edge_length // 2, dimension - 1, edge_length):
46.                 square(surface, x, y, edge_length, displacement_statement)
47.         # Diamond step
48.         for x in range(edge_length // 2, dimension - 1, edge_length):
49.             for y in range(edge_length // 2, dimension - 1, edge_length):
50.                 diamond(surface, x - edge_length //2, y, edge_length, displacement_statement)
51.                 diamond(surface, x, y - edge_length // 2, edge_length, displacement_statement)
52.         for x in range(edge_length // 2, dimension - 1, edge_length):
53.             diamond(surface, x, dimension - 1, edge_length, displacement_statement)
54.         for y in range(edge_length // 2, dimension - 1, edge_length):
55.             diamond(surface, dimension - 1, y, edge_length, displacement_statement)
56.         surfaces.append(surface[::(edge_length // 2), ::(edge_length // 2)].copy())
57.         edge_length = edge_length // 2
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!

Top