// ---------------------------------------------------------------------- // System Headers // ---------------------------------------------------------------------- #include #include // ---------------------------------------------------------------------- // Local Headers // ---------------------------------------------------------------------- #include "util.h" #include "svg.h" // ---------------------------------------------------------------------- // drawFace - Draw the minute marks (thicker every 5 minutes) and // the clock's numbers (1 - 12) // ---------------------------------------------------------------------- static void drawFace( SVG *svg, int xc, int yc, int r1, int r2, int r3, int fontSize ) { // Begin at 0 degrees with the number '3' int degrees = 0; int number = 3; while(degrees < 360) { int width = 1; int doNumber = 0; if(degrees % (360 / 12) == 0) { width = 4; doNumber = 1; } XY p1 = utilGetXY(degrees, r1, xc, yc); XY p2 = utilGetXY(degrees, r2, xc, yc); svgLineEx( svg, utilRound(p1.mX), utilRound(p1.mY), utilRound(p2.mX), utilRound(p2.mY), width, svg->mColors.black ); if(doNumber) { char buffer[255] = { '\0' }; snprintf(buffer, sizeof(buffer), "%d", number); XY p3 = utilGetXY(degrees, r3, xc, yc); svgText( svg, utilRound(p3.mX) - (strlen(buffer) * fontSize) / 4, utilRound(p3.mY) + fontSize / 3, buffer, fontSize ); if(++number > 12) number = 1; } degrees += (360 / 60); } } // ---------------------------------------------------------------------- // drawHand - Draw a handle with the specified width and radius, this // function is used for minutes and seconds but not hours // as it assumes 'value' is 0 => 60 // ---------------------------------------------------------------------- static void drawHand( SVG *svg, int value, int xc, int yc, int lineWidth, int radius ) { double degrees = -90.0 + (value / 60.0) * 360.0; XY p = utilGetXY(degrees, radius, xc, yc); svgLineEx( svg, xc, yc, utilRound(p.mX), utilRound(p.mY), lineWidth, svg->mColors.black ); } // ---------------------------------------------------------------------- // main // ---------------------------------------------------------------------- int main() { const int WIDTH = 500; const int HEIGHT = 500; const char *NAME = "clock.html"; SVG *svg = svgOpen(NAME, "Clock", WIDTH, HEIGHT); if(!svg) { printf("Call to 'svgOpen' failed\n"); return(1); } int hour = 0; int minute = 0; int second = 0; int xc = svg->mWidth / 2; int yc = svg->mHeight / 2; int theSize = (svg->mWidth <= svg->mHeight ? svg->mWidth : svg->mHeight); // The two outer edges of the clock face int r1 = utilRound(.400 * theSize); int r2 = utilRound(.375 * theSize); // The start and end of the minute marks and the // location of the numbers int r3 = utilRound(.358 * theSize); int r4 = utilRound(.338 * theSize); int r5 = utilRound(.298 * theSize); // The second, minute and hours hands int r6 = utilRound(.328 * theSize); int r7 = utilRound(.318 * theSize); int r8 = utilRound(.238 * theSize); svgCircle(svg, xc, yc, r1, svg->mColors.black, svg->mColors.none); svgCircle(svg, xc, yc, r2, svg->mColors.black, svg->mColors.none); drawFace(svg, xc, yc, r3, r4, r5, utilRound(.06 * theSize)); svgCircle(svg, xc, yc, 4, svg->mColors.black, svg->mColors.black); utilGetHMS(&hour, &minute, &second); drawHand(svg, second, xc, yc, 2, r6); drawHand(svg, minute, xc, yc, 5, r7); // The 'degrees' calcuation for our hours hand is more complex // since the hand moves part of the way towards the next number // as it progresses through the hour (so, at 7:30 the hour hand // would be 1/2 way between the 7 and the 8. int degrees = utilRound( -90 + (hour % 12) / 12.0 * 360.0 + 30.0 * (minute / 60.0) + 0.5 ); XY p = utilGetXY(degrees, r8, xc, yc); svgLineEx( svg, xc, yc, utilRound(p.mX), utilRound(p.mY), 10, svg->mColors.black ); svgBorder(svg, svg->mColors.black); svgClose(svg); printf("Successfully wrote: %s\n", NAME); return(0); }