Skip to content

Instantly share code, notes, and snippets.

@volfegan
Last active April 9, 2020 15:18
Show Gist options
  • Save volfegan/e8c04531f7e5e95e890fe56d491454b2 to your computer and use it in GitHub Desktop.
Save volfegan/e8c04531f7e5e95e890fe56d491454b2 to your computer and use it in GitHub Desktop.
Lists numbers in columns and highlights primes
//inspired by Max Cooper - Aleph 2 (Official Video by Martin Krzywinski)
//https://www.youtube.com/watch?v=tNYfqklRehM
import java.math.*;
BigInteger prime;
int n = 0;
int fontsize=12;
int x=0, y=0;
int repeat=1;
void setup() {
size(720, 720);
background(0);
textSize(fontsize);
}
void draw() {
float alpha = map(constrain(repeat, 1, 100), 1, 100, 4, 30);
fill(0, alpha);
square(-9, -9, 2*width);
//why background(0,alpha); does not work?
for (int i=0; i<repeat; i++) {
prime = BigInteger.valueOf(n);
if (prime.isProbablePrime(1)) fill(255, 0, 0);
else fill(0, 255, 0);
text(n, 5+x, fontsize+y);
n++;
//println(n + ", pos=(" +x +","+y + ") speed="+repeat);
y+=fontsize;
if (y>height) {
y = 0;
//faster alternative to String.valueOf(n).length()*fontsize
int digitfontsize = (int)(Math.log10(n)+1)*fontsize;//get how many digits the number n has for horizontal spacing
x += digitfontsize;
if (x > width-digitfontsize) {
x=0;
if (repeat < 250) repeat++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment