Skip to content

Instantly share code, notes, and snippets.

@madc
Created June 24, 2012 17:13
Show Gist options
  • Save madc/2984060 to your computer and use it in GitHub Desktop.
Save madc/2984060 to your computer and use it in GitHub Desktop.
Analog Clock with openFrameworks
#include "ofClock.h"
ofClock::ofClock()
{
//Make everything looking nice and smooth.
ofSetCircleResolution(100);
ofEnableSmoothing();
//Set size & position of our clock
if( ofGetHeight() < ofGetWidth() )
radius = (ofGetHeight()/2)-20;
else
radius = (ofGetWidth()/2)-20;
top = (ofGetHeight()/2);
left = (ofGetWidth()/2);
}
void ofClock::update( int sec, int min, int hour ){
secondsAngle = 6 * sec;
minutesAngle = 6 * min;
hoursAngle = 30 * hour + (min / 2);
}
void ofClock::draw(){
//Set the coortinatesystem to the center of the clock.
ofPoint circle_center = ofPoint( left, top );
ofTranslate(circle_center);
ofRotateZ(-90);
//Draw background of the clock
ofSetHexColor(0xbbbbbb);
ofFill();
ofCircle( ofPoint(0,0), radius );
//Draw Outline of the clock
ofSetLineWidth( (radius/100)*4 );
ofNoFill();
ofSetHexColor(0x888888);
ofCircle( ofPoint(0,0), radius );
//Draw the clock face
ofSetLineWidth(1);
for(int i=0;i<12;i++){
ofRotateZ( 30 );
ofFill();
ofRect( ofPoint( (radius/10)*8, -((radius/100)*2) ), (radius/100)*20, (radius/100)*4 );
ofNoFill();
ofRect( ofPoint( (radius/10)*8, -((radius/100)*2) ), (radius/100)*20, (radius/100)*4 );
}
//Draw the hour hand
ofPushMatrix();
ofSetHexColor(0xFF0000);
ofRotateZ( hoursAngle );
ofFill();
ofRect( 0,-((radius/100)*5),(radius/10)*6, (radius/100)*10 );
ofNoFill();
ofRect( 0,-((radius/100)*5),(radius/10)*6, (radius/100)*10 );
ofPopMatrix();
//Draw the minute hand
ofPushMatrix();
ofSetHexColor(0xFF6666);
ofRotateZ( minutesAngle );
ofFill();
ofRect( 0,-((radius/100)*4),(radius/10)*7, (radius/100)*8 );
ofNoFill();
ofRect( 0,-((radius/100)*4),(radius/10)*7, (radius/100)*8 );
ofPopMatrix();
//Draw the second hand
ofPushMatrix();
ofSetHexColor(0xFF9999);
ofRotateZ( secondsAngle );
ofFill();
ofRect( -((radius/100)*2),-((radius/100)*2),(radius/10)*9, (radius/100)*4 );
ofNoFill();
ofRect( -((radius/100)*2),-((radius/100)*2),(radius/10)*9, (radius/100)*4 );
ofPopMatrix();
ofFill();
ofSetHexColor(0xFF9999);
ofCircle( ofPoint(0,0), (radius/100)*6 );
}
#ifndef _OF_CLOCK
#define _OF_CLOCK
#include "ofMain.h"
class ofClock {
public:
ofClock();
void update( int, int , int );
void draw();
private:
int radius;
int top;
int left;
float secondsAngle;
float minutesAngle;
float hoursAngle;
};
#endif
@microcosm
Copy link

Thanks for this code - was just using it to learn about OF transforms. However note that the returned values for ofGetWidth / ofGetHeight in a constructor will always return 1024 x 768 regardless of the true window size. The way around this is to switch out the constructor for a setup() method and call it from ofApp.setup()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment