-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsquare_diamond_example.py
executable file
·96 lines (80 loc) · 2.83 KB
/
square_diamond_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
import random
from Tkinter import *
# global variables
# algorithm variables
iterations = 1
roughness = 1.0
# tk variables
canvas_width = 300
canvas_height = 300
canvas = 0
def draw_heightmap(c, map):
"""
Draw the double array given onto a canvas (using grayscale)
c - Tk Canvas to draw on
map - a 2d list of floats (0.0-1.0)
"""
# make sure we start from scratch
c.delete(ALL)
# make sure we fit the heightmap inside the canvas
scale_x = canvas_width / len(map[0])
scale_y = canvas_height / len(map)
for j in xrange(len(map)):
start_y = j * scale_y
end_y = start_y + scale_y
for i in xrange(len(map[0])):
start_x = i * scale_x
end_x = start_x + scale_x
# color computation
hex_color = hex(int(map[j][i] * 15))[2:] # hex, minus the '0x'
color = "#" + 3 * str(hex_color) # grayscale value, hence '3 *'
c.create_rectangle(start_x, start_y,
end_x, end_y,
fill = color, width = 0)
def initialize_map(s, seed):
"""
Create and initialize map of size 's' with seed value in the corners
s - size of the square map
seed - initial value to be place in the corners
"""
# initialize memory
map = [[0] * s for i in xrange(s)]
# set up seed values
map[ 0][ 0] = seed
map[ 0][-1] = seed
map[-1][ 0] = seed
map[-1][-1] = seed
return map
if __name__ == '__main__':
# set up window
root = Tk()
root.wm_title("Square-Diamond")
# add canvas (to draw lines on)
canvas = Canvas(root, width = canvas_width, height = canvas_height)
canvas.grid(row=0, columnspan = 2)
# add labels/scales for roughness and num iterations
roughness = DoubleVar()
roughness_label = Label(root, text = "Roughness: ")
roughness_label.grid(row = 1, column = 0, sticky = S)
roughness_scale = Scale(root, from_ = 0.0, to = 1.0, resolution = .05,
orient = HORIZONTAL, variable = roughness,
length = canvas_width - 100)
roughness_scale.set(1.0)
roughness_scale.grid(row = 1, column = 1)
iterations = IntVar()
iterations_label = Label(root, text = "Iterations: ")
iterations_label.grid(row = 3, column = 0, sticky = S)
iterations_scale = Scale(root, from_ = 0, to = 4,
orient = HORIZONTAL, variable = iterations,
length = canvas_width - 100)
iterations_scale.set(0)
iterations_scale.grid(row = 3, column = 1)
# add quit button
button = Button(root, text = "Quit", command = quit)
button.grid(row = 4, columnspan = 2)
# testing grid draw-er
map = initialize_map(17, .4)
draw_heightmap(canvas, map)
# start Tk main loop
root.mainloop()