Attachment 'tr04.c'

Download

   1 // tr04.c
   2 // bending Warren truss beam
   3 //
   4 // cc -o tr04 tr04.c -lgd -lpng -lm
   5 // 
   6 // The forces and angles shown are illustrative, not at all accurate.
   7 // A better version of this program would get the forces right.
   8 //
   9 // truss by James Warren 1848
  10 
  11 #define  NAME   "tr04"
  12 
  13 #define  WAVEL     1.0   //
  14 #define  RX       10.0   //  as drawn
  15 #define  RY       40.0   //
  16 #define  R0        1.0   //
  17 
  18 #define  PRELOAD -60.0   //  preload
  19 #define  SY     -40.0    //
  20 #define  SM      -1.0    //
  21 #define  SS       0.0    //  
  22 
  23 #define  SOFF      20    //
  24 
  25 #define  NSTRESS   101   //  Stress scale range
  26 #define  MSTRESS  300.0  //  Stress color multiplier
  27 #define  NPOINTS    12   //  Number of truss points (overlap sides)
  28 
  29 #define  RATE        6   //  Flash frame rate ( 1560x )
  30 #define  NPLOT      90   //  Number of plots
  31 
  32 #define  XSIZE    1024   //
  33 #define  YSIZE     320   //
  34 #define  YH       160.0  //
  35 
  36 #define  PI        3.141592653589793238
  37 #define  FNT       "DejaVuMonoSans"
  38 #define  FSZ        20
  39 
  40 #include <gd.h>
  41 #include <math.h>
  42 #include <string.h>
  43 #include <stdio.h>
  44 #include <stdlib.h>
  45 
  46 //
  47 int         i,  j                        ; // general index
  48 
  49 //
  50 FILE        *pngout;                     ; // output file
  51 char        rmmkdir[200]                 ; //
  52 char        png2swf[200]                 ; //
  53 char        filename[80]                 ; //
  54 char        plotlabel[80]                ; // plot label
  55 
  56 // plot stuff
  57 int         iplot                        ; // plot number
  58 gdImagePtr  im                           ; // working image
  59 int         black, gray, white           ; // color indexes
  60 int         red,  green, blue            ; // color indexes
  61 int         colorstress[NSTRESS+1]       ; // color index for stress
  62 
  63 double       tx[200],  ty[200]           ; // top points
  64 double       bx[200],  by[200]           ; // bottom points
  65 double      stx[200], sty[200]           ; // top stress
  66 double      sbx[200], sby[200]           ; // bottom stress
  67 
  68 double      sx=0.0                       ; // stress returned by strut
  69 double      sy=0.0                       ; // stress returned by strut
  70 
  71 int         sx0, sy0, sx1, sy1           ; // stress vector points
  72 
  73 //-----------------------------------------------------------------
  74 void initialize() {
  75    sprintf( rmmkdir, "rm -rf %sdir ; mkdir %sdir", NAME, NAME );
  76    system(  rmmkdir )                    ; // make directory
  77 }
  78 
  79 //-----------------------------------------------------------------
  80 void openplot() {
  81    im      = gdImageCreateTrueColor( (int)XSIZE, (int)YSIZE );
  82    black   = gdImageColorResolve(im,   0,   0,   0);
  83    gray    = gdImageColorResolve(im, 128, 128, 128);
  84    white   = gdImageColorResolve(im, 255, 255, 255);
  85    red     = gdImageColorResolve(im, 255,   0,   0);
  86    green   = gdImageColorResolve(im,   0, 255,   0);
  87    blue    = gdImageColorResolve(im,   0,   0, 255);
  88 
  89    // stress colors
  90    for( i=0 ; i < NSTRESS ; i++ ) {
  91       double stress= ((double) i ) / ((double)(NSTRESS-1));
  92       int    ir = (int) (255.0*     stress ) ;
  93       int    ig = (int) (255.0*(1.0-stress)) ;
  94       int    ib = ig                         ;
  95       colorstress[i] = gdImageColorAllocate (im, ir, ig, ib );
  96    }
  97    gdImageSetThickness( im, 5 );               // line thickness of 2
  98 }
  99 
 100 //-----------------------------------------------------------------
 101 // draw strut
 102 
 103 void strut( double x0, double y0, double x1, double y1, double nom ) {
 104 
 105    double length  = sqrt( (x0-x1)*(x0-x1) + (y0-y1)*(y0-y1) );
 106    double stress  = (length/nom) - 1.0 ;
 107 
 108    // returned through globals
 109    sx     = (PRELOAD+stress) * (x0-x1)/length ;  
 110    sy     = (PRELOAD+stress) * (y0-y1)/length ;
 111 
 112    int    istress = (int)( MSTRESS*stress + 0.5*(NSTRESS-1) );
 113    int    scolor  ;
 114    if(      istress <  0       ){ scolor = white               ; }
 115    else if( istress >= NSTRESS ){ scolor = white               ; }
 116    else                         { scolor = colorstress[istress]; }
 117 
 118    gdImageLine( im, ((int)x0), ((int)y0), ((int)x1), ((int)y1), scolor );
 119 }
 120 
 121 //--------------------------------------------------------------------
 122 void closeplot() {
 123    sprintf( filename, "%sdir/a%04d.png", NAME, iplot );
 124    pngout = fopen(  filename, "wb");
 125    gdImagePngEx(im, pngout, 1);
 126    fclose(pngout);
 127    gdImageDestroy(im);
 128 }
 129 
 130 //---------------------------------------------------------------------
 131 int main() {
 132 
 133 
 134    // -----------------------------------------------------------------
 135 
 136    initialize();
 137 
 138    // define truss points ( two off screen on each side );
 139    double  deltax = ((double)XSIZE)/((double)(NPOINTS));
 140    double  deltah = 0.5*deltax ;
 141    double  deltay = deltax ;
 142    double  startx = -1.6*deltax ;
 143    double  starty = YH - 0.5*deltay ;
 144    double  diag   = sqrt( deltah*deltah + deltay*deltay );  
 145    double  p                            ;
 146    double  r0    = R0                   ;
 147    double  wavel = WAVEL                ;
 148 
 149    for( iplot=0 ;  iplot < NPLOT ; iplot++ ) {
 150       double ipn = 6*((double)iplot)/((double)NPLOT) ; // 0 to almost 6
 151 
 152       if(      ipn < 1.0 ) {
 153          r0    = 0.0   ;
 154          wavel = WAVEL ;
 155       }
 156       else if( ipn < 2.0 ) {
 157          r0    = R0*(ipn-1.0) ;
 158          wavel = WAVEL ;
 159       }
 160       else if( ipn < 3.0 ) {
 161          r0    = R0    ;
 162          wavel = WAVEL ;
 163       }
 164       else if( ipn < 4.0 ) {
 165          r0    = R0    ;
 166          wavel = 0.5*WAVEL*(5.0-ipn) ;
 167       }
 168       else if( ipn < 5.0 ) {
 169          r0    = R0    ;
 170          wavel = 0.5*WAVEL ;
 171       }
 172       else {
 173          r0 = R0*(6.0-ipn);
 174          wavel = 0.5 * WAVEL ;
 175       }
 176       double  wavem = 2.0*PI/(wavel*XSIZE) ;
 177 
 178       openplot();
 179 
 180       //    t0 ----- t1
 181       //      \     /  
 182       //       \   /
 183       //        \ /
 184       //         b0 ----- b1
 185 
 186    
 187       for( i=0 ; i<NPOINTS+5 ; i++ ) {        // strut points loop
 188          double d0 = deltax*((double)i) ;
 189          double dh = d0 + deltah        ;
 190 
 191          tx[i]   = startx + d0     ;
 192          ty[i]   = starty          ;
 193          p       = wavem*tx[i]     ;
 194          tx[i]  -= r0 * RX * sin( p )   ;
 195          ty[i]  += r0 * RY * cos( p )   ;
 196    
 197          bx[i]   = startx + dh     ;
 198          by[i]   = starty + deltay ;
 199          p       = wavem*bx[i]     ;
 200          bx[i]  += r0 * RX * sin( p )   ;
 201          by[i]  += r0 * RY * cos( p )   ;
 202       }
 203    
 204       for( i=0 ; i<NPOINTS+4 ; i++ ) {
 205          stx[i] = 0.0 ;
 206          sty[i] = -SY ;
 207          sbx[i] = 0.0 ;
 208          sby[i] = +SY ;
 209       }
 210    
 211       // diagonals
 212       gdImageSetThickness( im,  6 );                 // line thickness
 213       for( i=0 ; i<NPOINTS+4 ; i++ ) {
 214          strut( tx[i], ty[i], bx[i],   by[i],  diag ); // t0 to b0
 215          stx[i]   += SS*sx ;
 216          sty[i]   += SS*sy ;
 217          sbx[i]   -= SS*sx ;
 218          sby[i]   -= SS*sy ;
 219    
 220          strut( bx[i], by[i], tx[i+1], ty[i+1], diag ); // b0 to t1
 221          sbx[i]   += SS*sx ;
 222          sby[i]   += SS*sy ;
 223          stx[i+1] -= SS*sx ;
 224          sty[i+1] -= SS*sy ;
 225       }
 226    
 227       // mains
 228       gdImageSetThickness( im, 10 );                 // line thickness
 229       for( i=0 ; i<NPOINTS+4 ; i++ ) {
 230          strut( tx[i], ty[i], tx[i+1], ty[i+1], deltax ); // t0 to t1
 231          stx[i]   += SM*sx ;
 232          sty[i]   -= SM*sy ;
 233          stx[i+1] -= SM*sx ;
 234          sty[i+1] += SM*sy ;
 235    
 236          strut( bx[i], by[i], bx[i+1], by[i+1], deltax ); // b0 to b1
 237          sbx[i]   += SM*sx ;
 238          sby[i]   -= SM*sy ;
 239          sbx[i+1] -= SM*sx ;
 240          sby[i+1] += SM*sy ;
 241       }
 242    
 243       // stress arrows
 244       gdImageSetThickness( im,  4 );                // line thickness
 245    
 246       for( i=1 ; i<NPOINTS+3 ; i++ ) {
 247    
 248          sx0 = ((int)tx[i]) ;
 249          sy0 = ((int)ty[i]) - SOFF  ;
 250          // sx1 = sx0 - (int)stx[i] ;
 251          sx1 = sx0 ;
 252          sy1 = sy0 - (int)sty[i] ;
 253           
 254          gdImageLine( im, sx0, sy0, sx1, sy1, white );
 255          
 256          sx0 = ((int)bx[i]) ;
 257          sy0 = ((int)by[i]) + SOFF ;
 258          // sx1 = sx0 - (int)sbx[i] ;
 259          sx1 = sx0 ;
 260          sy1 = sy0 - (int)sby[i] ;
 261           
 262          gdImageLine( im, sx0, sy0, sx1, sy1, white );
 263       }  
 264       closeplot();
 265    }
 266 
 267    // make flash video
 268    sprintf( png2swf, "png2swf -o %s.swf -r%3d %sdir/*.png", NAME, RATE, NAME );
 269    system( png2swf );
 270 
 271    return(0);
 272 }

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] (2011-07-06 21:21:29, 8.1 KB) [[attachment:tr04.c]]
  • [get | view] (2021-06-20 03:33:19, 184.1 KB) [[attachment:tr04.png]]
  • [get | view] (2011-07-06 21:21:12, 219.4 KB) [[attachment:tr04.swf]]
  • [get | view] (2011-07-03 04:37:15, 44.7 KB) [[attachment:truss1a.png]]
 All files | Selected Files: delete move to page

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