Created
October 17, 2012 20:55
-
-
Save hanshoglund/3908125 to your computer and use it in GitHub Desktop.
Processing interpolation (from an old example)
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
// Interpolera slumpmässiga värden mellan 0 och 200 | |
// Börja från mitten | |
final int SIZE = 230; | |
final int BALL_SIZE = 30; | |
Generator x = new RandomInterpolation(SIZE/2, 0, SIZE - BALL_SIZE, new Line(50)); | |
Generator y = new RandomInterpolation(SIZE/2, 0, SIZE - BALL_SIZE, new Line(10)); | |
void setup() { | |
size(SIZE, SIZE); | |
} | |
void draw() { | |
background(0); | |
smooth(); | |
ellipse(x.value(), y.value(), BALL_SIZE, BALL_SIZE); | |
} | |
interface Generator { | |
boolean trigger(); | |
float value(); | |
} | |
class Line implements Generator { | |
private int period; | |
Line(int period) { | |
this.period = period; | |
} | |
private int pos() { | |
return frameCount % period; | |
} | |
boolean trigger() { | |
return pos() == 0; | |
} | |
float value() { | |
return float(pos()) / float(period); | |
} | |
} | |
class RandomInterpolation implements Generator { | |
private float min, max, a, b; | |
private Generator phase; | |
RandomInterpolation(float init, float min, float max, Generator phase) { | |
this.min = min; | |
this.max = max; | |
this.phase = phase; | |
this.a = init; | |
this.b = random(min, max); | |
} | |
boolean trigger() { | |
return phase.trigger(); | |
} | |
float value() { | |
if (phase.trigger()) { | |
a = b; | |
b = random(min, max); | |
} | |
return a + (b - a) * phase.value(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment