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

  1. //
  2.  
  3. //+---------------------------------------------------------------+
  4.  
  5. //+ Program MAGNETS.CPP                                           +
  6.  
  7. //+ Plots 18 magnet-like fractals in the x-plane for different q. +
  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. //+ Modified by: Michael Sargent                                  +
  22.  
  23. //+ E-mail: msargent@moose.uvm.edu                                +
  24.  
  25. //+ Essex Junction, Vermont, USA, May 15, 1994                    +
  26.  
  27. //+                                                               +
  28.  
  29. //+ Needs SVGA256.H and SVGA256.BGI, supports up to four screens. +
  30.  
  31. //+                                                               +
  32.  
  33. //+ REFERENCE: Peitgen, H.-O. and Richter, P.H.                   +
  34.  
  35. //+            "The Beauty of Fractals" (Springer-Verlag 1986)    +
  36.  
  37. //+            pp. 129-37, 194.                                   +
  38.  
  39. //+---------------------------------------------------------------+
  40.  
  41. //
  42.  
  43. #include <stdio.h>
  44.  
  45. #include "svga256.h"
  46.  
  47. #include <conio.h>
  48.  
  49. #include <graphics.h>
  50.  
  51. #include <math.h>
  52.  
  53. #include <complex.h>
  54.  
  55. #include <dos.h>
  56.  
  57.  
  58.  
  59. void introscreen(void);
  60.  
  61. void userinput(void);
  62.  
  63. void parameters(void);
  64.  
  65. void calculate(void);
  66.  
  67. void preliminary(void);
  68.  
  69. void setVGApalette(char *buffer);
  70.  
  71. //void far initgraph(int far *,int far *,char far *);
  72.  
  73.  
  74.  
  75. // Global variables //
  76.  
  77. double xmin, xmax, ymin, ymax, fact=1.0, dist, xmag, ymag, zmag,
  78.  
  79.    ypy, x, y, r, deltap, deltaq;
  80.  
  81. int maxiter, Model, insidecolor=1, plotflag=0, npix, npiy, k, np, nq,
  82.  
  83.    npy, ipen, graphdriver=DETECT, graphmode, Vid, index;
  84.  
  85. complex c, z, q, a, b, ab, z2, secondz;
  86.  
  87.  
  88.  
  89. char palette[256][3]={
  90.  
  91. { 0, 0, 0},{ 0, 17, 45},{ 0, 18, 45},{ 0, 19, 44},
  92.  
  93. { 0, 20, 43},{ 0, 20, 42},{ 0, 21, 42},{ 0, 22, 41},
  94.  
  95. { 0, 22, 40},{ 0, 23, 40},{ 0, 24, 39},{ 0, 25, 38},
  96.  
  97. { 0, 25, 37},{ 0, 26, 37},{ 0, 27, 36},{ 0, 28, 35},
  98.  
  99. { 0, 28, 34},{ 0, 29, 34},{ 0, 30, 33},{ 0, 31, 32},
  100.  
  101. { 0, 31, 31},{ 0, 32, 31},{ 0, 33, 30},{ 0, 34, 29},
  102.  
  103. { 0, 34, 28},{ 0, 35, 28},{ 0, 36, 27},{ 0, 37, 26},
  104.  
  105. { 0, 37, 25},{ 0, 38, 25},{ 0, 39, 24},{ 0, 40, 23},
  106.  
  107. { 0, 40, 22},{ 0, 41, 22},{ 0, 42, 21},{ 0, 42, 20},
  108.  
  109. { 0, 43, 20},{ 0, 44, 19},{ 0, 45, 18},{ 0, 45, 17},
  110.  
  111. { 0, 46, 17},{ 0, 47, 16},{ 0, 48, 15},{ 0, 48, 14},
  112.  
  113. { 0, 49, 14},{ 0, 50, 13},{ 0, 51, 12},{ 0, 51, 11},
  114.  
  115. { 0, 52, 11},{ 0, 53, 10},{ 0, 54, 9},{ 0, 54, 8},
  116.  
  117. { 0, 55, 8},{ 0, 56, 7},{ 0, 57, 6},{ 0, 57, 5},
  118.  
  119. { 0, 58, 5},{ 0, 59, 4},{ 0, 60, 3},{ 0, 60, 2},
  120.  
  121. { 0, 61, 2},{ 0, 62, 1},{ 0, 63, 0},{ 0, 63, 0},
  122.  
  123. { 1, 62, 0},{ 2, 61, 0},{ 2, 60, 0},{ 3, 60, 0},
  124.  
  125. { 4, 59, 0},{ 5, 58, 0},{ 5, 57, 0},{ 6, 57, 0},
  126.  
  127. { 7, 56, 0},{ 8, 55, 0},{ 8, 54, 0},{ 9, 54, 0},
  128.  
  129. { 10, 53, 0},{ 11, 52, 0},{ 11, 51, 0},{ 12, 51, 0},
  130.  
  131. { 13, 50, 0},{ 14, 49, 0},{ 14, 48, 0},{ 15, 48, 0},
  132.  
  133. { 16, 47, 0},{ 17, 46, 0},{ 17, 45, 0},{ 18, 45, 0},
  134.  
  135. { 19, 44, 0},{ 20, 43, 0},{ 20, 42, 0},{ 21, 42, 0},
  136.  
  137. { 22, 41, 0},{ 22, 40, 0},{ 23, 40, 0},{ 24, 39, 0},
  138.  
  139. { 25, 38, 0},{ 25, 37, 0},{ 26, 37, 0},{ 27, 36, 0},
  140.  
  141. { 28, 35, 0},{ 28, 34, 0},{ 29, 34, 0},{ 30, 33, 0},
  142.  
  143. { 31, 32, 0},{ 31, 31, 0},{ 32, 31, 0},{ 33, 30, 0},
  144.  
  145. { 34, 29, 0},{ 34, 28, 0},{ 35, 28, 0},{ 36, 27, 0},
  146.  
  147. { 37, 26, 0},{ 37, 25, 0},{ 38, 25, 0},{ 39, 24, 0},
  148.  
  149. { 40, 23, 0},{ 40, 22, 0},{ 41, 22, 0},{ 42, 21, 0},
  150.  
  151. { 42, 20, 0},{ 43, 20, 0},{ 44, 19, 0},{ 45, 18, 0},
  152.  
  153. { 45, 17, 0},{ 46, 17, 0},{ 47, 16, 0},{ 48, 15, 0},
  154.  
  155. { 48, 14, 0},{ 49, 14, 0},{ 50, 13, 0},{ 51, 12, 0},
  156.  
  157. { 51, 11, 0},{ 52, 11, 0},{ 53, 10, 0},{ 54, 9, 0},
  158.  
  159. { 54, 8, 0},{ 55, 8, 0},{ 56, 7, 0},{ 57, 6, 0},
  160.  
  161. { 57, 5, 0},{ 58, 5, 0},{ 59, 4, 0},{ 60, 3, 0},
  162.  
  163. { 60, 2, 0},{ 61, 2, 0},{ 62, 1, 0},{ 63, 0, 0},
  164.  
  165. { 63, 0, 0},{ 62, 0, 1},{ 61, 0, 2},{ 60, 0, 2},
  166.  
  167. { 60, 0, 3},{ 59, 0, 4},{ 58, 0, 5},{ 57, 0, 5},
  168.  
  169. { 57, 0, 6},{ 56, 0, 7},{ 55, 0, 8},{ 54, 0, 8},
  170.  
  171. { 54, 0, 9},{ 53, 0, 10},{ 52, 0, 11},{ 51, 0, 11},
  172.  
  173. { 51, 0, 12},{ 50, 0, 13},{ 49, 0, 14},{ 48, 0, 14},
  174.  
  175. { 48, 0, 15},{ 47, 0, 16},{ 46, 0, 17},{ 45, 0, 17},
  176.  
  177. { 45, 0, 18},{ 44, 0, 19},{ 43, 0, 20},{ 42, 0, 20},
  178.  
  179. { 42, 0, 21},{ 41, 0, 22},{ 40, 0, 22},{ 40, 0, 23},
  180.  
  181. { 39, 0, 24},{ 38, 0, 25},{ 37, 0, 25},{ 37, 0, 26},
  182.  
  183. { 36, 0, 27},{ 35, 0, 28},{ 34, 0, 28},{ 34, 0, 29},
  184.  
  185. { 33, 0, 30},{ 32, 0, 31},{ 31, 0, 31},{ 31, 0, 32},
  186.  
  187. { 30, 0, 33},{ 29, 0, 34},{ 28, 0, 34},{ 28, 0, 35},
  188.  
  189. { 27, 0, 36},{ 26, 0, 37},{ 25, 0, 37},{ 25, 0, 38},
  190.  
  191. { 24, 0, 39},{ 23, 0, 40},{ 22, 0, 40},{ 22, 0, 41},
  192.  
  193. { 21, 0, 42},{ 20, 0, 42},{ 20, 0, 43},{ 19, 0, 44},
  194.  
  195. { 18, 0, 45},{ 17, 0, 45},{ 17, 0, 46},{ 16, 0, 47},
  196.  
  197. { 15, 0, 48},{ 14, 0, 48},{ 14, 0, 49},{ 13, 0, 50},
  198.  
  199. { 12, 0, 51},{ 11, 0, 51},{ 11, 0, 52},{ 10, 0, 53},
  200.  
  201. { 9, 0, 54},{ 8, 0, 54},{ 8, 0, 55},{ 7, 0, 56},
  202.  
  203. { 6, 0, 57},{ 5, 0, 57},{ 5, 0, 58},{ 4, 0, 59},
  204.  
  205. { 3, 0, 60},{ 2, 0, 60},{ 2, 0, 61},{ 1, 0, 62},
  206.  
  207. { 0, 0, 63},{ 0, 0, 63},{ 0, 1, 62},{ 0, 2, 61},
  208.  
  209. { 0, 2, 60},{ 0, 3, 60},{ 0, 4, 59},{ 0, 5, 58},
  210.  
  211. { 0, 5, 57},{ 0, 6, 57},{ 0, 7, 56},{ 0, 8, 55},
  212.  
  213. { 0, 8, 54},{ 0, 9, 54},{ 0, 10, 53},{ 0, 11, 52},
  214.  
  215. { 0, 11, 51},{ 0, 12, 51},{ 0, 13, 50},{ 0, 14, 49},
  216.  
  217. { 0, 14, 48},{ 0, 15, 48},{ 0, 16, 47},{ 0, 17, 46}
  218.  
  219. };
  220.  
  221.  
  222.  
  223. //===========================================================================
  224.  
  225. // Convoluted BGI stuff
  226.  
  227. //===========================================================================
  228.  
  229. int huge DetectVGA256()
  230.  
  231. {
  232.  
  233.    int mistake;
  234.  
  235.    char key;
  236.  
  237.  
  238.  
  239.    printf("\n\n                      Which video mode would you like to use? \n\n");
  240.  
  241.    printf("                      a: 640x400x256\n");
  242.  
  243.    printf("                      b: 640x480x256\n");
  244.  
  245.    printf("                      c: 800x600x256\n");
  246.  
  247.    printf("                      d: 1024x768x256\n\n");
  248.  
  249.    printf("                      > ");
  250.  
  251.    do
  252.  
  253.    {
  254.  
  255.       mistake = 0;
  256.  
  257.       key = getch();
  258.  
  259.       Vid = ("%d",key) - 96;
  260.  
  261.       if (Vid < 1 || Vid > 4)
  262.  
  263.       {
  264.  
  265.      mistake = 1;
  266.  
  267.      printf("%c%c%c",7,7,7);
  268.  
  269.      gotoxy(25,10);
  270.  
  271.      printf("                                                          ");
  272.  
  273.      gotoxy(25,10);
  274.  
  275.       }
  276.  
  277.     }
  278.  
  279.     while(mistake == 1);
  280.  
  281.     printf("%c",key);
  282.  
  283.     delay(500);
  284.  
  285.     return Vid;
  286.  
  287. }
  288.  
  289.  
  290.  
  291. //===========================================================================
  292.  
  293. // Main
  294.  
  295. //===========================================================================
  296.  
  297. void main()
  298.  
  299. {
  300.  
  301.      introscreen();
  302.  
  303.      userinput();
  304.  
  305.      parameters();
  306.  
  307.      cleardevice();
  308.  
  309.      setVGApalette(palette[0]);
  310.  
  311.      calculate();
  312.  
  313.      closegraph();
  314.  
  315. }
  316.  
  317.  
  318.  
  319. //===========================================================================
  320.  
  321. // Introduction & BOF credits
  322.  
  323. //===========================================================================
  324.  
  325. void introscreen(void)
  326.  
  327. {
  328.  
  329.    clrscr();
  330.  
  331.    _setcursortype(_NOCURSOR);
  332.  
  333.    gotoxy(31,4);
  334.  
  335.    printf("MAGNET-LIKE MODELS");
  336.  
  337.    gotoxy(15,6);
  338.  
  339.    printf("Reference: Peitgen, H.-O., and Richter, P.H.");
  340.  
  341.    gotoxy(15,7);
  342.  
  343.    printf("The Beauty of Fractals (Springer-Verlag 1986) pp. 129-37, 194");
  344.  
  345.    gotoxy(15,9);
  346.  
  347.    printf("By Fausto A. A. Barbuto, April 4, 1994");
  348.  
  349.    gotoxy(15,10);
  350.  
  351.    printf("E-mail: BJ06@C53000.PETROBRAS.ANRJ.BR");
  352.  
  353.    gotoxy(15,12);
  354.  
  355.    printf("Modifications by Michael Sargent, May 15, 1994");
  356.  
  357.    gotoxy(15,13);
  358.  
  359.    printf("E-mail: msargent@moose.uvm.edu\n\n\n");
  360.  
  361.    getch();
  362.  
  363. }
  364.  
  365.  
  366.  
  367. //===========================================================================
  368.  
  369. // Obtain input - who said user-proofing was fun?
  370.  
  371. //===========================================================================
  372.  
  373. void userinput(void)
  374.  
  375. {
  376.  
  377.    int i, mistake, status;
  378.  
  379.    char key;
  380.  
  381.  
  382.  
  383.    clrscr();
  384.  
  385.    _setcursortype(_NORMALCURSOR);
  386.  
  387.    printf("\n\n                              SELECT A MODEL TYPE: \n\n\n");
  388.  
  389.    printf("                              %c (z%c + q - 1) %c%c\n",218,253,191,253);
  390.  
  391.    cprintf("     a: Model I:  z  %c",26);
  392.  
  393.    gotoxy(31,7);
  394.  
  395.    printf("%c ",179);
  396.  
  397.    for (i=0;i<12;i++) putchar(196);
  398.  
  399.    printf(" %c\n",179);
  400.  
  401.    printf("                              %c (2z + q - 2) %c\n\n\n",192,217);
  402.  
  403.    printf("                        %c     (z^3 + 3az + ab)      %c%c\n",218,191,253);
  404.  
  405.    cprintf("     b: Model II: z  %c",26);
  406.  
  407.    gotoxy(25,12);
  408.  
  409.    printf("%c ",179);
  410.  
  411.    for (i=0;i<25;i++) putchar(196);
  412.  
  413.    printf(" %c\n",179);
  414.  
  415.    printf("                        %c (3z%c + 3bz + q%c - 3q + 3) %c\n\n",192,253,253,217);
  416.  
  417.    printf("                         (where a = q-1 and b = q-2) \n\n\n     > ");
  418.  
  419.    do
  420.  
  421.    {
  422.  
  423.       mistake = 0;
  424.  
  425.       key = getch();
  426.  
  427.       Model = ("%d",key) - 96;
  428.  
  429.       if (Model < 1 || Model > 2)
  430.  
  431.       {
  432.  
  433.      mistake = 1;
  434.  
  435.      printf("%c%c%c",7,7,7);
  436.  
  437.      gotoxy(8,18);
  438.  
  439.      printf("                                                          ");
  440.  
  441.      gotoxy(8,18);
  442.  
  443.       }
  444.  
  445.    }
  446.  
  447.    while(mistake == 1);
  448.  
  449.    printf("%c",key);
  450.  
  451.    delay(500);
  452.  
  453.  
  454.  
  455.    clrscr();
  456.  
  457.    if (Model == 1)
  458.  
  459.    {
  460.  
  461.     printf("\n                      Model I: Select a formulation:   \n\n\n");
  462.  
  463.     printf("   a: q = -1.0       -6<=Re(z)<=4       -5<=Im(z)<=5       [Fig. 53a]\n");
  464.  
  465.     printf("   b: q = -0.1       -6<=Re(z)<=4       -5<=Im(z)<=5       [Fig. 53b]\n");
  466.  
  467.     printf("   c: q =  0.0       -6<=Re(z)<=4       -5<=Im(z)<=5       [Fig. 53c]\n");
  468.  
  469.     printf("   d: q =  1.0       -5<=Re(z)<=5       -5<=Im(z)<=5       [Fig. 53d]\n");
  470.  
  471.     printf("   e: q =  1.2       -5<=Re(z)<=5       -5<=Im(z)<=5       [Fig. 53e]\n");
  472.  
  473.     printf("   f: q =  1.6       -5<=Re(z)<=5       -5<=Im(z)<=5       [Fig. 53f]\n");
  474.  
  475.     printf("   g: q =  2.0       -5<=Re(z)<=5       -5<=Im(z)<=5       [Fig. 53g]\n");
  476.  
  477.     printf("   h: q =  2.5       -5<=Re(z)<=5       -5<=Im(z)<=5       [Fig. 53h]\n");
  478.  
  479.     printf("   i: q =  2.9       -5<=Re(z)<=5       -5<=Im(z)<=5       [Fig. 53i]\n");
  480.  
  481.     printf("   j: q =  3.0       -5<=Re(z)<=5       -5<=Im(z)<=5       [Fig. 53j]\n");
  482.  
  483.     printf("   k: q =  3.1       -5<=Re(z)<=5       -5<=Im(z)<=5       [Fig. 53k]\n");
  484.  
  485.     printf("   l: q =  4.0       -4<=Re(z)<=6       -5<=Im(z)<=5       [Fig. 53l]\n");
  486.  
  487.     printf("   m: q =  4.0     -5.5<=Re(z)<=7.9     -5<=Im(z)<=5       [Map 3]\n");
  488.  
  489.     printf("   n: q = -0.1      0.8<=Re(z)<=2.0  2.635<=Im(z)<=3.535   [Map 4]\n");
  490.  
  491.     printf("   o: q=~1.1+2.07i -0.5<=Re(z)<=1.5  -1.45<=Im(z)<=0.7     [Map 5]\n");
  492.  
  493.     printf("   p: q=1.21+0.01i -2.1<=Re(z)<=-0.3  -0.5<=Im(z)<=0.5625  [Map 6]");
  494.  
  495.     printf("\n\n   > ");
  496.  
  497.     do
  498.  
  499.     {
  500.  
  501.       mistake = 0;
  502.  
  503.       key = getch();
  504.  
  505.       index = ("%d",key) - 96;
  506.  
  507.       if (index < 1 || index > 16)
  508.  
  509.       {
  510.  
  511.      mistake = 1;
  512.  
  513.      printf("%c%c%c",7,7,7);
  514.  
  515.      gotoxy(6,22);
  516.  
  517.      printf("                                                          ");
  518.  
  519.      gotoxy(6,22);
  520.  
  521.       }
  522.  
  523.     }
  524.  
  525.     while(mistake == 1);
  526.  
  527.     printf("%c",key);
  528.  
  529.     delay(500);
  530.  
  531.    }
  532.  
  533.    else if (Model == 2)
  534.  
  535.    {
  536.  
  537.     printf("\n\n                      Model II: Select a formulation:   \n\n\n");
  538.  
  539.     printf("   a: q = 2.0       -15<=Re(z)<=15     -11<=Im(z)<=11      [Map 7]\n");
  540.  
  541.     printf("   b: q = 1.2+2i   -3.2<=Re(z)<=3.8   -3.3<=Im(z)<=1.9     [Map 8-10]\n\n   > ");
  542.  
  543.     do
  544.  
  545.     {
  546.  
  547.       mistake = 0;
  548.  
  549.       key = getch();
  550.  
  551.       index = ("%d",key) - 96;
  552.  
  553.       if (index < 1 || index > 2)
  554.  
  555.       {
  556.  
  557.      mistake = 1;
  558.  
  559.      printf("%c%c%c",7,7,7);
  560.  
  561.      gotoxy(6,9);
  562.  
  563.      printf("                                                          ");
  564.  
  565.      gotoxy(6,9);
  566.  
  567.       }
  568.  
  569.     }
  570.  
  571.     while(mistake == 1);
  572.  
  573.     printf("%c",key);
  574.  
  575.     delay(500);
  576.  
  577.    }
  578.  
  579.  
  580.  
  581.    clrscr();
  582.  
  583.    printf("\n\n                      Enter number of iterations:\n\n");
  584.  
  585.    printf("                      a: 16\n");
  586.  
  587.    printf("                      b: 64\n");
  588.  
  589.    printf("                      c: 128\n");
  590.  
  591.    printf("                      d: 256\n");
  592.  
  593.    printf("                      e: 512\n\n");
  594.  
  595.    printf("                      > ");
  596.  
  597.    do
  598.  
  599.    {
  600.  
  601.       mistake = 0;
  602.  
  603.       key = getch();
  604.  
  605.       maxiter = ("%d",key) - 96;
  606.  
  607.       if (maxiter < 1 || maxiter > 5)
  608.  
  609.       {
  610.  
  611.      mistake = 1;
  612.  
  613.      printf("%c%c%c",7,7,7);
  614.  
  615.      gotoxy(25,11);
  616.  
  617.      printf("                                                          ");
  618.  
  619.      gotoxy(25,11);
  620.  
  621.       }
  622.  
  623.    }
  624.  
  625.    while(mistake == 1);
  626.  
  627.    printf("%c",key);
  628.  
  629.    delay(500);
  630.  
  631.    if (maxiter == 1) maxiter = 16;
  632.  
  633.    if (maxiter == 2) maxiter = 64;
  634.  
  635.    if (maxiter == 3) maxiter = 128;
  636.  
  637.    if (maxiter == 4) maxiter = 256;
  638.  
  639.    if (maxiter == 5) maxiter = 512;
  640.  
  641.  
  642.  
  643.    if (Model == 1)
  644.  
  645.    {
  646.  
  647.       printf("\n\n                      Select an inside-coloring scheme:\n");
  648.  
  649.       printf("\n                      a: Standard target set method for finite attractor");
  650.  
  651.       printf("\n                      b: Alternate subtle gradient scheme\n\n");
  652.  
  653.       printf("                      > ");
  654.  
  655.    do
  656.  
  657.    {
  658.  
  659.       mistake = 0;
  660.  
  661.       key = getch();
  662.  
  663.       insidecolor = ("%d",key) - 96;
  664.  
  665.       if (insidecolor < 1 || insidecolor > 2)
  666.  
  667.       {
  668.  
  669.      mistake = 1;
  670.  
  671.      printf("%c%c%c",7,7,7);
  672.  
  673.      gotoxy(25,18);
  674.  
  675.      printf("                                                          ");
  676.  
  677.      gotoxy(25,18);
  678.  
  679.       }
  680.  
  681.    }
  682.  
  683.    while(mistake == 1);
  684.  
  685.    printf("%c",key);
  686.  
  687.    delay(500);
  688.  
  689.    }
  690.  
  691.  
  692.  
  693.    clrscr();
  694.  
  695.    installuserdriver("Svga256",DetectVGA256);
  696.  
  697. //
  698.  
  699. // Change the directory below to fit your own .BGI requirements.
  700.  
  701. //
  702.  
  703.    initgraph(&graphdriver, &graphmode, "C:\\BORLANDC\\BGI");
  704.  
  705. }
  706.  
  707.  
  708.  
  709. //===========================================================================
  710.  
  711. // Initialize parameters
  712.  
  713. //===========================================================================
  714.  
  715. void parameters(void)
  716.  
  717. {
  718.  
  719.       if (Vid == 1) { npix = 640; npiy = 400;}
  720.  
  721.       if (Vid == 2) { npix = 640; npiy = 480;}
  722.  
  723.       if (Vid == 3) { npix = 800; npiy = 600;}
  724.  
  725.       if (Vid == 4) { npix =1024; npiy = 768;}
  726.  
  727. //
  728.  
  729. //    Defaults for the plotting window.
  730.  
  731. //
  732.  
  733.       xmin = -5.0; xmax = 5.0; ymin = -5.0; ymax = 5.0;
  734.  
  735.  
  736.  
  737.       if (Model == 1) {
  738.  
  739.     if (index == 1) {xmin=-6.0, xmax=4.0; q=complex(-1.0,0.0);}
  740.  
  741.     else
  742.  
  743.     if (index == 2) {xmin=-6.0, xmax=4.0; q=complex(-0.1,0.0);}
  744.  
  745.     else
  746.  
  747.     if (index == 3) {xmin=-6.0, xmax=4.0; q=complex(0.0,0.0);}
  748.  
  749.     else
  750.  
  751.     if (index == 4) q=complex(1.0,0.0);
  752.  
  753.     else
  754.  
  755.     if (index == 5) q=complex(1.2,0.0);
  756.  
  757.     else
  758.  
  759.     if (index == 6) q=complex(1.6,0.0);
  760.  
  761.     else
  762.  
  763.     if (index == 7) q=complex(2.0,0.0);
  764.  
  765.     else
  766.  
  767.     if (index == 8) q=complex(2.5,0.0);
  768.  
  769.     else
  770.  
  771.     if (index == 9) q=complex(2.9,0.0);
  772.  
  773.     else
  774.  
  775.     if (index ==10) q=complex(3.0,0.0);
  776.  
  777.     else
  778.  
  779.     if (index ==11) q=complex(3.1,0.0);
  780.  
  781.     else
  782.  
  783.     if (index ==12) {xmin=-4.0, xmax=6.0; q=complex(4.0,0.0);}
  784.  
  785.     else
  786.  
  787.     if (index ==13) {xmin=-5.5, xmax=7.9; q=complex(4.0,0.0);}
  788.  
  789.     else
  790.  
  791.     if (index ==14) {xmin=0.8, xmax=2.0; ymin=2.635; ymax=3.535;
  792.  
  793.              q=complex(-0.1,0.0);}
  794.  
  795.     else
  796.  
  797.     if (index ==15) {xmin=-0.5, xmax=1.5; ymin=-1.45; ymax=0.7;
  798.  
  799.              q=complex(1.09582,2.07142);}
  800.  
  801.     else
  802.  
  803.     if (index ==16) {xmin=-2.1, xmax=-0.3; ymin=-0.5; ymax=0.5625;
  804.  
  805.              q=complex(1.21,0.01);}
  806.  
  807.     else {q=complex(1.2,0.0);}
  808.  
  809.       }
  810.  
  811.       else if (Model == 2) {
  812.  
  813.     if (index ==1) {xmin=-15.0, xmax=15.0; ymin=-11.0; ymax=11.0;
  814.  
  815.             q=complex(2.0,0.0);}
  816.  
  817.     else
  818.  
  819.     if (index ==2) {xmin=-3.2, xmax=3.8; ymin=-3.3; ymax=1.9;
  820.  
  821.             q=complex(1.20,2.0);}
  822.  
  823.     else {xmin=-3.2, xmax=3.8; ymin=-3.3; ymax=1.9;
  824.  
  825.           q=complex(1.20,2.0);}
  826.  
  827.       }
  828.  
  829.  
  830.  
  831.       if(fact>=1.0 || fact <=0.0)
  832.  
  833.     fact = 1.0;
  834.  
  835.       else {
  836.  
  837.     npix = (int)(npix*fact);
  838.  
  839.     npiy = (int)(npiy*fact);
  840.  
  841.       }
  842.  
  843.       ypy = (double)npiy - 0.5;
  844.  
  845.       deltap = (xmax-xmin)/(npix-1);
  846.  
  847.       deltaq = (ymax-ymin)/(npiy-1);
  848.  
  849.  
  850.  
  851.       if(ymin==-ymax)
  852.  
  853.      npy = npiy/2;
  854.  
  855.       else
  856.  
  857.      npy = npiy;
  858.  
  859. }
  860.  
  861.  
  862.  
  863. //===========================================================================
  864.  
  865. // Do the calculations
  866.  
  867. //===========================================================================
  868.  
  869. void calculate(void)
  870.  
  871. {
  872.  
  873.      for (np=0; np<=npix-1; np++)
  874.  
  875.      {
  876.  
  877.        x = xmin + (double)np*deltap;
  878.  
  879.        for (nq=0; nq<=npy-1; nq++)
  880.  
  881.        {
  882.  
  883.      y = ymin + (double)nq*deltaq;
  884.  
  885.      k  = 0;
  886.  
  887.      z = complex(x,y);
  888.  
  889. // - - - Standard target set coloring scheme - - - - - - - - - - - - - - -
  890.  
  891.      if (insidecolor==1)
  892.  
  893.      {
  894.  
  895.        do
  896.  
  897.        {
  898.  
  899.          preliminary();
  900.  
  901. //
  902.  
  903. //           Points which escape towards infinity.
  904.  
  905. //
  906.  
  907.          if (r >= maxiter)
  908.  
  909.          {
  910.  
  911.            ipen = ((5 * k)%255) + 1;
  912.  
  913.            if (ymin == -ymax)
  914.  
  915.            {
  916.  
  917.          putpixel(np,nq,ipen);
  918.  
  919.          putpixel(np,npiy-nq-1,ipen);
  920.  
  921.            }
  922.  
  923.            else putpixel(np,nq,ipen);
  924.  
  925.          }
  926.  
  927. //
  928.  
  929. //           Points converging to (1,0).
  930.  
  931. //
  932.  
  933.          else if (zmag<0.00001)
  934.  
  935.          {
  936.  
  937.         ipen = ((k << 2)%100) + 156;
  938.  
  939.         plotflag = 1;
  940.  
  941.          }
  942.  
  943. //
  944.  
  945. //           Points converging to (0,0) (for Model 2).
  946.  
  947. //
  948.  
  949.          else if (r<0.00001)
  950.  
  951.          {
  952.  
  953.            ipen = ((k << 2)%50)+206;
  954.  
  955.            plotflag = 1;
  956.  
  957.          }
  958.  
  959. //
  960.  
  961. //           Any leftover points.
  962.  
  963. //
  964.  
  965.          else if (k == maxiter)
  966.  
  967.          {
  968.  
  969.         ipen = ((k << 2)%100) + 156;
  970.  
  971.         plotflag = 1;
  972.  
  973.          }
  974.  
  975.  
  976.  
  977.          if (plotflag)
  978.  
  979.          {
  980.  
  981.            if (ymin == -ymax)
  982.  
  983.            {
  984.  
  985.          ypy = double(npiy) - nq - 0.5;
  986.  
  987.          putpixel(np,ypy,ipen);
  988.  
  989.          putpixel(np,nq,ipen);
  990.  
  991.            }
  992.  
  993.            else putpixel(np,nq,ipen);
  994.  
  995.            plotflag = 0;
  996.  
  997.          }
  998.  
  999.        } while (r<=maxiter && k<maxiter && zmag>=0.00001 && r>=0.00001);
  1000.  
  1001.      }
  1002.  
  1003. // - - - Alternative inside-coloring scheme - - - - - - - - - - - - - - - -
  1004.  
  1005.      if (insidecolor==2)
  1006.  
  1007.      {
  1008.  
  1009.        do
  1010.  
  1011.        {
  1012.  
  1013.          preliminary();
  1014.  
  1015.          if (k == 2) secondz = z;
  1016.  
  1017. //
  1018.  
  1019. //           Points which escape towards infinity.
  1020.  
  1021. //
  1022.  
  1023.          if (r >= maxiter)
  1024.  
  1025.          {
  1026.  
  1027.            ipen = ((5 * k)%255) + 1;
  1028.  
  1029.            if (ymin == -ymax)
  1030.  
  1031.            {
  1032.  
  1033.          putpixel(np,nq,ipen);
  1034.  
  1035.          putpixel(np,npiy-nq-1,ipen);
  1036.  
  1037.            }
  1038.  
  1039.            else
  1040.  
  1041.          putpixel(np,nq,ipen);
  1042.  
  1043.          }
  1044.  
  1045. //
  1046.  
  1047. //           Converging points.
  1048.  
  1049. //
  1050.  
  1051.          if (k == maxiter)
  1052.  
  1053.          {
  1054.  
  1055.            xmag = real(secondz) - 1;
  1056.  
  1057.            ymag = imag(secondz);
  1058.  
  1059.            zmag = sqrt(xmag * xmag + ymag * ymag);
  1060.  
  1061.            ipen = ((int)(16 * zmag)%86)+170;
  1062.  
  1063.            if (ymin == -ymax)
  1064.  
  1065.            {
  1066.  
  1067.          ypy = double(npiy) - nq - 0.5;
  1068.  
  1069.          putpixel(np,ypy,ipen);
  1070.  
  1071.          putpixel(np,nq,ipen);
  1072.  
  1073.            }
  1074.  
  1075.            else
  1076.  
  1077.          putpixel(np,nq,ipen);
  1078.  
  1079.          }
  1080.  
  1081.        } while (r <= maxiter && k<=maxiter);
  1082.  
  1083.      }
  1084.  
  1085. // - - - End of inside coloring routines - - - - - - - - - - - - - - - - -
  1086.  
  1087.        }
  1088.  
  1089.        if(kbhit()) break;
  1090.  
  1091.      }
  1092.  
  1093.      getch();
  1094.  
  1095. }
  1096.  
  1097.  
  1098.  
  1099. //==========================================================================
  1100.  
  1101. // Preliminary calculations before plotting colors
  1102.  
  1103. //==========================================================================
  1104.  
  1105. void preliminary(void)
  1106.  
  1107. {
  1108.  
  1109. //           Definition for Model I magnets.
  1110.  
  1111. //
  1112.  
  1113.          if (Model == 1) z = (z*z + q - 1.0)/(2.0*z + q - 2.0);
  1114.  
  1115. //
  1116.  
  1117. //           Definition for Model II magnets.
  1118.  
  1119. //
  1120.  
  1121.          else if (Model == 2)
  1122.  
  1123.          {
  1124.  
  1125.            a = q - 1.0;
  1126.  
  1127.            b = q - 2.0;
  1128.  
  1129.            ab = a*b;
  1130.  
  1131.            z2 = z*z;
  1132.  
  1133.            z = (z*z2 + 3.0*a*z + ab)/(3.0*z2 + 3.0*b*z + ab + 1.0);
  1134.  
  1135.          }
  1136.  
  1137.          z = z*z;
  1138.  
  1139.          r = abs(z);
  1140.  
  1141.          xmag = real(z) - 1;
  1142.  
  1143.          ymag = imag(z);
  1144.  
  1145.          zmag = sqrt(xmag * xmag + ymag * ymag);
  1146.  
  1147.          k++;
  1148.  
  1149. }
  1150.  
  1151.  
  1152.  
  1153. //==========================================================================
  1154.  
  1155. // Function to set all 256 color registers
  1156.  
  1157. //==========================================================================
  1158.  
  1159. #pragma warn -eff
  1160.  
  1161. void setVGApalette(char *buffer)
  1162.  
  1163. {
  1164.  
  1165.    int i, j;
  1166.  
  1167.  
  1168.  
  1169.    for (i=0;i<256;i++)
  1170.  
  1171.    {
  1172.  
  1173.       outp(0x3c8, i);
  1174.  
  1175.       for (j=0;j<3;j++)
  1176.  
  1177.      outp(0x3c9, *buffer++);
  1178.  
  1179.    }
  1180.  
  1181. }
  1182.  
  1183. #pragma warn +eff
  1184.  
  1185.