home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / pdflib / pdflib-4.0.1.sit / pdflib-4.0.1 / bind / c / pdfclock.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  2.4 KB  |  111 lines  |  [TEXT/CWIE]

  1. /* $Id: pdfclock.c,v 1.6.2.1 2001/05/16 12:25:50 york Exp $
  2.  * A little PDFlib application to draw an analog clock.
  3.  *
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <time.h>
  8.  
  9. #include "pdflib.h"
  10.  
  11. #define RADIUS        200.0
  12. #define MARGIN        20.0
  13.  
  14. int
  15. main(int argc, char *argv[])
  16. {
  17.     PDF        *p;
  18.     float    alpha;
  19.     time_t    timer;
  20.     struct tm    ltime;
  21.     
  22.     p = PDF_new();
  23.  
  24.     if (PDF_open_file(p, "pdfclock.pdf") == -1) {
  25.     fprintf(stderr, "Error: cannot open PDF file pdfclock.pdf.\n");
  26.     exit(2);
  27.     }
  28.  
  29.     PDF_set_info(p, "Creator", "pdfclock.c");
  30.     PDF_set_info(p, "Author", "Thomas Merz");
  31.     PDF_set_info(p, "Title", "PDF clock (C)");
  32.  
  33.     PDF_begin_page(p, (float) (2 * (RADIUS + MARGIN)),
  34.               (float) (2 * (RADIUS + MARGIN)));
  35.     
  36.     PDF_translate(p, RADIUS + MARGIN, RADIUS + MARGIN);
  37.     PDF_setcolor(p, "both", "rgb", 0.0, 0.0, 1.0, 0.0);
  38.     PDF_save(p);
  39.  
  40.     /* minute strokes */
  41.     PDF_setlinewidth(p, 2.0);
  42.     for (alpha = 0; alpha < 360; alpha += 6)
  43.     {
  44.     PDF_rotate(p, 6.0);
  45.     PDF_moveto(p, RADIUS, 0.0);
  46.     PDF_lineto(p, (float) (RADIUS-MARGIN/3), 0.0);
  47.     PDF_stroke(p);
  48.     }
  49.  
  50.     PDF_restore(p);
  51.     PDF_save(p);
  52.  
  53.     /* 5 minute strokes */
  54.     PDF_setlinewidth(p, 3.0);
  55.     for (alpha = 0; alpha < 360; alpha += 30)
  56.     {
  57.     PDF_rotate(p, 30.0);
  58.     PDF_moveto(p, RADIUS, 0.0);
  59.     PDF_lineto(p, RADIUS-MARGIN, 0.0);
  60.     PDF_stroke(p);
  61.     }
  62.  
  63.     time(&timer);
  64.     ltime = *localtime(&timer);
  65.  
  66.     /* draw hour hand */
  67.     PDF_save(p);
  68.     PDF_rotate(p, 
  69.         (float)(-((ltime.tm_min/60.0) + ltime.tm_hour - 3.0) * 30.0));
  70.     PDF_moveto(p, -RADIUS/10, -RADIUS/20);
  71.     PDF_lineto(p, RADIUS/2, 0.0);
  72.     PDF_lineto(p, -RADIUS/10, RADIUS/20);
  73.     PDF_closepath(p);
  74.     PDF_fill(p);
  75.     PDF_restore(p);
  76.  
  77.     /* draw minute hand */
  78.     PDF_save(p);
  79.     PDF_rotate(p,
  80.         (float) (-((ltime.tm_sec/60.0) + ltime.tm_min - 15.0) * 6.0));
  81.     PDF_moveto(p, -RADIUS/10, -RADIUS/20);
  82.     PDF_lineto(p, RADIUS * 0.8, 0.0);
  83.     PDF_lineto(p, -RADIUS/10, RADIUS/20);
  84.     PDF_closepath(p);
  85.     PDF_fill(p);
  86.     PDF_restore(p);
  87.  
  88.     /* draw second hand */
  89.     PDF_setcolor(p, "both", "rgb", 1.0, 0.0, 0.0, 0.0);
  90.     PDF_setlinewidth(p, 2);
  91.     PDF_save(p);
  92.     PDF_rotate(p, (float) -((ltime.tm_sec - 15.0) * 6.0));
  93.     PDF_moveto(p, -RADIUS/5, 0.0);
  94.     PDF_lineto(p, RADIUS, 0.0);
  95.     PDF_stroke(p);
  96.     PDF_restore(p);
  97.  
  98.     /* draw little circle at center */
  99.     PDF_circle(p, 0, 0, (float) RADIUS/30);
  100.     PDF_fill(p);
  101.  
  102.     PDF_restore(p);
  103.  
  104.     PDF_end_page(p);
  105.  
  106.     PDF_close(p);
  107.     PDF_delete(p);
  108.  
  109.     return 0;
  110. }
  111.