/** Module: simple_clock_v1 Author: david tames, http://kino-eye.com Description: Demonstrate the use of trigonometric functions along with map() and norm() to drawing a simple analog clock. Basic trig formulas from "Foundation ActionScript 3.0 Animation" by Keith Peters (Friends of Ed, 2007). More interesting visulizations of time passing to come, but I could not resist making an analog clock. */ /* --------- globals --------- */ // time variables float ss_rad; // current seconds in the local time as radians float mm_rad; // current minutes in the local time as radians float hh_rad; // current hours in the local time as radians // clock dimensions, colors, weights, etc. int handColor = 255; int backgroundColor = 64; int secondHandWeight = 2; int minuteHandWeight = 4; int hourHandWeight = 10; int minuteTickWeight = 2; int hourTickWeight = 7; float clockCenterX = 200; float clockCenterY = 200; float minuteHandLength = 130; float secondHandLength = 150; float hourHandLength = 80; float X2; float Y2; float tickX; float tickY; /* --------- setup --------- */ void setup() { size(400, 400); stroke(255); smooth(); } /* --------- draw --------- */ void draw() { background(backgroundColor); noStroke(); /* the angles for sin() and cos() start at the 3 o'clock position herefore subtract HALF_PI so they start at the top, map and norm are very handy functions for refining data into usable forms */ ss_rad = map(second(), 0, 60, 0, TWO_PI) - HALF_PI; mm_rad = map(minute() + norm(second(), 0, 60), 0, 60, 0, TWO_PI) - HALF_PI; hh_rad = map(hour() + norm(minute(), 0, 60), 0, 24, 0, TWO_PI * 2) - HALF_PI; // println ("hh:mm:ss: " + hh_rad + ":" + mm_rad + ":" + ss_rad); fill(handColor); stroke(handColor); // Draw the second hand strokeWeight(secondHandWeight); X2 = cos (ss_rad) * secondHandLength + clockCenterX; Y2 = sin (ss_rad) * secondHandLength + clockCenterY; line (clockCenterX, clockCenterY, X2, Y2); // Draw the minute hand strokeWeight(minuteHandWeight); X2 = cos (mm_rad) * minuteHandLength + clockCenterX; Y2 = sin (mm_rad) * minuteHandLength + clockCenterY; line (clockCenterX, clockCenterY, X2, Y2); // Draw the hour hand strokeWeight(hourHandWeight); X2 = cos (hh_rad) * hourHandLength + clockCenterX; Y2 = sin (hh_rad) * hourHandLength + clockCenterY; line (clockCenterX, clockCenterY, X2, Y2); // Draw the seconds ticks strokeWeight(minuteTickWeight); for (int angle = 0; angle < 360; angle+=6) { tickX = clockCenterX + (cos(radians(angle)) * secondHandLength); tickY = clockCenterY + (sin(radians(angle)) * secondHandLength); point(tickX, tickY); } // Draw the minute ticks strokeWeight(hourTickWeight); for (int angle = 0; angle < 360; angle+=30) { tickX = clockCenterX + (cos(radians(angle)) * secondHandLength); tickY = clockCenterY + (sin(radians(angle)) * secondHandLength); point(tickX, tickY); } }