Created
February 10, 2012 23:20
-
-
Save quickredfox/1793937 to your computer and use it in GitHub Desktop.
Problem I cannot solve, should be simple math for someone...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function oscillator(low, high, inc) { | |
// basic test for illegal parameters | |
if (low > high || inc < 0 || 2 * (high - low) < inc) | |
return function() { return NaN; }; | |
var curr = low; | |
return function() { | |
var ret = curr; | |
curr += inc; | |
if (curr > high || curr < low) | |
{ | |
curr = inc>0 ? 2 * high - curr: 2 * low - curr; | |
inc = -inc; | |
}; | |
return ret; | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function oscillator(low, high, increment) { | |
// basic test for illegal parameters | |
if (low > high || increment < 0) | |
return function() { return NaN; }; | |
var curr = low; | |
return function() { | |
var ret = curr; | |
curr += increment; | |
// if the next number will exceed the boundaries, reverse the increment | |
if (curr + increment > high || curr + increment < low) | |
increment = -increment; | |
return ret; | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pseudo Coffeescript class | |
Class OscillatingIterator | |
constructor: ( low, high, increment )-> | |
this.low = low | |
this.high = high | |
this.i = increment | |
iterate: -> | |
### can haz magical math code plz? ### | |
# Usage | |
oi = new OscillatingIterator( 1, 10 , 1 ) | |
oi.iterate() #=> 1 | |
oi.iterate() #=> 2 | |
oi.iterate() #=> 3 | |
oi.iterate() #=> 4 | |
oi.iterate() #=> 5 | |
oi.iterate() #=> 6 | |
oi.iterate() #=> 7 | |
oi.iterate() #=> 8 | |
oi.iterate() #=> 9 | |
oi.iterate() #=> 10 | |
oi.iterate() #=> 9 | |
oi.iterate() #=> 8 | |
oi.iterate() #=> 7 | |
oi.iterate() #=> 6 | |
oi.iterate() #=> 5 | |
oi.iterate() #=> 4 | |
oi.iterate() #=> 3 | |
oi.iterate() #=> 2 | |
oi.iterate() #=> 1 | |
oi.iterate() #=> 2 | |
oi.iterate() #=> 3 | |
oi.iterate() #=> etc... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# V1 | |
class OscillatingIterator | |
constructor: ( @min, @max, @incr )-> | |
@index = 0 | |
iterate: -> | |
@index+=@incr*( @dir = if @index >= @max and @dir isnt -1 then -1 else if @index<=@min and @dir isnt +1 then +1 else @dir ) | |
for more on ori-via-stackoverflow.js see http://stackoverflow.com/a/9236626/261114
I ended up using cheery's tweaked version of Ori's http://stackoverflow.com/a/9238879/261114
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ph No big rush, was just poking you in a very complex way! I'm actually logging all this for meself