Skip to content

Instantly share code, notes, and snippets.

@CSchoel
Created January 27, 2015 13:31
Show Gist options
  • Save CSchoel/f83a70dabef133d176aa to your computer and use it in GitHub Desktop.
Save CSchoel/f83a70dabef133d176aa to your computer and use it in GitHub Desktop.
Binary clock
//Autor: Nadja Krümmel
class BinaryDigit {
int[] binary;
int decimal;
BinaryDigit() {
this.binary = new int[4];
this.binary[3] =0;
this.decimal = 0;
}
BinaryDigit(int decimal) {
this.decimal = decimal;
int zahl = decimal;
int[] result = new int[4];
int i = 0;
while (zahl >0) {
int digit = zahl % 2;
zahl = zahl / 2;
result[i] = digit;
i++;
}
int[] res = new int[4];
i =4;
for (int j = 0; j < res.length; j++) {
i--;
res[j] =
result[i];
}
this.binary = res;
}
void display(int posX, int posY, int factor) {
for (int i =0; i< this.binary.length; i++) {
if (this.binary[i] == 0 ) {
fill(255);
} else {
fill(255, 0, 0);
}
rect(posX, posY+factor*i+(factor/3)*i, factor, factor);
}
}
void displayMini(int posX, int posY, int factor) {
boolean drawn = false;
for (int i =0; i< this.binary.length; i++) {
if (this.binary[i] == 0 ) {
if (drawn)
fill(255);
else
fill(0);
} else {
drawn = true;
fill(255, 0, 0);
}
rect(posX, posY+factor*i+(factor/3)*i, factor, factor);
}
}
}
BinaryDigit h1, h2, m1, m2, s1, s2;
int factor;
void setup() {
size(500, 500);
factor = 50;
background(0);
}
void draw() {
int h = hour();
int m = minute();
int s = second();
h1 = new BinaryDigit(h/10);
h2 = new BinaryDigit(h % 10);
m1 = new BinaryDigit(m/10);
m2 = new BinaryDigit(m % 10);
s1 = new BinaryDigit(s/10);
s2 = new BinaryDigit(s % 10);
h1.display(10, 10, factor);
h2.display(10+factor*1+10, 10, factor);
m1.display(10+factor*2+10+20, 10, factor);
m2.display(10+factor*3+10+20+10, 10, factor);
s1.display(10+factor*4+10+10+20+10+20, 10, factor);
s2.display(10+factor*5+10+10+20+10+20+10, 10, factor);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment