Mercurial > touhou
comparison pytouhou/utils/interpolator.py @ 60:af7914413b89
Enable interpolators to use different formulas
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Wed, 24 Aug 2011 21:12:45 +0200 |
parents | 694f25881d0f |
children | 1f591adcea04 |
comparison
equal
deleted
inserted
replaced
59:4fe37a620b22 | 60:af7914413b89 |
---|---|
12 ## GNU General Public License for more details. | 12 ## GNU General Public License for more details. |
13 ## | 13 ## |
14 | 14 |
15 | 15 |
16 class Interpolator(object): | 16 class Interpolator(object): |
17 def __init__(self, values=()): | 17 def __init__(self, values=(), formula=None): |
18 self.values = tuple(values) | 18 self.values = tuple(values) |
19 self.start_values = tuple(values) | 19 self.start_values = tuple(values) |
20 self.end_values = tuple(values) | 20 self.end_values = tuple(values) |
21 self.start_frame = 0 | 21 self.start_frame = 0 |
22 self.end_frame = 0 | 22 self.end_frame = 0 |
23 self._frame = 0 | 23 self._frame = 0 |
24 self._formula = formula or (lambda x: x) | |
24 | 25 |
25 | 26 |
26 def __nonzero__(self): | 27 def __nonzero__(self): |
27 return self._frame <= self.end_frame | 28 return self._frame < self.end_frame |
28 | 29 |
29 | 30 |
30 def set_interpolation_start(self, frame, values): | 31 def set_interpolation_start(self, frame, values): |
31 self.start_values = tuple(values) | 32 self.start_values = tuple(values) |
32 self.start_frame = frame | 33 self.start_frame = frame |
45 self.end_values = tuple(values) | 46 self.end_values = tuple(values) |
46 | 47 |
47 | 48 |
48 def update(self, frame): | 49 def update(self, frame): |
49 self._frame = frame | 50 self._frame = frame |
50 if frame >= self.end_frame: | 51 if frame >= self.end_frame - 1: |
51 self.values = tuple(self.end_values) | 52 self.values = tuple(self.end_values) |
52 self.start_values = tuple(self.end_values) | 53 self.start_values = tuple(self.end_values) |
53 self.start_frame = frame | 54 self.start_frame = frame |
54 else: | 55 else: |
55 truc = float(frame - self.start_frame) / float(self.end_frame - self.start_frame) | 56 coeff = self._formula(float(frame - self.start_frame) / float(self.end_frame - self.start_frame)) |
56 self.values = tuple(start_value + truc * (end_value - start_value) | 57 self.values = tuple(start_value + coeff * (end_value - start_value) |
57 for (start_value, end_value) in zip(self.start_values, self.end_values)) | 58 for (start_value, end_value) in zip(self.start_values, self.end_values)) |
58 | 59 |