home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / vifs / ifsrand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-14  |  1.7 KB  |  75 lines

  1. /*
  2.  *    ifsrand.c  --  generate a random ifs.
  3.  *
  4.  *    4 june 1989  Olle Olsson
  5.  */
  6.  
  7. #include <stdlib.h>
  8. #include <math.h>
  9. #include "ifs.h"
  10.  
  11. static double dbrand( double sup );
  12.  
  13. static double dbrand( sup )
  14. double sup;        /* supremum */
  15. {
  16. return ((sup * rand()) / MAXRAND);
  17. }
  18.  
  19. int ifsrand( dp, minsize, maxsize, show )
  20. ifsdes *dp;        /* empty descriptor */
  21. int minsize;        /* min size of the transform */
  22. int maxsize;        /* max size of the transform */
  23. int show;        /* trace flag */
  24. {
  25. area *ap = &dp -> area;        /* area */
  26. transform *tp = dp -> tp;    /* empty transform */
  27. rgb *rp;            /* tmp */
  28. int ntr;            /* number of transforms */
  29. int i;
  30. double da;
  31.  
  32. /* return value is actual number of transforms */
  33.  
  34. /* set a suitable area */
  35. ap -> xlow = -1;
  36. ap -> xlen = 2;
  37. ap -> ylow = -1;
  38. ap -> ylen = 2;
  39.  
  40. /* select count */
  41. if (minsize >= maxsize)
  42.     ntr = maxsize;
  43. else     ntr = rand() % (1 + maxsize - minsize) + minsize;
  44.  
  45. /* make transforms */
  46. /* check that it is contractive  (det( A ) < 1) */
  47. /* check that it isn't too contractive (det( A ) > 0) */
  48. for (i = 0; i < ntr; ++i, ++tp) do
  49.     {
  50.     tp -> a11 = dbrand( 2 ) - 1;
  51.     tp -> a12 = dbrand( 2 ) - 1;
  52.     tp -> a21 = dbrand( 2 ) - 1;
  53.     tp -> a22 = dbrand( 2 ) - 1;
  54.     tp -> b1  = dbrand( 1 ) - .5;
  55.     tp -> b2  = dbrand( 1 ) - .5;
  56.     tp -> prob = dbrand( 1 ) + .1;
  57.  
  58.     /* take a nice color, but not black since the background is black */
  59.     tp -> color = i % (dp -> clrsize - 1) + 1;
  60.     rp = &dp -> colors[tp -> color];
  61.     do randrgb( rp );
  62.     while (rp -> r == 0 && rp -> g == 0 && rp -> b == 0);
  63.  
  64.     /* use group 0 only */
  65.     tp -> group = 0;
  66.     
  67.     da = dett( tp );
  68.     /* */
  69.     if (show) printf( "tr %d det %g\n", i, da );
  70.     /* */
  71.     } while (da >= .5 || da < .05);
  72.  
  73. return (ntr);
  74. }
  75.