Attachment 'gd12.c'

Download

   1 // gd12.c
   2 // really fake orbiting  server-sat
   3 // This approximates a Kepler orbit with high eccentricity
   4 // 
   5 // Given the way I set up the palette, I would expect a white
   6 // background, but it makes a black one.  Huh?
   7 //
   8 // compile with
   9 // cc -o gd12 gd12.c -lgd -lpng -lz -ljpeg -lfreetype -lm
  10 //
  11 // Uses the libgd library.  For documentation see the file:
  12 //   /usr/share/doc/gd-*/index.html  
  13 //
  14 // or the website:
  15 //   http://www.libgd.org/Reference
  16 
  17 #include "gd.h"
  18 #include "math.h"
  19 #include <stdio.h>
  20 
  21 #define  XSIZE       1024
  22 #define  YSIZE       768
  23 #define  SEMIMAJOR   430
  24 #define  ECCENTRIC   0.7000
  25 #define  CENTERX     200
  26 #define  CENTERY     384
  27 
  28 int main() {
  29    gdImagePtr im0, im1, im2;               /* Declare the image */
  30    FILE *gifout;                           /* Declare output file */
  31    int black, white, red, green, blue;     /* Declare color indexes */
  32    int  gray, trans ;
  33    double  pi2    = 8.0 * atan( 1.0 );
  34    double  delta  = pi2 / 200 ;
  35    double  delta2 = pi2 / 400 ;
  36    double  angle ;
  37    double  r     ;
  38    double  r00 = SEMIMAJOR * ( 1.0 - ECCENTRIC*ECCENTRIC ) ; 
  39    gdPoint points[1000] ;
  40    int     point ;
  41 
  42    double  object=0.0 ;
  43    int     ox, oy ;  
  44 
  45    im1 = gdImageCreate(XSIZE, YSIZE );
  46 
  47    /* Allocate the colors black, white, red, green, blue */
  48    white = gdImageColorAllocate(im1, 255, 255, 255);
  49    black = gdImageColorAllocate(im1,   0,   0,   0);
  50    red   = gdImageColorAllocate(im1, 255,   0,   0);
  51    green = gdImageColorAllocate(im1,   0, 255,   0);
  52    blue  = gdImageColorAllocate(im1,   0,   0, 255);
  53    gray  = gdImageColorAllocate(im1, 128, 128, 128);
  54    trans = gdImageColorAllocate(im1, 1, 1, 1);
  55 
  56    point = 0 ;
  57    /* compute points for ellipse with line segments */
  58    for( angle=0.0 ; angle <= pi2 ; ) {
  59       r  = r00/( 1.0 + ECCENTRIC * cos( angle ));
  60       points[point].x = CENTERX - (int) ( r * cos( angle ) );
  61       points[point].y = CENTERY + (int) ( r * sin( angle ) );
  62       angle += delta * SEMIMAJOR / r ;
  63       point++ ;
  64    }
  65 
  66    gifout = fopen( "test12.gif", "wb");
  67 
  68    // loop forever
  69    gdImageGifAnimBegin( im1, gifout, 1, 0 ) ;
  70 
  71    for( angle=0.0 ; angle <= pi2 ; ) {
  72 
  73       im2 = gdImageCreate( XSIZE, YSIZE ) ;
  74       gdImagePaletteCopy( im2, im1 );
  75 
  76       /* line thickness of 1 */
  77       gdImageSetThickness( im2, 1 );
  78 
  79       /* draw orbit */
  80       gdImagePolygon( im2, points, point, blue );
  81 
  82       ox = CENTERX ;
  83       oy = CENTERY ;
  84       /* earth */
  85       gdImageFilledArc( im2, ox, oy, 130, 130, 0, 360, green, gdArc );
  86 
  87       /* server_sat */
  88       r  = r00/( 1.0 + ECCENTRIC * cos( angle ));
  89       ox = CENTERX - (int) ( r * cos( angle ) );
  90       oy = CENTERY + (int) ( r * sin( angle ) );
  91  
  92       gdImageFilledArc(im2, ox    , oy,    60, 60, 0, 360, gray,  gdArc);
  93       gdImageFilledArc(im2, ox    , oy+40, 20, 20, 0, 360, white, gdArc);
  94       gdImageFilledArc(im2, ox-35 , oy-20, 20, 20, 0, 360, white, gdArc);
  95       gdImageFilledArc(im2, ox+35 , oy-20, 20, 20, 0, 360, white, gdArc);
  96 
  97       /* Output the image to the disk file in GIF format. */
  98       gdImageGifAnimAdd( im2, gifout, 0, 0, 2, 1,
  99                          gdDisposalRestoreBackground, im1 );
 100 
 101       angle += delta2 * pow( ( SEMIMAJOR / r ), 2) ;
 102       im0 = im1 ;
 103       im1 = im2 ;
 104       gdImageDestroy(im0);
 105    }
 106    gdImageGifAnimEnd(gifout);
 107 
 108    /* Close the files. */
 109    fclose(gifout);
 110    gdImageDestroy(im1);
 111 }

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] (2009-04-07 07:49:28, 2.3 KB) [[attachment:gd10.c]]
  • [get | view] (2009-04-07 07:55:24, 3.3 KB) [[attachment:gd12.c]]
  • [get | view] (2009-04-07 07:51:23, 4.4 KB) [[attachment:test10.gif]]
  • [get | view] (2009-04-07 07:55:07, 124.4 KB) [[attachment:test12.gif]]
 All files | Selected Files: delete move to page

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