home *** CD-ROM | disk | FTP | other *** search
/ Rat's Nest 1 / ratsnest1.iso / prgmming / c / magnets.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-19  |  8.4 KB  |  244 lines

  1. //
  2.  
  3. //+-----------------------------------------------------------------+
  4.  
  5. //+ Program MAGNETS.CPP                                             +
  6.  
  7. //+ Plots 18 magnet-like fractals in the x-plane for different q's. +
  8.  
  9. //+ (16 for Model I, 2 for Model II)                                +
  10.  
  11. //+                                                                 +
  12.  
  13. //+ By: Fausto Arinos de Almeida Barbuto                            +
  14.  
  15. //+ E-mail: BJ06@C53000.PETROBRAS.ANRJ.BR                           +
  16.  
  17. //+ Rio de Janeiro, BRAZIL, April 4th 1994                          +
  18.  
  19. //+                                                                 +
  20.  
  21. //+ Needs SVGA256.H and SVGA256.BGI, supports up to five screens.   +
  22.  
  23. //+                                                                 +
  24.  
  25. //+ REFERENCE: Peitgen, H.-O. and Richter, P.H.:                    +
  26.  
  27. //+            "The Beauty of Fractals", Springer-Verlag, 1986,     +
  28.  
  29. //+            pg. 194.                                             +
  30.  
  31. //+-----------------------------------------------------------------+
  32.  
  33. //
  34.  
  35. #include <stdio.h>
  36.  
  37. #include "svga256.h"
  38.  
  39. #include <conio.h>
  40.  
  41. #include <graphics.h>
  42.  
  43. #include <math.h>
  44.  
  45. #include <complex.h>
  46.  
  47.  
  48.  
  49. //void far initgraph(int far *,int far *,char far *);
  50.  
  51.  
  52.  
  53. int Vid; // Global variable //
  54.  
  55.  
  56.  
  57. int huge DetectVGA256()
  58.  
  59. {
  60.  
  61.   printf("\n Which video mode would you like to use? \n\n");
  62.  
  63.   printf(" 0 - 320x200x256\n");
  64.  
  65.   printf(" 1 - 640x400x256\n");
  66.  
  67.   printf(" 2 - 640x480x256\n");
  68.  
  69.   printf(" 3 - 800x600x256\n");
  70.  
  71.   printf(" 4 - 1024x768x256\n\n>");
  72.  
  73.   scanf("%d",&Vid);
  74.  
  75.   if((Vid<0) || (Vid)>4) Vid = 2;
  76.  
  77.   return Vid;
  78.  
  79. }
  80.  
  81.  
  82.  
  83. void main()
  84.  
  85. {
  86.  
  87.       double xmin, xmax, ymin, ymax, fact=1.0;
  88.  
  89.       double ypy, x, y, r, deltap, deltaq;
  90.  
  91.       int istart, maxiter, Model;
  92.  
  93.       register int npix, npiy, k, np, nq, npy, ipen;
  94.  
  95.       complex c, z, q, a, b, ab, z2;
  96.  
  97.       int graphdriver=DETECT, graphmode, index;
  98.  
  99.  
  100.  
  101.       clrscr();
  102.  
  103.       printf("\n  MAGNET-LIKE MODELS");
  104.  
  105.       printf("\n  Reference: Peitgen, H.-O., and Richter, P.H. :");
  106.  
  107.       printf("\n  _The Beauty of Fractals_, Springer-Verlag, 1986, pg. 194");
  108.  
  109.       printf("\n\n  By Fausto A. A. Barbuto, April 4, 1994");
  110.  
  111.       printf("\n  E-mail: BJ06@C53000.PETROBRAS.ANRJ.BR \n\n\n");
  112.  
  113.       printf("\n  SELECT A MODEL TYPE: \n");
  114.  
  115.       printf("\n  Model I:");
  116.  
  117.       printf("\n     z-> ((z^2 + q - 1)/(2z + q - 2))^2 \n");
  118.  
  119.       printf("\n  Model II:");
  120.  
  121.       printf("\n     z-> ((z^3 + 3az + ab)/(3z^2 + 3bz + q^2 - 3q + 3))^2");
  122.  
  123.       printf("\n     (where a = q-1 and b = q-2) \n\n");
  124.  
  125.       printf("\n  (Model I = 1 ; Model II = 2)\n\n>");
  126.  
  127.       scanf("%d",&Model);
  128.  
  129.       clrscr();
  130.  
  131.       if ((Model != 1) && (Model != 2)) Model = 1;
  132.  
  133.       if (Model == 1) {
  134.  
  135.        printf("\n             Model I: Select a formulation:   \n\n\n");
  136.  
  137.        printf("   1: q = -1.0       -6<=Re(z)<=4       -5<=Im(z)<=5 \n");
  138.  
  139.        printf("   2: q = -0.1       -6<=Re(z)<=4       -5<=Im(z)<=5 \n");
  140.  
  141.        printf("   3: q =  0.0       -6<=Re(z)<=4       -5<=Im(z)<=5 \n");
  142.  
  143.        printf("   4: q =  1.0       -5<=Re(z)<=5       -5<=Im(z)<=5 \n");
  144.  
  145.        printf("   5: q =  1.2       -5<=Re(z)<=5       -5<=Im(z)<=5 \n");
  146.  
  147.        printf("   6: q =  1.6       -5<=Re(z)<=5       -5<=Im(z)<=5 \n");
  148.  
  149.        printf("   7: q =  2.0       -5<=Re(z)<=5       -5<=Im(z)<=5 \n");
  150.  
  151.        printf("   8: q =  2.5       -5<=Re(z)<=5       -5<=Im(z)<=5 \n");
  152.  
  153.        printf("   9: q =  2.9       -5<=Re(z)<=5       -5<=Im(z)<=5 \n");
  154.  
  155.        printf("  10: q =  3.0       -5<=Re(z)<=5       -5<=Im(z)<=5 \n");
  156.  
  157.        printf("  11: q =  3.1       -5<=Re(z)<=5       -5<=Im(z)<=5 \n");
  158.  
  159.        printf("  12: q =  4.0       -4<=Re(z)<=6       -5<=Im(z)<=5 \n");
  160.  
  161.        printf("  13: q =  4.0     -5.5<=Re(z)<=7.9     -5<=Im(z)<=5\n");
  162.  
  163.        printf("  14: q = -0.1      0.8<=Re(z)<=2.0  2.635<=Im(z)<=3.535\n");
  164.  
  165.        printf("  15: q=~1.1+2.07i -0.5<=Re(z)<=1.5  -1.45<=Im(z)<=0.7\n");
  166.  
  167.        printf("  16: q=1.21+0.01i -2.1<=Re(z)<=-0.3  -0.5<=Im(z)<=0.5625");
  168.  
  169.        printf("\n\n>");
  170.  
  171.        scanf("%d",&index);
  172.  
  173.        clrscr();
  174.  
  175.       }
  176.  
  177.       else
  178.  
  179.       if (Model == 2) {
  180.  
  181.        printf("\n             Model II: Select a formulation:   \n\n\n");
  182.  
  183.        printf("   1: q = 2.0       -15<=Re(z)<=15     -11<=Im(z)<=11\n");
  184.  
  185.        printf("   2: q = 1.2+2i   -3.2<=Re(z)<=3.8   -3.3<=Im(z)<=1.9\n>");
  186.  
  187.        scanf("%d",&index);
  188.  
  189.       }
  190.  
  191.       if ((index>16) || (index<1)) index = 1;
  192.  
  193.       clrscr();
  194.  
  195.       printf("\n Number of iterations (maximum=16000, default=256)?");
  196.  
  197.       printf("\n (Suggested: 64 or higher) \n\n>");
  198.  
  199.       scanf("%d",&maxiter);
  200.  
  201.       printf("\n Set starting colour (>=0 , <=256)?");
  202.  
  203.       printf("\n (Suggested: 33, 30, 16, 40, 145)\n\n>");
  204.  
  205.       scanf("%d",&istart);
  206.  
  207.       if((maxiter>16000) || (maxiter)<=0) maxiter = 256;
  208.  
  209.       if((istart)<=0 || (istart>256)) istart = 33;
  210.  
  211.  
  212.  
  213.       installuserdriver("Svga256",DetectVGA256);
  214.  
  215.       initgraph(&graphdriver, &graphmode, "C:\\BORLANDC\\BGI");
  216.  
  217.  
  218.  
  219.       if (Vid == 0) { npix = 320; npiy = 200;}
  220.  
  221.       if (Vid == 1) { npix = 640; npiy = 400;}
  222.  
  223.       if (Vid == 2) { npix = 640; npiy = 480;}
  224.  
  225.       if (Vid == 3) { npix = 800; npiy = 600;}
  226.  
  227.       if (Vid == 4) { npix =1024; npiy = 768;}
  228.  
  229.       if ((Vid>4) || (Vid<0)) { npix = 640; npiy = 480;}
  230.  
  231. //
  232.  
  233. //    Defaults for the plotting window.
  234.  
  235. //
  236.  
  237.       xmin = -5.0; xmax = 5.0; ymin = -5.0; ymax = 5.0;
  238.  
  239. //
  240.  
  241.       if (Model == 1) {
  242.  
  243.     if (index == 1) {xmin=-6.0, xmax=4.0; q=complex(-1.0,0.0);}
  244.  
  245.     else
  246.  
  247.     if (index == 2) {xmin=-6.0, xmax=4.0; q=complex(-0.1,0.0);}
  248.  
  249.     else
  250.  
  251.     if (index == 3) {xmin=-6.0, xmax=4.0; q=complex(0.0,0.0);}
  252.  
  253.     else
  254.  
  255.     if (index == 4) q=complex(1.0,0.0);
  256.  
  257.     else
  258.  
  259.     if (index == 5) q=complex(1.2,0.0);
  260.  
  261.     else
  262.  
  263.     if (index == 6) q=complex(1.6,0.0);
  264.  
  265.     else
  266.  
  267.     if (index == 7) q=complex(2.0,0.0);
  268.  
  269.     else
  270.  
  271.     if (index == 8) q=complex(2.5,0.0);
  272.  
  273.     else
  274.  
  275.     if (index == 9) q=complex(2.9,0.0);
  276.  
  277.     else
  278.  
  279.     if (index ==10) q=complex(3.0,0.0);
  280.  
  281.     else
  282.  
  283.     if (index ==11) q=complex(3.1,0.0);
  284.  
  285.     else
  286.  
  287.     if (index ==12) {xmin=-4.0, xmax=6.0; q=complex(4.0,0.0);}
  288.  
  289.     else
  290.  
  291.     if (index ==13) {xmin=-5.5, xmax=7.9; q=complex(4.0,0.0);}
  292.  
  293.     else
  294.  
  295.     if (index ==14) {xmin=0.8, xmax=2.0; ymin=2.635; ymax=3.535;
  296.  
  297.              q=complex(-0.1,0.0);}
  298.  
  299.     else
  300.  
  301.     if (index ==15) {xmin=-0.5, xmax=1.5; ymin=-1.45; ymax=0.7;
  302.  
  303.              q=complex(1.09582,2.07142);}
  304.  
  305.     else
  306.  
  307.     if (index ==16) {xmin=-2.1, xmax=-0.3; ymin=-0.5; ymax=0.5625;
  308.  
  309.              q=complex(1.21,0.01);}
  310.  
  311.     else {q=complex(1.2,0.0);}
  312.  
  313.       }
  314.  
  315.       else if (Model == 2) {
  316.  
  317.     if (index ==1) {xmin=-15., xmax=15.; ymin=-11.0; ymax=11.0;
  318.  
  319.             q=complex(2.0,0.0);}
  320.  
  321.     else
  322.  
  323.     if (index ==2) {xmin=-3.2, xmax=3.8; ymin=-3.3; ymax=1.9;
  324.  
  325.             q=complex(1.20,2.0);}
  326.  
  327.     else {xmin=-3.2, xmax=3.8; ymin=-3.3; ymax=1.9;
  328.  
  329.           q=complex(1.20,2.0);}
  330.  
  331.       }
  332.  
  333.  
  334.  
  335.       if(fact>=1.0 || fact <=0.0)
  336.  
  337.     fact = 1.0;
  338.  
  339.       else {
  340.  
  341.     npix = (int)(npix*fact);
  342.  
  343.     npiy = (int)(npiy*fact);
  344.  
  345.       }
  346.  
  347.       ypy = (double)npiy - 0.5;
  348.  
  349.       deltap = (xmax-xmin)/(npix-1);
  350.  
  351.       deltaq = (ymax-ymin)/(npiy-1);
  352.  
  353.  
  354.  
  355.       if(ymin==-ymax)
  356.  
  357.      npy = npiy/2;
  358.  
  359.       else
  360.  
  361.      npy = npiy;
  362.  
  363.  
  364.  
  365.      cleardevice();
  366.  
  367.      for (np=0; np<=npix-1; np++) {
  368.  
  369.        x = xmin + (double)np*deltap;
  370.  
  371.        for (nq=0; nq<=npy-1; nq++) {
  372.  
  373.      y = ymin + (double)nq*deltaq;
  374.  
  375.      k  = 0;
  376.  
  377.      z = complex(x,y);
  378.  
  379.      do {
  380.  
  381. //
  382.  
  383. //         Definition for Model I magnets.
  384.  
  385. //
  386.  
  387.        if (Model == 1) z = (z*z + q - 1.0)/(2.0*z + q - 2.0);
  388.  
  389.        else
  390.  
  391. //
  392.  
  393. //         Definition for Model II magnets.
  394.  
  395. //
  396.  
  397.        if (Model == 2) {
  398.  
  399.          a = q - 1.0;
  400.  
  401.          b = q - 2.0;
  402.  
  403.          ab = a*b;
  404.  
  405.          z2 = z*z;
  406.  
  407.          z = (z*z2 + 3.0*a*z + ab)/(3.0*z2 + 3.0*b*z + ab + 1.0);
  408.  
  409.        }
  410.  
  411.        z = z*z;
  412.  
  413.        r = abs(z);
  414.  
  415.        k++;
  416.  
  417. //
  418.  
  419. //         Points which escape towards infinity.
  420.  
  421. //
  422.  
  423.        if (r >= maxiter) {
  424.  
  425.          ipen = istart + k;
  426.  
  427.          if (ymin == -ymax) {
  428.  
  429.            putpixel(np,nq,ipen);
  430.  
  431.            putpixel(np,npiy-nq-1,ipen);
  432.  
  433.          }
  434.  
  435.          else
  436.  
  437.            putpixel(np,nq,ipen);
  438.  
  439.        }
  440.  
  441. //
  442.  
  443. //         Converging points.
  444.  
  445. //
  446.  
  447.        if (k == maxiter) {
  448.  
  449.          ipen = 33;
  450.  
  451.          if (ymin == -ymax) {
  452.  
  453.            ypy = double(npiy) - nq - 0.5;
  454.  
  455.            putpixel(np,ypy,ipen);
  456.  
  457.            putpixel(np,nq,ipen);
  458.  
  459.          }
  460.  
  461.          else
  462.  
  463.            putpixel(np,nq,33);
  464.  
  465.        }
  466.  
  467.      } while (r <= maxiter && k<=maxiter);
  468.  
  469.        }
  470.  
  471.        if(kbhit()) break;
  472.  
  473.      }
  474.  
  475. //
  476.  
  477. //   Clean-up and end.
  478.  
  479. //
  480.  
  481.      getch();
  482.  
  483.      closegraph();
  484.  
  485. }
  486.  
  487.