-
Notifications
You must be signed in to change notification settings - Fork 2
/
mathutil.py
134 lines (105 loc) · 3.91 KB
/
mathutil.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import math
import bisect
def sign(v):
# v == 0: return 0
# v > 0 : return 1
# v < 0 : return -1
return 0 if v == 0 else (1 if v >= 0 else -1)
def lerp(a,b,t):
# linear interpolation between scalars a and b using t
return (1-t)*a + t*b
def clamp(x,a,b):
return min(max(x,a),b)
def step(a,t):
return 0 if t < a else 1
def smoothstep(a,b,t):
x = clamp((t-a)/(t-a),0,1)
return x*x*(3-2*x)
def smootherstep(a,b,t):
x = clamp((t-a)/(t-a),0,1)
return x * x * x * (x * (x * 6 - 15) + 10)
def dot(v,q):
return v[0]*q[0] + v[1]*q[1]
class ivec2(object):
def __init__(self, x=0, y=0):
self.x = int(x)
self.y = int(y)
def dot(self, q):
# dot product
return self.x*q.x + self.y*q.y
def squaredLength(self):
return self.dot(self)
def length(self):
return math.sqrt(self.squaredLength())
def abs(self):
return ivec2(abs(self.x),abs(self.y))
def get(self, i):
# get the i-th component
return self.x if i == 0 else self.y
def sign(self):
# vector-form of the sign function
return ivec2(sign(self.x), sign(self.y))
def muls(self, scalar):
return ivec2(self.x*scalar, self.y * scalar)
def mulv(self, v):
return ivec2(self.x*v.x, self.y * v.y)
def normalized(self):
# Return a unit vector based on this. return (0,0) if this vector is zero
l = self.length()
mul = 1.0/l if l > 0.0 else 1
return (self.x*mul,self.y*mul) # don't return ivec2 because it will be zero! (it's integer vector)
def __eq__(self,q):
return self.x==q.x and self.y==q.y
def __str__(self):
return "({0},{1})".format(self.x, self.y)
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return ivec2(x, y)
def __sub__(self, other):
x = self.x - other.x
y = self.y - other.y
return ivec2(x, y)
def __hash__(self):
return hash((self.x,self.y))
class SortedPoints(object):
"""
Store a list of ivec2 points, sorted by euclidean length, like starting from the origin and going outwards on a spiral
Also store the squared lengths for each of the sorted points, so that we can do efficient binary search
"""
def __init__(self, maxLos):
self.points = []
for y in range(-maxLos,maxLos+1):
for x in range(-maxLos,maxLos+1):
self.points.append(ivec2(x,y))
self.points.sort(key=lambda p: p.squaredLength())
self.keys = [p.squaredLength() for p in self.points]
def range(self, r_inner,r_outer):
r_inner_squared = r_inner*r_inner
r_outer_squared = r_outer*r_outer
i0 = bisect.bisect_left( self.keys, r_inner_squared)
i1 = bisect.bisect_right( self.keys, r_outer_squared)
return self.points[i0:i1]
# Slower version of above
#return [x for x in self.points if x.squaredLength() >= r_inner_squared and x.squaredLength() <= r_outer_squared]
class Map2D(object):
"""
2D array class, storing the data as a 1D list
"""
def __init__(self, w,h, default_value = None):
self.width = w
self.height = h
self.data = [default_value] * w*h
def linear_index(self, point):
return point.x+point.y*self.width
def get(self, point ):
assert( self.in_bounds(point))
return self.data[ self.linear_index(point)]
def set(self, point, value):
assert( self.in_bounds(point))
self.data[ self.linear_index(point)] = value
def add(self, point, value):
assert( self.in_bounds(point))
self.data[ self.linear_index(point)] += value
def in_bounds( self, point ):
return point.x >= 0 and point.x < self.width and point.y >= 0 and point.y < self.height