/* field.c - Don Yang (uguu.org) 11/09/08 */ #include #include #include #include #include #define RAND() ((double)rand() / (double)RAND_MAX) static void GenerateImage(int width, int height, FILE *outfile) { double x_phase, y_phase, d_phase, x_period, y_period, d_period; double pi, sx, sy, sd; int pscale, x, y, pixel; pscale = width; if( height > width ) pscale = height; srand((unsigned)time(NULL)); pi = atan2(0.0, -1.0); x_phase = 2.0 * pi * RAND(); y_phase = 2.0 * pi * RAND(); d_phase = 2.0 * pi * RAND(); x_period = (8.0 + (8.0 * RAND())) / pscale; y_period = (8.0 + (8.0 * RAND())) / pscale; d_period = (8.0 + (8.0 * RAND())) / pscale; for(y = 0; y < height; y++) { sy = sin((y + y_phase) * y_period); for(x = 0; x < width; x++) { sx = sin((x + x_phase) * x_period); sd = sin((x + y + d_phase) * d_period); pixel = (int)(255.0 * (sx + sy + sd + 3.0) / 6.0); (void)fputc(pixel, outfile); } } } int main(int argc, char **argv) { char header[32]; int width, height; FILE *outfile; if( argc < 4 ) { printf("%s \n", *argv); return 1; } width = atoi(argv[1]); height = atoi(argv[2]); if( width < 1 || height < 1 ) { printf("Invalid image size: %s %s\n", argv[1], argv[2]); return 1; } if( (outfile = fopen(argv[3], "wb")) == NULL ) { printf("Error writing %s\n", argv[3]); return 1; } /*@i1@*/sprintf(header, "P5\n%d %d\n255\n", width, height); (void)fwrite(header, strlen(header), 1, outfile); GenerateImage(width, height, outfile); (void)fclose(outfile); return 0; }