Skip to content

Instantly share code, notes, and snippets.

@jmitchell
Last active December 18, 2015 11:39
Show Gist options
  • Save jmitchell/5776989 to your computer and use it in GitHub Desktop.
Save jmitchell/5776989 to your computer and use it in GitHub Desktop.
The Matrix in Processing.org
class MatrixChar {
final int STREAK_LENGTH = 100;
final color COLOR = color(0, 255, 0);
final PFont FONT = createFont("Monospace", 12);
final int DEFAULT_CHAR_SIZE = 100;
final int DEFAULT_SPEED = 5;
int x, y;
char chr;
float distanceAway;
boolean shouldReplace;
public MatrixChar()
{
x = (int)random(width);
y = (int)random(-height, 0);
chr = randomChar();
distanceAway = randomDistance();
shouldReplace = false;
}
public void draw()
{
updatePosition();
textFont(FONT);
textSize(chrHeight());
drawStreak();
drawCurrentPosition();
}
public boolean readyToReplace()
{
return shouldReplace;
}
private void updatePosition()
{
y += DEFAULT_SPEED / distanceAway;
if (y > height + chrHeight()) {
shouldReplace = true;
}
}
private int chrHeight()
{
return round(DEFAULT_CHAR_SIZE / distanceAway);
}
private void drawStreak()
{
for (int i = 0; i < STREAK_LENGTH; i += 3)
{
int streakY = round(map(i, 0, STREAK_LENGTH, y-STREAK_LENGTH, y));
int alpha = round(sqrt(map(i, 0, STREAK_LENGTH, 0, 255)));
fill(COLOR, alpha / distanceAway);
text(chr, x, streakY);
}
}
private void drawCurrentPosition()
{
fill(COLOR, 512 / distanceAway);
text(chr, x, y);
}
private char randomChar()
{
return (char)random('!', '~');
}
private float randomDistance()
{
return random(0.5, 1.5);
}
}
ArrayList<MatrixChar> chrs;
void setup()
{
size(800, 600);
background(0);
frameRate(30);
randomSeed(4);
chrs = new ArrayList<MatrixChar>();
for (int i = 0; i < 100; i++) {
chrs.add(new MatrixChar());
}
}
void draw()
{
background(0);
for (int i = 0; i < chrs.size(); i++) {
MatrixChar curr = chrs.get(i);
curr.draw();
if (curr.readyToReplace()) {
chrs.set(i, new MatrixChar());
}
}
saveFrame("frames/#####.tif");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment