Attachment 'g200.c'

Download

   1 // gcc -o g200 g200.c -lgd -lpng -lm ; ./g200
   2 //
   3 // Uses the libgd library.  For documentation see the file:
   4 //   /usr/share/doc/gd-*/index.html  
   5 //
   6 // or the website:
   7 //   http://www.libgd.org/Reference
   8 
   9 #define  NAME        "g200"
  10 #define  LABEL0      "Max NLP"
  11 #define  LABEL3      "Zero NLP"
  12 #define  NFRAME      120
  13 #define  XFRAME      1024
  14 #define  YFRAME      768
  15 #define  R00         200
  16 #define  XCENT0      (512-240)
  17 #define  XCENT3      (512+240)
  18 #define  YCENT       250
  19 #define  SATSIZE     50
  20 #define  DG          16
  21 #define  FSZ         30
  22 #define  FSX         100
  23 #define  FSY         40
  24 #define  FNT         "DejaVuMonoSans"
  25 #define  P1          490
  26 #define  P0          670
  27 #define  PX0         (XCENT0)
  28 #define  PX1         (XCENT3)
  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    // graph parameters and data arrays
  64    int     pmax[NFRAME]             ;
  65    int     pzero[NFRAME]            ;
  66    int     px[NFRAME]               ;
  67    double  pxscale = ((double)(PX1-PX0))/((double)NFRAME);
  68    double  pyscale = (double)(P1-P0);
  69    int     pf                       ;
  70 
  71    gdImagePtr         im            ; // Declare the image
  72 
  73    //------------------------------------------------------------
  74    // make directory
  75 
  76    sprintf( cmdstr, "rm -rf %sdir ; mkdir %sdir", NAME, NAME );
  77    system( cmdstr );
  78 
  79    // make graph points
  80    for( pf=0 ; pf < NFRAME ;  pf++ ) {
  81       
  82       double angle = dangle*((double)pf);
  83       double cx = r00*sin(angle);
  84       double cy = -r00*cos(angle);
  85       double dh = 0.5*( (double) derth );
  86       
  87       px[pf] = PX0 + (int)( pxscale * ((double)pf )) ;
  88 
  89       if(     cy > 0.0 ) {      // Front side
  90          pmax[ pf] = P1 ;
  91          pzero[pf] = P1 ;
  92       }
  93       else if( cx > dh ) {     // Back right
  94          pmax[ pf] = P1 ;
  95          double dx = cx-dh ;
  96          double cc = dx / sqrt(dx*dx+cy*cy) ;
  97          pzero[pf] = P0 + (int)( pyscale*cc ) ;
  98       }
  99       else if( cx < -dh ) {     // Back right
 100          pmax[ pf] = P1 ;
 101          double dx = -(cx+dh) ;
 102          double cc = dx / sqrt(dx*dx+cy*cy) ;
 103          pzero[pf] = P0 + (int)( pyscale*cc ) ;
 104       }
 105       else                {     // Night
 106          pmax[ pf] = P0 ;
 107          pzero[pf] = P0 ;
 108       } 
 109    }
 110 
 111    for( frame=0 ; frame < NFRAME ;  frame++ ) {
 112 
 113       double angle = dangle*((double)frame);
 114       im           = gdImageCreateTrueColor(XFRAME, YFRAME );
 115 
 116       // Allocate the colors sun1, white, red, green, blue
 117       white  = gdImageColorAllocate(im, 255, 255, 255);
 118       sun1   = gdImageColorAllocate(im,  51,  51, 102);
 119       red    = gdImageColorAllocate(im, 255,   0,   0);
 120       dred   = gdImageColorAllocate(im,  96,   0,   0);
 121       green  = gdImageColorAllocate(im,   0, 255,   0);
 122       dgreen = gdImageColorAllocate(im,   0,  80,   0);
 123       cyan   = gdImageColorAllocate(im,   0, 255, 255);
 124       dcyan  = gdImageColorAllocate(im,   0,  96,  96);
 125       gray   = gdImageColorAllocate(im, 128, 128, 128);
 126       dwhit  = gdImageColorAllocate(im, 192, 192, 192);
 127       dgray  = gdImageColorAllocate(im,  48,  48,  48);
 128       blue   = gdImageColorAllocate(im,   0,   0, 255);
 129       trans  = gdImageColorAllocate(im, 1, 1, 1);
 130 
 131       // line thickness of 1
 132       gdImageSetThickness( im, 3 ) ;
 133 
 134       // draw graph
 135       gdImageLine( im, PX0, P0, PX1, P0, gray );
 136       gdImageLine( im, PX0, P1, PX1, P1, gray );
 137       gdImageLine( im, PX0, P0, PX0, P1, gray );
 138       gdImageLine( im, PX1, P0, PX1, P1, gray );
 139 
 140       for( pf = 1 ; pf < NFRAME ; pf++ ) {
 141          int pm = pf-1 ;
 142          gdImageLine( im, px[pm], pmax[pm] , px[pf], pmax[pf],    red );
 143          gdImageLine( im, px[pm], pzero[pm], px[pf], pzero[pf], green );
 144       }
 145       gdImageFilledArc(im,px[frame],pmax[ frame],DG,DG, 180,0,   red, gdArc );
 146       gdImageFilledArc(im,px[frame],pzero[frame],DG,DG, 0,180, green, gdArc );
 147 
 148       gdImageStringFT( im,NULL,white, FNT,FSZ,0.0, PX0+80, P0-15, "Thinsat Power" );
 149 
 150       //--------------------------------------------------------------------
 151 
 152       centery  = YCENT          ;
 153 
 154       double satdraw            ;
 155       double cx = r00*sin(angle);
 156       double cy = r00*cos(angle);
 157       double th = (double) sth  ;
 158       double ox, oy             ;
 159       double c, s               ;
 160       double tx, ty             ;
 161 
 162       // max nightlight -----------------------------------------------
 163 
 164       gdImageFilledArc(im, XCENT0,YCENT, derth,derth, 0, 180, dgreen, gdArc );
 165       gdImageFilledArc(im, XCENT0,YCENT, derth,derth, 180, 0,  green, gdArc );
 166       gdImageStringFT( im,NULL,red, FNT,FSZ,0.0, XCENT0-FSX, FSY, LABEL0 );
 167       centerx = XCENT0   ;
 168       oxh = centerx + 0.5*derth;
 169       oxl = centerx - 0.5*derth;
 170 
 171       satangle = rad*0.0 ;
 172       c  = cos(satangle) ;
 173       s  = sin(satangle) ;
 174       tx = cx-th*s       ;
 175       ty = cy+th*c       ;
 176 
 177       for( satdraw = -dcell ; satdraw <= dcell ; satdraw += 1.0 ) {
 178 
 179          double  wx =  satdraw*c ;
 180          double  wy = -satdraw*s ;
 181 
 182          ox = centerx + (int) ( tx + wx ) ;
 183          oy = centery + (int) ( ty + wy ) ;
 184 
 185          gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, red  , gdArc);
 186 
 187          ox = centerx + (int) ( cx + wx ) ;
 188          oy = centery + (int) ( cy + wy ) ;
 189 
 190          if( ( ox > oxh ) || ( ox < oxl ) || ( oy < centery ) ) {
 191             gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, white , gdArc);
 192          } else {
 193             gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, gray  , gdArc);
 194          }
 195       }
 196 
 197       // zero nightlight ----------------------------------------------
 198       
 199       gdImageFilledArc(im, XCENT3,YCENT, derth,derth, 0, 180, dgreen, gdArc );
 200       gdImageFilledArc(im, XCENT3,YCENT, derth,derth, 180, 0,  green, gdArc );
 201       gdImageStringFT( im,NULL,green, FNT,FSZ,0.0, XCENT3-FSX, FSY, LABEL3 );
 202       centerx = XCENT3 ;
 203       oxh = centerx + 0.5*derth;
 204       oxl = centerx - 0.5*derth;
 205 
 206       if( ( angle > rad*90.0 ) && ( angle < rad*270.0 ) ) {
 207          satangle = rad*0.0 ;
 208       } else if( ( angle < rad*30.0 ) || ( angle > rad*330.0 ) ) {
 209          satangle =  3.0*angle + rad*180.0 ;
 210       } else {
 211          double  xx =  sin( angle ) ;
 212          double  yy = -cos( angle ) ;
 213 
 214          if ( xx > 0.0 ) {
 215             satangle = atan2(  yy,  xx-0.5 );
 216          } else {
 217             satangle = atan2( -yy, -xx-0.5 );
 218          }
 219       }
 220 
 221       c  = cos(satangle) ;
 222       s  = sin(satangle) ;
 223       tx = cx+th*s       ;    // - to +
 224       ty = cy+th*c       ;    // - to +
 225 
 226       for( satdraw = -dcell ; satdraw <= dcell ; satdraw += 1.0 ) {
 227 
 228          double  wx =  satdraw*c ;
 229          double  wy = -satdraw*s ;
 230 
 231          ox = centerx + (int) ( tx + wx ) ;
 232          oy = centery + (int) ( ty + wy ) ;
 233          
 234          gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, red  , gdArc);
 235 
 236          ox = centerx + (int) ( cx + wx ) ;
 237          oy = centery + (int) ( cy + wy ) ;
 238          
 239          if( ( ox > oxh ) || ( ox < oxl ) || ( oy < centery ) ) { 
 240             gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, white , gdArc);
 241          } else {
 242             gdImageFilledArc(im, ox, oy, sth, sth, 0, 360, gray  , gdArc);
 243          }
 244       }
 245 
 246       // ---------------------------------------------------------------
 247 
 248       sprintf( framename, "%sdir/a%04d.png", NAME, frame );
 249       pngout = fopen( framename, "wb");
 250       gdImagePngEx( im, pngout, 1 );
 251       gdImageDestroy(im);
 252       fclose(pngout);
 253       printf( "%04d/%04d frames rendered\r", frame, NFRAME );
 254       fflush( stdout );
 255    }
 256    printf("%04d/%04d frames rendered, now make flash animation\n",frame,NFRAME);
 257 
 258    sprintf( cmdstr, "png2swf -o %s.swf -r 20 -j 50 %sdir/*.png", NAME, NAME ); 
 259    system(  cmdstr );
 260 
 261    sprintf( cmdstr, "png2swf -X 512 -o %s_512.swf -r 20 -j 50 %sdir/*.png", NAME, NAME ); 
 262    system(  cmdstr );
 263 
 264    sprintf( cmdstr, "png2swf -X 384 -o %s_384.swf -r 20 -j 50 %sdir/*.png", NAME, NAME ); 
 265    system(  cmdstr );
 266 
 267    sprintf( cmdstr, "png2swf -X 256 -o %s_256.swf -r 20 -j 50 %sdir/*.png", NAME, NAME ); 
 268    system(  cmdstr );
 269 
 270    printf( "All done, flash animation complete\n" );
 271 }

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-14 23:22:23, 9.0 KB) [[attachment:g200.c]]
  • [get | view] (2022-03-16 00:29:52, 64.0 KB) [[attachment:g200.png]]
  • [get | view] (2013-08-14 21:02:48, 814.8 KB) [[attachment:g200_1024.swf]]
  • [get | view] (2013-08-14 23:22:33, 814.9 KB) [[attachment:g200_384.swf]]
  • [get | view] (2013-08-14 21:03:12, 814.9 KB) [[attachment:g200_512.swf]]
  • [get | view] (2022-03-16 00:41:03, 197.2 KB) [[attachment:g201.png]]
  • [get | view] (2013-08-29 18:50:31, 0.2 KB) [[attachment:gridhex1.ini]]
  • [get | view] (2013-08-29 08:11:48, 117.3 KB) [[attachment:gridhex1.png]]
  • [get | view] (2013-08-29 18:50:17, 1.3 KB) [[attachment:gridhex1.pov]]
  • [get | view] (2013-08-08 05:34:27, 118.4 KB) [[attachment:ir-reflect0.png]]
  • [get | view] (2013-08-08 05:23:38, 5.8 KB) [[attachment:ir0.c]]
  • [get | view] (2013-08-08 05:23:49, 1.9 KB) [[attachment:ir0.gp]]
  • [get | view] (2013-08-08 05:24:06, 16.9 KB) [[attachment:ir0.png]]
  • [get | view] (2013-08-22 15:39:57, 6.6 KB) [[attachment:ir1.c]]
  • [get | view] (2013-08-22 15:40:06, 2.3 KB) [[attachment:ir1.gp]]
  • [get | view] (2013-08-22 15:40:15, 16.5 KB) [[attachment:ir1.png]]
  • [get | view] (2013-08-14 22:55:55, 4.2 KB) [[attachment:irn1.c]]
  • [get | view] (2013-08-14 22:56:15, 1.8 KB) [[attachment:irn1.gp]]
  • [get | view] (2013-08-14 22:56:26, 17.9 KB) [[attachment:irn1.png]]
 All files | Selected Files: delete move to page

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