Search:

Return to previous page

Contents of file 'boxcount/koch/koch.c':



    1   /*-----------------------------------------------------------------------------
    2   | File: koch.c [ANSI-C conforming source code]
    3   | Created: May 8, 2006, Fredrik Jonsson <fj@phys.soton.ac.uk>
    4   | Last modified: May 8, 2006, Fredrik Jonsson <fj@phys.soton.ac.uk>
    5   | Description:
    6   |    The KOCH program creates data sets corresponding to the Koch fractal,
    7   |    for the purpose of acting as test objects for the program BOXCOUNT,
    8   |    which calculates box-counting estimates to the fractal dimension.
    9   |    The program is simply executed by 'koch <N>', where <N> is an integer
   10   |    describing the maximum depth of recursion that is to be used in the
   11   |    generation of the data set for the fractal. If invoked without any
   12   |    arguments present at the command line, a default value of <N>=6 is
   13   |    assumed. The generated data stream is sent to standard terminal output,
   14   |    easy to pipe into files (as suitable for the BOXCOUNT program) or as
   15   |    feeds to other programs of data analysis.
   16   |
   17   | Compile with: gcc -O2 -g -Wall -pedantic -ansi koch.c -o koch -lm
   18   |
   19   | Copyright (C) 2006 Fredrik Jonsson <fj@phys.soton.ac.uk>
   20   =============================================================================*/
   21   #include <math.h>
   22   #include <stdio.h>
   23   
   24   extern char *optarg;
   25   
   26   void kochsegment(double xa,double ya,double xb,double yb,
   27      int depth,int maxdepth) {
   28      double xca,yca,xcb,ycb,xcc,ycc;
   29      if (depth==maxdepth) {
   30         fprintf(stdout,"%2.8f %2.8f\n",xb,yb);
   31      } else {
   32         xca=xa+(xb-xa)/3.0;
   33         yca=ya+(yb-ya)/3.0;
   34         xcb=xb-(xb-xa)/3.0;
   35         ycb=yb-(yb-ya)/3.0;
   36         xcc=(xa+xb)/2.0-(yb-ya)/(2.0*sqrt(3.0));
   37         ycc=(ya+yb)/2.0+(xb-xa)/(2.0*sqrt(3.0));
   38         kochsegment(xa,ya,xca,yca,depth+1,maxdepth);
   39         kochsegment(xca,yca,xcc,ycc,depth+1,maxdepth);
   40         kochsegment(xcc,ycc,xcb,ycb,depth+1,maxdepth);
   41         kochsegment(xcb,ycb,xb,yb,depth+1,maxdepth);
   42      }
   43   }
   44   
   45   int main(int argc, char *argv[]) {
   46      int maxdepth=6;
   47      if (argc>1) sscanf(argv[1],"%d",&maxdepth);
   48      if (maxdepth>0) {
   49         fprintf(stdout,"%2.8f %2.8f\n",0.0,1.0);
   50         kochsegment(0.0,1.0,sqrt(3.0)/2.0,-0.5,1,maxdepth);
   51         kochsegment(sqrt(3.0)/2.0,-0.5,-sqrt(3.0)/2.0,-0.5,1,maxdepth);
   52         kochsegment(-sqrt(3.0)/2.0,-0.5,0.0,1.0,1,maxdepth);
   53      }
   54      return(0);
   55   }
   56   

Return to previous page

Generated by ::viewsrc::

Last modified Wednesday 15 Feb 2023