/*----------------------------------------------------------------------------- | File: koch.c [ANSI-C conforming source code] | Created: May 8, 2006, Fredrik Jonsson | Last modified: May 8, 2006, Fredrik Jonsson | Description: | The KOCH program creates data sets corresponding to the Koch fractal, | for the purpose of acting as test objects for the program BOXCOUNT, | which calculates box-counting estimates to the fractal dimension. | The program is simply executed by 'koch ', where is an integer | describing the maximum depth of recursion that is to be used in the | generation of the data set for the fractal. If invoked without any | arguments present at the command line, a default value of =6 is | assumed. The generated data stream is sent to standard terminal output, | easy to pipe into files (as suitable for the BOXCOUNT program) or as | feeds to other programs of data analysis. | | Compile with: gcc -O2 -g -Wall -pedantic -ansi koch.c -o koch -lm | | Copyright (C) 2006 Fredrik Jonsson =============================================================================*/ #include #include extern char *optarg; void kochsegment(double xa,double ya,double xb,double yb, int depth,int maxdepth) { double xca,yca,xcb,ycb,xcc,ycc; if (depth==maxdepth) { fprintf(stdout,"%2.8f %2.8f\n",xb,yb); } else { xca=xa+(xb-xa)/3.0; yca=ya+(yb-ya)/3.0; xcb=xb-(xb-xa)/3.0; ycb=yb-(yb-ya)/3.0; xcc=(xa+xb)/2.0-(yb-ya)/(2.0*sqrt(3.0)); ycc=(ya+yb)/2.0+(xb-xa)/(2.0*sqrt(3.0)); kochsegment(xa,ya,xca,yca,depth+1,maxdepth); kochsegment(xca,yca,xcc,ycc,depth+1,maxdepth); kochsegment(xcc,ycc,xcb,ycb,depth+1,maxdepth); kochsegment(xcb,ycb,xb,yb,depth+1,maxdepth); } } int main(int argc, char *argv[]) { int maxdepth=6; if (argc>1) sscanf(argv[1],"%d",&maxdepth); if (maxdepth>0) { fprintf(stdout,"%2.8f %2.8f\n",0.0,1.0); kochsegment(0.0,1.0,sqrt(3.0)/2.0,-0.5,1,maxdepth); kochsegment(sqrt(3.0)/2.0,-0.5,-sqrt(3.0)/2.0,-0.5,1,maxdepth); kochsegment(-sqrt(3.0)/2.0,-0.5,0.0,1.0,1,maxdepth); } return(0); }