home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1991 / 05 / titel / biomorph.c next >
Encoding:
C/C++ Source or Header  |  1991-01-03  |  4.6 KB  |  192 lines

  1. /**********************************************************/
  2. /*                                                                 */
  3. /*                         BIOMORPH                       */
  4. /* Ein Programm zur Erzeugung von sog. 'Biomorphen' nach  */
  5. /*   C. A. Pickover, beschrieben von A. K. Dewdney in     */
  6. /* 'Spektrum der Wissenschaft', Sonderheft 10, S. 60 ff.  */
  7. /*                                                                 */
  8. /*      Dient zur Demonstration der INCLUDE-Dateien       */
  9. /*              'complex.c' und 'complex.h'.              */
  10. /*                                                                 */
  11. /*                 Sprache: Turbo C 2.0                      */
  12. /*               Autor: Ralf Baethke-Franke                 */
  13. /*              (C) 1991 Ralf Baethke-Franke              */
  14. /*                                                                 */
  15. /**********************************************************/
  16.  
  17. #include <graphics.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <math.h>
  21. #include <dos.h>
  22. #include "complex.c"
  23.  
  24. /* Globale Variable */
  25.  
  26. compl z,z0,c;
  27. int j,k,n,iter,farben;
  28. float rmin,rmax,imin,imax;
  29. int funktion;
  30. int xasp,yasp,x,y;
  31.  
  32. /* Funktionsprototypen */
  33.  
  34. void Rechnen     ( void );
  35. void main        ( void );
  36. void Eingabe     ( void );
  37. void Grafik      ( void );
  38. int  round       ( double v );
  39. void Einheiten   ( int g, double b, double c, int x1,
  40.                                                     int y1, char achse );
  41. void Koordinaten ( void );
  42.  
  43. /* Funktionen */
  44.  
  45. void Rechnen ( void )
  46. {
  47.     z = sqr_c(z);
  48. }
  49.  
  50. void main ( void )
  51. {
  52.     double a,b;
  53.  
  54.     Eingabe ();
  55.     sound ( 1000 ); delay ( 10 ); nosound ();
  56.     Grafik ();
  57.     Koordinaten ();
  58.     a = ( rmax - rmin ) / x;
  59.     b = ( imax - imin ) / y;
  60.     for ( j = 0; j < x; j++ )
  61.     {
  62.         for ( k = 0; k < y; k++ )
  63.         {
  64.             z0.re = rmin + a * j;
  65.             z0.im = imin + b * k;
  66.             z = z0;
  67.             for ( n = 1; n < iter + 1; n++)
  68.             {
  69.  
  70.                 if ( kbhit () != 0 )
  71.                 {
  72.                     if ( getch () == 27 )
  73.                     {
  74.                         restorecrtmode ();
  75.                         exit ( 0 );
  76.                     }
  77.                 }
  78.                 Rechnen ();
  79.                 z = add_c ( z, c );
  80.  
  81.                 if ( abs ( z.re ) > 10 || abs ( z.im ) > 10 )
  82.                     break;
  83.             }
  84.             if ( abs ( z.re ) < 10 || abs ( z.im ) < 10 )
  85.             {
  86.                 while ( n > farben ) n -= farben;
  87.                 if ( n == farben ) n -= 1;
  88.                 if ( getpixel ( j + 50, y + 25 - k ) != WHITE )
  89.                     putpixel ( j + 50, y + 25 - k, n );
  90.             }
  91.         }
  92.     }
  93.     sound ( 2000 ); delay ( 500 ); nosound ();
  94.     getch ();
  95. }
  96.  
  97. void Eingabe ( void )
  98. {
  99.     float c1;
  100.  
  101.     clrscr ();
  102.     gotoxy ( 32, 1 );
  103.     highvideo ();
  104.     cprintf ( "B I O M O R P H" );
  105.     gotoxy ( 32, 2 );
  106.     cprintf ( "===============" );
  107.     lowvideo ();
  108.     printf ( "\n\nUntere Grenze für die reelle Achse   : " );
  109.     scanf ( "%f", &rmin );
  110.     printf ( "Obere Grenze für die reelle Achse    : " );
  111.     scanf ( "%f", &rmax );
  112.     printf ( "Untere Grenze für die imaginäre Achse: " );
  113.     scanf ( "%f", &imin );
  114.     imax = imin + ( rmax - rmin );
  115.     printf ( "Obere Grenze für die imaginäre Achse : %.2f\n",
  116.                                                                       imax );
  117.     printf ( "\nRealteil der Konstante   : " );
  118.     scanf ( "%f", &c1 );
  119.     c.re = c1;
  120.     printf ( "Imaginärteil der Konstante: " );
  121.     scanf ( "%f", &c1 );
  122.     c.im = c1;
  123.     printf ( "\nAnzahl Iterationen: " );
  124.     scanf ( "%d", &iter );
  125. }
  126.  
  127. void Grafik ( void )
  128. {
  129.     int gd, gm;
  130.  
  131.     detectgraph ( &gd, &gm );
  132.     initgraph ( &gd, &gm, "" );
  133.     getaspectratio ( &xasp, &yasp );
  134.     y = getmaxy () - 50;
  135.     x = (int) ( ( long ) y * ( long ) yasp / ( long ) xasp );
  136.     farben = getmaxcolor () + 1;
  137. }
  138.  
  139. int round ( double v )
  140. {
  141.     return ( floor ( v + 0.5 ) );
  142. }
  143.  
  144. void Einheiten ( int g, double b, double c, int x1, int y1,
  145.                                                                  char achse)
  146. {
  147.     int a, e;
  148.  
  149.     if (achse == 'x')
  150.     {
  151.         for ( a = 0; a <= g; a++)
  152.         {
  153.             e = x1 + round ( ( a * b ) / ( 10 * c ) );
  154.             line ( e, y1 - 3, e, y1 + 3 );
  155.             if ( !(a % 5) )  line ( e, y1 - 4, e, y1 + 4 );
  156.             if ( !(a % 10) ) line ( e, y1 - 5, e, y1 + 5 );
  157.         }
  158.     }
  159.  
  160.     if ( achse == 'y' )
  161.     {
  162.         for ( a = 0; a <= g; a++)
  163.         {
  164.             e = y1 - round ( ( a * b ) / ( 10 * c ) );
  165.             line ( x1 - 3, e, x1 + 3, e );
  166.             if ( !(a % 5) )  line ( x1 - 4, e, x1 + 4, e );
  167.             if ( !(a % 10) ) line ( x1 - 5, e, x1 + 5, e );
  168.         }
  169.     }
  170. }
  171.  
  172. void Koordinaten ( void )
  173. {
  174.     int x1, y1;
  175.     double r, i;
  176.  
  177.     setcolor ( WHITE );
  178.     rectangle ( 47, 22, x+53, y+28 );
  179.     r = rmax - rmin;
  180.     x1 = 50 + round ( ( -rmin ) * x / r );
  181.     line ( x1, 22, x1, y + 28 );
  182.     i = imax - imin;
  183.     y1 = 25 + round ( imax * y / i );
  184.     line ( 47, y1, x+53, y1);
  185.  
  186.     Einheiten (  round ( 10 * rmax ), x, r, x1, y1, 'x');
  187.     Einheiten ( -round ( 10 * rmin ), x, -r, x1, y1, 'x');
  188.     Einheiten (  round ( 10 * imax ), y, i, x1, y1, 'y');
  189.     Einheiten ( -round ( 10 * imin ), y, -i, x1, y1, 'y');
  190. }
  191. /****************** Ende von BIOMORPH.C ******************/
  192.