/* -*- Mode:C; Coding:us-ascii-unix; fill-column:132 -*- */
/**********************************************************************************************************************************/
/**
   @file      lorenz1.c
   @author    Mitch Richling <http://www.mitchr.me/>
   @Copyright Copyright 1994,1997,2014 by Mitch Richling.  All rights reserved.
   @Revision  $Revision: 1.1 $ 
   @SCMdate   $Date: 2014/10/10 19:36:49 $
   @brief     Generate a PovRay include file for the lorenz strange attractor. @EOL
   @Keywords  lorenz strange attractor
   @Std       C89

   This is a very simple program to generate a curve based on Lorenz's attractor.  It uses Euler's method to solve the equations.
   The "curve" consists of a series of spheres placed at each point found on the curve.  The spheres are connected with cylinders.
   Alternate implimentations: lorenz1.pl, lorenz1.inc
              
***********************************************************************************************************************************/

/**********************************************************************************************************************************/
#include <stdio.h>

int main() {
  int maxBalls    = 10000;
  double tDelta   = 0.01;
  double x = 0.1;
  double y = 0.0;
  double z = 0;
  double a = 10;
  double b = 28;
  double c = 8.0 / 3.0;
  int numBalls;
  double xNew, yNew, zNew;
  fprintf(stderr, "Computation starting...\n");
  printf("sphere {<%f,%f,%f>, %f texture { crvTex } }\n", x, y, z, 0.2);
      
  for(numBalls=0;numBalls<maxBalls;numBalls++) {
    xNew = x + a*(y-x)*tDelta;
    yNew = y + (x*(b-z)-y)*tDelta;
    zNew = z + (x*y-c*z)*tDelta;

    /* Display 20 "status" messages. */
    if((numBalls % (int)(maxBalls/20)) == 0)
      fprintf(stderr, "Step: %6d  tDelta: %15.3f x: %15.2f y: %15.2f z: %15.2f\n", numBalls, tDelta, x, y, z);

    printf("sphere {<%f,%f,%f>, %f texture { crvTex } }\n", xNew, yNew, zNew, 0.2);
    printf("cylinder {<%f,%f,%f>, <%f,%f,%f>, %f texture { crvTex } }\n", x, y, z, xNew, yNew, zNew, 0.2);
    
    x=xNew;
    y=yNew;
    z=zNew;
  }

  return 0;
}