Mercurial > code > home > repos > light9
annotate light9/chase.py @ 1321:715a442c2635
cursor use d3.scales for coordinate system transforms
Ignore-this: 373c3e855697624dc05e4555863002d4
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Fri, 03 Jun 2016 22:00:24 +0000 |
parents | 1c590824dd14 |
children | 7772cc48e016 |
rev | line source |
---|---|
291
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
1 from __future__ import division |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
2 |
293
1c590824dd14
chase logic is (mostly) fixed, integrate with curvecalc
David McClosky <dmcc@bigasterisk.com>
parents:
291
diff
changeset
|
3 def chase(t, ontime=0.5, offset=0.2, onval=1.0, |
291
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
4 offval=0.0, names=None, combiner=max): |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
5 names = names or [] |
293
1c590824dd14
chase logic is (mostly) fixed, integrate with curvecalc
David McClosky <dmcc@bigasterisk.com>
parents:
291
diff
changeset
|
6 # maybe this is better: |
1c590824dd14
chase logic is (mostly) fixed, integrate with curvecalc
David McClosky <dmcc@bigasterisk.com>
parents:
291
diff
changeset
|
7 # period = ontime + ((offset + ontime) * (len(names) - 1)) |
1c590824dd14
chase logic is (mostly) fixed, integrate with curvecalc
David McClosky <dmcc@bigasterisk.com>
parents:
291
diff
changeset
|
8 period = (offset + ontime) * len(names) |
291
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
9 outputs = {} |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
10 for index, name in enumerate(names): |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
11 # normalize our time |
293
1c590824dd14
chase logic is (mostly) fixed, integrate with curvecalc
David McClosky <dmcc@bigasterisk.com>
parents:
291
diff
changeset
|
12 local_offset = (offset + ontime) * index |
291
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
13 local_t = t - local_offset |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
14 local_t %= period |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
15 |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
16 # see if we're still in the on part |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
17 if local_t <= ontime: |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
18 value = onval |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
19 else: |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
20 value = offval |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
21 |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
22 # it could be in there twice (in a bounce like (1, 2, 3, 2) |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
23 if name in outputs: |
293
1c590824dd14
chase logic is (mostly) fixed, integrate with curvecalc
David McClosky <dmcc@bigasterisk.com>
parents:
291
diff
changeset
|
24 outputs[name] = combiner(value, outputs[name]) |
291
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
25 else: |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
26 outputs[name] = value |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
27 return outputs |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
28 |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
29 if __name__ == "__main__": |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
30 # a little testing |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
31 for x in range(80): |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
32 x /= 20.0 |
293
1c590824dd14
chase logic is (mostly) fixed, integrate with curvecalc
David McClosky <dmcc@bigasterisk.com>
parents:
291
diff
changeset
|
33 output = chase(x, onval='x', offval=' ', ontime=0.1, offset=0.2, |
291
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
34 names=('a', 'b', 'c', 'd')) |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
35 output = output.items() |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
36 output.sort() |
6c3b487e7bbc
Basic chase logic (does not do bounces correctly)
David McClosky <dmcc@bigasterisk.com>
parents:
diff
changeset
|
37 print "%.2f\t%s" % (x, ' '.join([str(x) for x in output])) |