Attachment 'g400.c'

Download

   1 // g400.c
   2 // compile with:   cc -o g400 g400.c -lgd -lpng -lm
   3 //
   4 // Uses the libgd library.  For documentation see the file:
   5 //   /usr/share/doc/gd-*/index.html  
   6 //
   7 // or the website:
   8 //   http://www.libgd.org/Reference
   9 
  10 #define  NAME        "g400"
  11 #define  LABEL0      "Tumbling"
  12 #define  LABEL1      "Max  NLP"
  13 #define  LABEL2      "Min  NLP"
  14 #define  LABEL3      "Zero NLP"
  15 #define  NFRAME      120
  16 #define  XFRAME      1024
  17 #define  YFRAME      300
  18 #define  R00         120
  19 #define  XCENT0      (512-360)
  20 #define  XCENT1      (512-120)
  21 #define  XCENT2      (512+120)
  22 #define  XCENT3      (512+360)
  23 #define  YCENT       160
  24 #define  SATSIZE     30
  25 #define  FSZ         16
  26 #define  FSX         55
  27 #define  FSY         22
  28 #define  FNT         "DejaVuMonoSans"
  29 
  30 #include "gd.h"
  31 #include "math.h"
  32 #include <stdio.h>
  33 
  34 int main() {
  35    FILE   *pngout         ;                /* Declare output file */
  36    char   dirname[80]     ;
  37    char   framename[80]   ;
  38    char   cmdstr[80]      ;
  39    char   pngfile[80]     ;
  40 
  41    int     sun1, white, red, green, blue;  /* Declare color indexes */
  42    int     gray, dgray, dwhit, dgreen, trans ;
  43    int     dred, cyan, dcyan ;
  44 
  45    double  pi2    = 8.0 * atan( 1.0 );
  46    double  dangle = pi2 / ((double)NFRAME );
  47    double  rad    = pi2/360.0       ;
  48    double  satangle                 ;
  49    double  satdraw                  ;
  50    double  c, s                     ; // satangle sine and cosine
  51    double  cx, cy                   ; // orbit center
  52    double  tx, ty                   ; // back offset x and y
  53    double  ox, oy                   ; // drawing dot position
  54    int     dcell    = SATSIZE/2     ;
  55    int     drand    = 4*SATSIZE/5   ;
  56    int     sth      = SATSIZE/10    ;
  57    double  th       = (double) sth  ; // back thickness/offset
  58    int     derth    = R00           ;
  59    double  r00      = (double) R00  ;
  60    int     frame                    ;
  61    int     centerx, centery         ; // orbit and earth center
  62    int     oxh, oxl                 ; // edges of the earth
  63 
  64    gdImagePtr         im            ; // Declare the image
  65 
  66    //------------------------------------------------------------
  67  
  68    // make directory
  69    sprintf( cmdstr, "rm -rf %sdir ; mkdir %sdir", NAME, NAME );
  70    system( cmdstr );
  71 
  72    for( frame=0 ; frame < NFRAME ;  frame++ ) {
  73 
  74       double angle = dangle*((double)frame);
  75       im           = gdImageCreateTrueColor(XFRAME, YFRAME );
  76 
  77       // Allocate the colors sun1, white, red, green, blue
  78       white  = gdImageColorAllocate(im, 255, 255, 255);
  79       sun1   = gdImageColorAllocate(im,  51,  51, 102);
  80       red    = gdImageColorAllocate(im, 255,   0,   0);
  81       dred   = gdImageColorAllocate(im,  96,   0,   0);
  82       green  = gdImageColorAllocate(im,   0, 255,   0);
  83       dgreen = gdImageColorAllocate(im,   0,  80,   0);
  84       cyan   = gdImageColorAllocate(im,   0, 255, 255);
  85       dcyan  = gdImageColorAllocate(im,   0,  96,  96);
  86       gray   = gdImageColorAllocate(im, 128, 128, 128);
  87       dwhit  = gdImageColorAllocate(im, 192, 192, 192);
  88       dgray  = gdImageColorAllocate(im,  48,  48,  48);
  89       blue   = gdImageColorAllocate(im,   0,   0, 255);
  90       trans  = gdImageColorAllocate(im, 1, 1, 1);
  91 
  92       // line thickness of 1
  93       gdImageSetThickness( im, 1 ) ;
  94 
  95       centery  = YCENT          ;
  96 
  97       double satdraw            ;
  98       double cx = r00*sin(angle);
  99       double cy = r00*cos(angle);
 100       double th = (double) sth  ;
 101       double ox, oy             ;
 102       double c, s               ;
 103       double tx, ty             ;
 104 
 105       // random tumble  ----------------------------------------------
 106 
 107       gdImageFilledArc(im, XCENT0,YCENT, derth,derth, 0, 180,  green, gdArc );
 108       gdImageFilledArc(im, XCENT0,YCENT, derth,derth, 180, 0, dgreen, gdArc );
 109       gdImageStringFT( im,NULL,white, FNT,FSZ,0.0, XCENT0-FSX, FSY, LABEL0 );
 110       centerx = XCENT0 ;
 111       oxh = centerx + 0.5*derth;
 112       oxl = centerx - 0.5*derth;
 113 
 114       ox  = centerx + ((int)(r00*sin(angle)));
 115       oy  = centery + ((int)(r00*cos(angle)));
 116 
 117       if( ( ox > oxh ) || ( ox < oxl ) || ( oy > centery ) ) {
 118          gdImageFilledArc(im, ox, oy, drand, drand, 0, 360, dwhit  , gdArc);
 119       } else {
 120          gdImageFilledArc(im, ox, oy, drand, drand, 0, 360, gray   , gdArc);
 121       }
 122 
 123       // max nightlight -----------------------------------------------
 124 
 125       gdImageFilledArc(im, XCENT1,YCENT, derth,derth, 0, 180,  green, gdArc );
 126       gdImageFilledArc(im, XCENT1,YCENT, derth,derth, 180, 0, dgreen, gdArc );
 127       gdImageStringFT( im,NULL,white, FNT,FSZ,0.0, XCENT1-FSX, FSY, LABEL1 );
 128       centerx = XCENT1   ;
 129       oxh = centerx + 0.5*derth;
 130       oxl = centerx - 0.5*derth;
 131 
 132       satangle = rad*0.0 ;
 133       c  = cos(satangle) ;
 134       s  = sin(satangle) ;
 135       tx = cx-th*s       ;
 136       ty = cy-th*c       ;
 137 
 138       for( satdraw = -dcell ; satdraw <= dcell ; satdraw += 1.0 ) {
 139 
 140          double  wx =  satdraw*c ;
 141          double  wy = -satdraw*s ;
 142 
 143          ox = centerx + (int) ( tx + wx ) ;
 144          oy = centery + (int) ( ty + wy ) ;
 145 
 146          gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, red  , gdArc);
 147 
 148          ox = centerx + (int) ( cx + wx ) ;
 149          oy = centery + (int) ( cy + wy ) ;
 150 
 151          if( ( ox > oxh ) || ( ox < oxl ) || ( oy > centery ) ) {
 152             gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, white , gdArc);
 153          } else {
 154             gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, gray  , gdArc);
 155          }
 156       }
 157 
 158       // min nightlight -----------------------------------------------
 159 
 160       gdImageFilledArc(im, XCENT2,YCENT, derth,derth, 0, 180,  green, gdArc );
 161       gdImageFilledArc(im, XCENT2,YCENT, derth,derth, 180, 0, dgreen, gdArc );
 162       gdImageStringFT( im,NULL,white, FNT,FSZ,0.0, XCENT2-FSX, FSY, LABEL2 );
 163       centerx = XCENT2   ;
 164       oxh = centerx + 0.5*derth;
 165       oxl = centerx - 0.5*derth;
 166 
 167       if( ( angle < rad*90.0 ) || ( angle > rad*270.0 ) ) {
 168          satangle = rad*0.0 ;
 169       } else if( ( angle < rad*210.0 ) && ( angle > rad*150.0 ) ) {
 170          satangle = 4.0*angle + rad*180.0 ;
 171       } else if( angle > rad*180 ) {
 172          satangle = angle + rad*90.0 ;
 173       } else {
 174          satangle = angle - rad*90.0 ;
 175       }
 176       c  = cos(satangle) ;
 177       s  = sin(satangle) ;
 178       tx = cx-th*s       ;
 179       ty = cy-th*c       ;
 180 
 181       for( satdraw = -dcell ; satdraw <= dcell ; satdraw += 1.0 ) {
 182 
 183          double  wx =  satdraw*c ;
 184          double  wy = -satdraw*s ;
 185 
 186          ox = centerx + (int) ( tx + wx ) ;
 187          oy = centery + (int) ( ty + wy ) ; 
 188 
 189          gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, red  , gdArc);
 190 
 191          ox = centerx + (int) ( cx + wx ) ;
 192          oy = centery + (int) ( cy + wy ) ;
 193 
 194          if( ( ox > oxh ) || ( ox < oxl ) || ( oy > centery ) ) {
 195             gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, white , gdArc);
 196          } else {
 197             gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, gray  , gdArc);
 198          }
 199       }
 200 
 201       // zero nightlight ----------------------------------------------
 202       
 203       gdImageFilledArc(im, XCENT3,YCENT, derth,derth, 0, 180,  green, gdArc );
 204       gdImageFilledArc(im, XCENT3,YCENT, derth,derth, 180, 0, dgreen, gdArc );
 205       gdImageStringFT( im,NULL,white, FNT,FSZ,0.0, XCENT3-FSX, FSY, LABEL3 );
 206       centerx = XCENT3 ;
 207       oxh = centerx + 0.5*derth;
 208       oxl = centerx - 0.5*derth;
 209 
 210       if( ( angle < rad*90.0 ) || ( angle > rad*270.0 ) ) {
 211          satangle = rad*0.0 ;
 212       } else if( ( angle < rad*210.0 ) && ( angle > rad*150.0 ) ) {
 213          satangle = 3.0*angle + rad*0.0 ;
 214       } else {
 215          double  xx =  sin( angle ) ;
 216          double  yy = -cos( angle ) ;
 217 
 218          if ( xx > 0.0 ) {
 219             satangle = atan2 (  yy,  xx-0.5 );
 220          } else {
 221             satangle = atan2 ( -yy, -xx-0.5 );
 222          }
 223       }
 224 
 225       c  = cos(satangle) ;
 226       s  = sin(satangle) ;
 227       tx = cx-th*s       ;
 228       ty = cy-th*c       ;
 229 
 230       for( satdraw = -dcell ; satdraw <= dcell ; satdraw += 1.0 ) {
 231 
 232          double  wx =  satdraw*c ;
 233          double  wy = -satdraw*s ;
 234 
 235          ox = centerx + (int) ( tx + wx ) ;
 236          oy = centery + (int) ( ty + wy ) ;
 237          
 238          gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, red  , gdArc);
 239 
 240          ox = centerx + (int) ( cx + wx ) ;
 241          oy = centery + (int) ( cy + wy ) ;
 242          
 243          if( ( ox > oxh ) || ( ox < oxl ) || ( oy > centery ) ) { 
 244             gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, white , gdArc);
 245          } else {
 246             gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, gray  , gdArc);
 247          }
 248       }
 249 
 250       // ---------------------------------------------------------------
 251 
 252       sprintf( framename, "%sdir/a%04d.png", NAME, frame );
 253       pngout = fopen( framename, "wb");
 254       gdImagePngEx( im, pngout, 1 );
 255       gdImageDestroy(im);
 256       fclose(pngout);
 257       printf( "%04d/%04d frames rendered\r", frame, NFRAME );
 258       fflush( stdout );
 259    }
 260    printf("%04d/%04d frames rendered, now make flash animation\n",frame,NFRAME);
 261 
 262    sprintf( cmdstr, "png2swf -o %s.swf -r 20 -j 100 %sdir/*.png", NAME, NAME ); 
 263    system(  cmdstr );
 264    printf( "All done, flash animation complete\n" );
 265 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2013-08-06 04:51:46, 9.0 KB) [[attachment:g400.c]]
  • [get | view] (2013-08-06 04:52:00, 4004.5 KB) [[attachment:g400.swf]]
  • [get | view] (2012-06-04 22:29:30, 8.9 KB) [[attachment:nl02.c]]
  • [get | view] (2012-06-04 22:14:08, 11.2 KB) [[attachment:nl02nlp.png]]
  • [get | view] (2012-06-04 22:15:23, 10.4 KB) [[attachment:nl02pow.png]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.