-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
61 lines (50 loc) · 1.47 KB
/
utils.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
'''
See examples.py, constants.py, or main.py for configurable code.
'''
def clamp(value, min_value, max_value):
return min(max_value, max(min_value, value))
def smoothstep(min_edge, max_edge, value):
value = clamp((value-min_edge) / (max_edge-min_edge), 0, 1)
return value * value * (3 - 2 * value)
import random
def random_color():
''' generates a tuple with 3 components in the range [0,255] '''
return [
random.randint(0, 255)
for _ in range(3)
]
def toColor(V):
return tuple(
clamp(c, 0, 255)
for c
in (V.x, V.y, V.z)
)
class Traversable:
'''
a convenience class for classes that represent
any 2D space that allows iteration over the x-
y-axes and also every point.
the class should extent Traversable and provide min
and max x- and y- coordinates.
'''
def __init__(self, minx=0, miny=0, maxx=0, maxy=0):
self.minx = minx
self.maxx = maxx
self.miny = miny
self.maxy = maxy
def min_xcor(self):
return self.minx
def max_xcor(self):
return self.maxx
def min_ycor(self):
return self.miny
def max_ycor(self):
return self.maxy
def xcors(self):
return range(self.min_xcor(), self.max_xcor())
def ycors(self):
return range(self.min_ycor(), self.max_ycor())
def cors(self):
for y in self.ycors():
for x in self.xcors():
yield x, y