Skip to content

Instantly share code, notes, and snippets.

@andreuinyu
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreuinyu/47affb842a8c1411ced1 to your computer and use it in GitHub Desktop.
Save andreuinyu/47affb842a8c1411ced1 to your computer and use it in GitHub Desktop.
This program allows you to generate n-hour clocks with the hand pointing at the f-th hour. I wrote this program to use the images to explain simple modular arithmetic visually.
void setup(){
size(350, 350);
background(255,255,255);
textSize(26);
}
void arrow(int x1, int y1, float x2, float y2) {
line(x1, y1, x2, y2);
pushMatrix();
translate(x2, y2);
float a = atan2(x1-x2, y2-y1);
rotate(a);
line(0, 0, -10, -10);
line(0, 0, 10, -10);
popMatrix();
}
void draw(){
int n=12;
int f=3;
textAlign(CENTER);
f=f%n;
fill(255, 255, 255);
ellipse(width/2, height/2, 250, 250);
fill(0, 0, 0);
ellipse(width/2, height/2, 10, 10);
for(int i=0; i < n; i++){
fill(0, 0, 0);
ellipse(cos(2*PI*i/n-PI/2)*125+width/2, sin(2*PI*i/n-PI/2)*125+height/2, 5, 5);
text(i, cos(2*PI*i/n-PI/2)*140+width/2, sin(2*PI*i/n-PI/2)*140+height/2+9);
if(i == f){
arrow(width/2, height/2, cos(2*PI*i/n-PI/2)*115+width/2, sin(2*PI*i/n-PI/2)*115+height/2);
}
}
String S="clock_of_"+n+"_hours_at_"+f+".jpg";
save(S);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment