Attachment 'pa04.c'
Download 1 // phased array, 7x7
2 //
3 // compile with: cc -o pa04 pa04.c -lgd -lpng -lm
4 //
5 // Uses the libgd library. For documentation see the file:
6 // /usr/share/doc/gd-*/index.html
7 // or the website: http://www.libgd.org/Reference
8 //
9 // You will need truetype fonts. If you don't have them, you can
10 // copy ../fonts/truetype/.. from openoffice.org to /usr/share/
11 //
12 // Uses swftools to build a swf movie from individual png images
13 // for more information, see http://www.swftools.org/
14 //
15 // This constructs half the slides, then mirrors them
16
17 #include "gd.h"
18 #include "math.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22
23 #define RMMKDIR "rm -rf pa04dir ; mkdir pa04dir"
24 #define PNG2APNG "/usr/local/bin/apngasm phased03.png pa04dir/* 1 10"
25 #define PNGFMT "pa04dir/a%04d.png"
26
27 #define YCENT 130
28 #define WAVELENGTH 104.0 // radio wavelength in pixel units
29 #define SPACE 40.0 // array spacing in pixel units
30 #define NPICS 40 // the number of frames
31
32 #define TANG 30.0 // target angle in degrees
33
34 #define SPOT 16 // size of drawn spot
35 #define ASIZE 7 // number of elements per side
36 #define XSIZE 1000 // display window in pixels
37 #define YSIZE 750 // display window in pixels
38 #define FNT "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"
39 #define FSZ 10
40
41 // ==========================================================================
42
43 int main() {
44
45 gdImagePtr im ; // pixel image map used by gd
46 double space = (double) SPACE ; // nominal spacing between server-sats
47 double yacent = (double) YCENT ; // center of array drawing
48 double xsize = (double) XSIZE ; // width of pixels
49 double ysize = (double) YSIZE ; // height of pixels
50 double pi2 = 8.0*atan(1.0) ; // two times PI
51 double d2r = pi2/360.0 ; // degrees to radians conversion factor
52 int oxc = XSIZE/2 ; // center of output pixel array
53 int oyc = YCENT ; // center of output pixel array
54 int ybot = 2*YCENT ; //
55 double xacent = (double) oxc ; // real ""
56 double k = pi2/WAVELENGTH ; // wavenumber
57 FILE *pngout ; // file handle for PNG output frame
58 char dirname[80] ; // frame output directory
59 char framename[80] ; // directory/name of frame
60 char labstring[80] ; // used for labelling
61 char link0[80] ; // directory/name of source
62 char link1[80] ; // directory/name of target hardlink
63 int black, sun1, white, red ; // color map numbers
64 int green, blue, gray ; // color map numbers
65 int dgray, trans, olp, olm ; // color map numbers
66 int gcolor[256] ; // color map numbers
67 double ax[ASIZE][ASIZE] ; // x position of element
68 double ay[ASIZE][ASIZE] ; // y position of element
69 double ap[ASIZE][ASIZE] ; // phase of element
70 double xx, yy ; //
71 int ix, iy ; // integer counters
72 int ox, oy ; // output pixel position
73 int i ; // general counter
74 int fs ; // big label font size
75 int icolor ; //
76 int cir ; //
77 int mag ; // pixel magnitude, 0-255
78 double ss ; // summed sin component
79 double phase ; // phase
80 double gphase ; // global time phase
81 double axx ;
82 double sxx ;
83 double stang=sin(d2r*TANG) ;
84 double ctang=cos(d2r*TANG) ;
85 int frame ; // animation frame count
86 int pct=0 ; // percentage of complete frame
87 int pct0=0 ; // previous percentage
88 double dis ;
89 double ffrac ; // frame fraction, 0 to 1
90 int asize = ASIZE ; // nominal spacing of server-sats
91 int asize2 = asize*asize ; //
92 double offset=0.5*(1.0-asize) ; // offsets counter integer to center
93 double norm ; // normalize
94
95 norm=1.0/(double)asize2 ; // normalize
96
97 // set up target directory
98
99 system( RMMKDIR );
100
101 // define array with even spacing ---------------------------------------
102
103 for( iy=0; iy<asize ; iy++ ) {
104 yy = space*(((double)iy)+offset) ;
105 for( ix=0; ix<asize ; ix++ ) {
106 // compute array positions
107 xx = space*(((double)ix)+offset) ;
108 ax[ix][iy] = xx+xacent ;
109 ay[ix][iy] = yy+yacent ;
110 ap[ix][iy] = k*(ctang*yy+stang*xx) ;
111 }
112 }
113
114 #ifdef DEBUG
115 for( iy=0; iy<asize ; iy++ ) {
116 for( ix=0; ix<asize ; ix++ ) {
117 printf( "%2d%2d%10.2f%10.2f%10.4f\n",
118 ix,iy, ax[ix][iy], ay[ix][iy], ap[ix][iy] );
119 }
120 }
121 #endif
122
123 // outer loop, sinusodial random factor change --------------------------
124 // from 0 to 1 to 0
125
126 for( frame=0 ; frame<NPICS ; frame++ ) {
127
128 ffrac = ((double)frame)/((double)NPICS);
129 gphase = pi2*ffrac ;
130
131 // set up array and define colors -----------------------------------------
132
133 im = gdImageCreateTrueColor(XSIZE, YSIZE );
134
135 // Allocate standard colors
136 black = gdImageColorAllocate(im, 0, 0, 0);
137 white = gdImageColorAllocate(im, 255, 255, 255);
138 olm = gdImageColorAllocate(im, 32, 0, 0);
139 olp = gdImageColorAllocate(im, 255, 224, 224);
140 sun1 = gdImageColorAllocate(im, 51, 51, 102);
141 red = gdImageColorAllocate(im, 255, 0, 0);
142 green = gdImageColorAllocate(im, 0, 255, 0);
143 blue = gdImageColorAllocate(im, 0, 0, 255);
144 gray = gdImageColorAllocate(im, 128, 128, 128);
145 dgray = gdImageColorAllocate(im, 48, 48, 48);
146 trans = gdImageColorAllocate(im, 1, 1, 1);
147
148 // allocate gray scale array
149 for( icolor=0 ; icolor<256 ; icolor++ ) {
150 gcolor[icolor] = gdImageColorAllocate(im, icolor, icolor, icolor);
151 }
152
153 // now compute phases
154
155 for( ox=0 ; ox<XSIZE ; ox++ ) {
156 pct0 = pct ;
157 pct = (100*ox)/XSIZE ;
158
159 #ifndef SINGLE
160 if( pct != pct0 ) {
161 printf( "%04d/%04d%4d\r", frame, NPICS, pct ) ;
162 fflush(stdout);
163 }
164 #endif
165 for( oy=0 ; oy<YSIZE ; oy++ ) {
166
167 ss = 0.0 ; // sum the components
168
169 // compute z distance and phase of each source to plane
170 for( iy=0; iy<asize ; iy++ ) {
171 for( ix=0; ix<asize ; ix++ ) {
172 yy = (double) (oy-ay[ix][iy]);
173 xx = (double) (ox-ax[ix][iy]);
174 dis = sqrt( xx*xx+yy*yy ) ;
175 if( dis > 0.9 ) {
176 // not inverse square!!!
177 axx = -gphase + ap[ix][iy] + k*dis ;
178 sxx = sin( axx );
179 ss += sxx ;
180 } else {
181
182 #ifdef DEBUG
183 printf( "%6d%4d", ox, oy );
184 #endif
185 }
186
187 #ifdef DEBUG
188 if( (oy==384) && (ox==512) ) {
189 printf(
190 "%2d%2d %7.0f%7.0f%10.4f %10.4f%10.4f%10.4f%10.4f\n",
191 ix,iy, xx, yy, sxx,
192 axx, ap[ix][iy], k, dis );
193 }
194 #endif
195 } }
196
197 mag = (int)( 127.0*(1.0+norm*ss ) );
198
199 #ifdef DEBUG
200 if( ( oy == 384 ) && (ox == 512 ) ) {
201 printf( "%4d%4d%13.4f%13.4f%12d\n", ox, oy, ss, gphase, mag );
202 }
203 printf("%5d",mag );
204 #endif
205
206 if( mag < 0 ) {
207 gdImageSetPixel( im, ox, oy, olm ) ;
208 }
209 else if( mag > 255 ) {
210 gdImageSetPixel( im, ox, oy, olp ) ;
211 }
212 else {
213 gdImageSetPixel( im, ox, oy, gcolor[mag] ) ;
214 }
215 } }
216
217 #ifdef DEBUG
218 printf("\n%4d\n", frame);
219 #endif
220
221 // Slide Label ----------------------------------------------------------
222
223 fs= ybot/8 ;
224 gdImageStringFT( im, NULL, // imagespace, bounding box
225 white , FNT, fs, 0.0, // color, font, fontsize, angle
226 15 , fs+15 , // x, y
227 "Phased Array" ); // text
228
229 // draw array ------------------------------------------------------------
230
231 for( iy=0; iy<asize ; iy++ ) {
232 for( ix=0; ix<asize ; ix++ ) {
233 ox = (int) ax[ix][iy] ;
234 oy = (int) ay[ix][iy] ;
235 cir = SPOT+6 ;
236 gdImageFilledEllipse(im, ox, oy, cir, cir, black );
237
238 cir = SPOT ;
239 icolor = (int) ( 127.0*(1.0+0.9*sin(gphase+ap[ix][iy] ) ) ) ;
240 gdImageFilledEllipse(im, ox, oy, cir, cir, gcolor[icolor] );
241 } }
242
243 // output the frame ------------------------------------------------------
244
245 sprintf( framename, PNGFMT , frame );
246 pngout = fopen( framename, "wb");
247 gdImagePngEx( im, pngout, 1 );
248 gdImageDestroy(im);
249 fclose(pngout);
250
251 } // end of single frame loop
252
253 printf( "frames complete, now run\n");
254 printf( "%s\n", PNG2APNG );
255 }
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.You are not allowed to attach a file to this page.