nicer formatting
[tridge/junkcode.git] / fern.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <time.h>
5 #include <tridgec/tridge.h>
6
7 #define NUM_RAN 200
8 #define MAX_COLOUR 255
9 #define XOFS 0
10 #define YOFS 0
11
12 typedef unsigned char PIXEL;
13
14
15
16 /*******************************************************************
17 write a pixel matrix
18 *******************************************************************/
19 BOOL write_pix_matrix(PIXEL **mat,int size,CONST char *fname)
20 {
21 FILE *file;
22 file = file_open(fname,"w");
23 fwrite((char *)&mat[0][0],sizeof(PIXEL),size*size,file);
24 file_close(file);
25 return(True);
26 }
27
28 /*******************************************************************
29 read a pixel matrix
30 *******************************************************************/
31 BOOL read_pix_matrix(PIXEL **mat,int size,CONST char *fname)
32 {
33 FILE *file;
34 file = file_open(fname,"r");
35 fread((char *)&mat[0][0],sizeof(PIXEL),size*size,file);
36 file_close(file);
37 return(True);
38 }
39
40
41 /*******************************************************************
42 make a 2 D  pixel matrix
43 ********************************************************************/
44 PIXEL **pmatrix2D(int dim1,int dim2)
45 {
46 PIXEL **ret;
47 int i,j;
48
49 ret = (PIXEL **)any_matrix(2,sizeof(PIXEL),dim1,dim2);
50 if (ret == NULL) return(NULL);
51
52 for (i=0;i<dim1;i++)
53 for (j=0;j<dim2;j++)
54         ret[i][j] = 0.0;
55 return(ret);
56 }
57
58
59
60
61 int N,NUM_ITTR;
62 BOOL read_in;
63
64 int main(int argc,char *argv[])
65 {
66 float a[4],b[4],c[4],d[4],e[4],f[4];
67 int randx , randy;
68 int loop,i,j;
69 PIXEL **oldimage;
70 PIXEL **newimage;
71 int COL1 = 180;
72
73 if (argc<5)
74         {
75         fprintf(debugfile,"Usage: %s N niter read_in base_colour\n",argv[0]);
76         exit(0);
77         }
78
79 N = atoi(argv[1]);
80 NUM_ITTR = atoi(argv[2]);
81 read_in = atoi(argv[3]);
82 COL1 = atoi(argv[4]);
83
84 xStartDisplay(N,N,argc,argv);
85
86         oldimage = pmatrix2D(N,N);
87         newimage = pmatrix2D(N,N);
88
89
90         if ((oldimage == NULL) || (newimage == NULL))
91                 {
92                 fprintf(debugfile,"Help!!!");
93                 exit(0);
94                 }
95         
96         /* initialize the transformation values */
97
98         a[0] = 0 ; a[1] = 0.85 ; a[2] = 0.2 ; a[3] = -0.15 ;
99         b[0] = 0 ; b[1] = 0.04 ; b[2] = -0.26 ; b[3] = 0.28;
100         c[0] = 0 ; c[1] = -0.04 ; c[2] = 0.23 ; c[3] = 0.26;
101         d[0] = 0.16 ; d[1] = 0.85 ; d[2] = 0.22 ; d[3] = 0.24;
102         e[0] = 0 ; e[1] = 0 ; e[2] = 0 ; e[3] = 0 ;
103         f[0] = 0 ; f[1] = 1.6 ; f[2] = 1.6 ; f[3] = 0.44;
104
105
106 if (read_in)
107         read_pix_matrix(oldimage,N,"fern.mat");
108 else
109 {
110         /* set up initial configuration */
111
112         start_random();
113         for ( i = 0 ; i < NUM_RAN ; i++)
114         {
115                 randx = rand_float(0.0,N-1);
116                 randy = rand_float(0.0,N-1);
117                 oldimage[randx][randy] = 1;
118         }
119 }
120
121
122         for(loop=0;loop<NUM_ITTR;loop++)
123         {
124         int changed=0;
125         fprintf(stdout,"Loop %d\n",loop);
126         for(i=0; i < N; i++)
127                 for (j=0; j < N; j++)
128                 {
129
130 #define OK(ii,jj) (!((ii<0) || (jj<0) || (ii>=N) || (jj>=N)))
131 #define ISHIFT 200
132 #define JSHIFT 30
133 #define SCALE   70 
134                         if (oldimage[i][j])
135                         {
136                         int k;
137                         int ii,jj;
138                         for (k=0;k<4;k++)
139                                 {
140                                 ii = (a[k]*(i-ISHIFT) + b[k]*(j-JSHIFT) + e[k]*SCALE);
141                                 jj = (c[k]*(i-ISHIFT) + d[k]*(j-JSHIFT) + f[k]*SCALE);
142                                 ii += ISHIFT;
143                                 jj += JSHIFT;
144                                 if (OK(ii,jj))
145                                         newimage[ii][jj] = oldimage[ii][jj]+1;
146                                 }
147                         }
148                 }
149
150
151
152         xClearDisplay(); 
153
154         for(i=0; i<N ; i++)
155         for(j=0;j<N;j++)
156                 {
157                 PIXEL old,new;
158                 old = oldimage[i][j];
159                 new = newimage[i][j];
160
161                         if ((old != new) && (old*new == 0))
162                                 changed++;
163                         
164                         if (new)
165                           {
166                             xSetForeground(COL1 + new - 1);
167                             xPutPoint(XOFS + i,YOFS + j);
168                           }
169
170                         oldimage[i][j] = new;
171                         newimage[i][j] = 0;
172                 }
173         xSync(False);
174
175         if ((loop % 10 == 0) || (changed == 0))
176                 write_pix_matrix(oldimage,N,"fern.mat");
177
178         fprintf(stdout,"\n Changed pixels = %d ",changed);
179         if (changed == 0) break;
180 }
181
182 free(oldimage);
183 free(newimage);
184
185 xWaitEvent(eKeyPress);
186
187 xEndDisplay();
188 return(0);
189 }
190
191