linear spline

This commit is contained in:
Frank DeMarco 2014-01-13 23:11:23 +09:00
parent 206caee387
commit 3a6000886a
1 changed files with 28 additions and 11 deletions

View File

@ -9,6 +9,7 @@ class Interpolator(dict, GameChild):
def read(self):
for name, value in self.get_configuration("interpolate").iteritems():
self[name] = Nodes(value)
self[name].print_checkpoints()
class Nodes(list):
@ -36,7 +37,11 @@ class Nodes(list):
self.set_cubic_splines()
def set_linear_splines(self):
pass
self.splines = splines = []
for ii in xrange(len(self) - 1):
x1, y1, x2, y2 = self[ii] + self[ii + 1]
m = float(y2 - y1) / (x2 - x1)
splines.append(LinearSpline(x1, y1, m))
def set_cubic_splines(self):
n = len(self) - 1
@ -65,8 +70,8 @@ class Nodes(list):
b[jj] = (a[jj + 1] - a[jj]) / h[jj] - \
(h[jj] * (c[jj + 1] + 2 * c[jj])) / 3
d[jj] = (c[jj + 1] - c[jj]) / (3 * h[jj])
self.splines = [Spline(a[ii], b[ii], c[ii], d[ii], self[ii].x) for \
ii in xrange(n)]
self.splines = [CubicSpline(self[ii].x, a[ii], b[ii], c[ii],
d[ii]) for ii in xrange(n)]
def get_y(self, t):
splines = self.splines
@ -79,6 +84,7 @@ class Nodes(list):
return "<%i, %s>" % (self.interpolation_method, list(self))
def print_checkpoints(self):
print self
start, end = int(self[0].x), int(self[-1].x)
step = (end - start) / 8
for t in xrange(start, end + step, step):
@ -100,20 +106,31 @@ class Node(list):
class Spline:
def __init__(self, a, b, c, d, x):
def __init__(self, x):
self.x = x
class CubicSpline(Spline):
def __init__(self, x, a, b, c, d):
Spline.__init__(self, x)
self.a = a
self.b = b
self.c = c
self.d = d
self.x = x
def __repr__(self):
x = self.x
return ("<%.1e + %.1e * (x - %.1e) + %.1e * (x - %.1e) ** 2 + " + \
"%.1e * (x - %.1e) ** 3>") % (self.a, self.b, x, self.c, x,
self.d, x)
def get_y(self, t):
x = self.x
return self.a + self.b * (t - x) + self.c * (t - x) ** 2 + self.d * \
(t - x) ** 3
class LinearSpline(Spline):
def __init__(self, x, y, m):
Spline.__init__(self, x)
self.y = y
self.m = m
def get_y(self, t):
return self.m * (t - self.x) + self.y