home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / graf / plotfrac.zip / PLOTFRAC.C < prev    next >
Text File  |  1987-10-26  |  34KB  |  1,003 lines

  1. /*
  2.  
  3.     P L O T F R A C . C
  4.  
  5.     VERSION   :   3.0
  6.  
  7. */
  8.  
  9.  
  10. #include <vcstdio.h>
  11. #include <conio.h>
  12. #include <ctype.h>
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include <dos.h>
  16. #include <process.h>
  17. #include <string.h>
  18. #include <sys\types.h>
  19. #include <sys\stat.h>
  20. #include <float.h>
  21. #include "grafix.h"       /* My own Int. 10h driven graphics routines */
  22.  
  23. extern void set_plot(int);
  24. extern void plot_fractal (char *, int);
  25. extern void restore_picture (void);
  26. extern long factorial (int);
  27. extern int build_fctn (int);
  28. extern double calc_nx (double, double);
  29. extern double calc_ny (double, double);
  30. extern double pow_r (double, int);
  31.  
  32. struct stat pic_buf;
  33.  
  34. int result = 0, graf_mode = 4, mycolor = 0, n_x = 0, n_y = 0, hr_mode = 6,
  35.     M = 100, K = 200, v_lin_count = 0, v_lin_max = 319, h_lin_count = 0,
  36.     max_row = 199, degree = 0, wno, wno1, wno2, wno3, errwno;
  37.  
  38. int real_exp[51], imag_exp[51];
  39. long konst[51];
  40. double x[201], y[201];
  41. double x_max = 0, y_max = 0, x_min = 0, y_min = 0, p = 0, q = 0, p_max = 0,
  42.       p_min = 0, a = 0, b = 0, c = 0, delt_y= 0, delt_x= 0, delt_p = 0,
  43.       delt_q = 0, r = 0, q_min = 0, q_max = 0;
  44.  
  45. main()
  46. {
  47.     register int ii;
  48.     int alldone, i, j, iresult;
  49.     double d_i;
  50.     char *result, line[80], title[80];
  51.  
  52.     for (ii=0;ii<51;ii++) {
  53.         real_exp[ii] = 0;
  54.         imag_exp[ii] = 0;
  55.         konst[ii] = 0;
  56.         }
  57.     for (ii=0;ii<201;ii++) {
  58.         x[ii] = 0;
  59.         y[ii] = 0;
  60.         }
  61.     vcstart(CLRSCRN);
  62.     empty(title, 80);
  63.     sprintf(title,"PLOTFRAC  V4.0  25-Oct-87   by   Ron Merts");
  64.     if ((wno = wxopen(0,0,24,79,title,
  65.      BORDER+BD1+CURSOR+ACTIVE+CENTER,24,80)) == -1)
  66.         terror("main():  Error opening main window (WNO).\n");
  67.     wselect(wno);
  68.     h_lin_count = 0;
  69.     alldone = 0;
  70.     erase();
  71.     wselect(wno);
  72.     woff();
  73.     atsay(0,26,"Welcome to PLOTFRAC V4.0");
  74.     atsay(1,24,"Written By Ronald F. Merts");
  75.     won();
  76.     while (!alldone) {
  77.         degree = 0;
  78.         if ((wno1 = wxopen(8,0,16,79,"Main Selection Menu",
  79.          BORDER+BD2+CURSOR+CENTER,24,80)) == -1)
  80.             terror("main():  Error opening Main Selection Menu window (WNO1)\n");
  81.         wselect(wno1);
  82.         empty(line, 80);
  83.         atsay(0,0,"Do you want to:");
  84.         atsay(1,0,"    1 - Plot a Julia Set");
  85.         atsay(2,0,"    2 - Plot a Mandelbrot Set");
  86.         atsay(3,0,"    3 - Load a previous picture");
  87.         atsay(4,0,"    4 - DIRectory of .PIC files");
  88.         atsay(5,0,"    Q - Quit to DOS");
  89.         atsayget(6,0,"Enter your choice (1, 2, 3, 4 or Q): ",line,"X");
  90.         wshow(wno1);
  91.         readgets();
  92.         switch (line[0]) {
  93.             case '1':                  /* Plot a Julia-Set */
  94.                 wselect(wno);
  95.                 wshow(wno);
  96.                 wclose(wno1);
  97.                 if ((wno1 == wxopen(8,0,12,79,"Picture File Selection",
  98.                  BORDER+BD2+CURSOR+CENTER,10,80)) == -1)
  99.                     terror("main(): Error opening Picture file selection window (WNO1)\n");
  100.                 wselect(wno1);
  101.                 empty(line, 80);
  102.                 atsayget(1,0,"Enter filename for picture SAVE:  ", line,
  103.                  "XXXXXXXX.XXX");
  104.                 wshow(wno1);
  105.                 readgets();
  106.                 if (isblank(line)){
  107.                     wselect(wno);
  108.                     wshow(wno);
  109.                     wclose(wno1);
  110.                     break;
  111.                     }
  112.                 whide(wno);
  113.                 set_plot(1);
  114.                 plot_fractal(result,1);
  115.                 wshow(wno);
  116.                 break;
  117.             case '2':                  /* Plot a Mendlebrot Graph */
  118.                 wselect(wno);
  119.                 wshow(wno);
  120.                 wclose(wno1);
  121.                 if ((wno1 == wxopen(8,0,12,79,"Picture File Selection",
  122.                  BORDER+BD2+CURSOR+CENTER,10,80)) == -1)
  123.                     terror("main(): Error opening Picture file selection window (WNO1)\n");
  124.                 wselect(wno1);
  125.                 empty(line, 80);
  126.                 atsayget(1,0,"Enter filename for picture SAVE:  ", line,
  127.                 "XXXXXXXX.XXX");
  128.                 wshow(wno1);
  129.                 readgets();
  130.                 if (isblank(line)){
  131.                     wselect(wno);
  132.                     wshow(wno);
  133.                     wclose(wno1);
  134.                     break;
  135.                     }
  136.                 whide(wno);
  137.                 set_plot(2);
  138.                 plot_fractal(result,2);
  139.                 wshow(wno);
  140.                 break;
  141.             case '3':                  /* Restore a previous picture */
  142.                 wselect(wno);
  143.                 wshow(wno);
  144.                 wclose(wno1);
  145.                 whide(wno);
  146.                 restore_picture();
  147.                 wshow(wno);
  148.                 break;
  149.             case '4':                  /* Directory of .PIC files */
  150.                 wselect(wno);
  151.                 wshow(wno);
  152.                 wclose(wno1);
  153.                 whide(wno);
  154.                 i = system("DIR *.PIC");
  155.                 printf("Press any key to continue . . . ");
  156.                 iresult = getone();
  157.                 wshow(wno1);
  158.                 break;
  159.             case 'Q':
  160.             case 'q':                  /* Quit to DOS */
  161.                 wclose(wno1);
  162.                 wclose(wno);
  163.                 vcend(CLOSE);
  164.                 printf("Good Bye...\n");
  165.                 alldone = 1;
  166.                 break;
  167.             default:                   /* Difficulty typing 'eh? */
  168.                 if ((errwno == wxopen(22,0,24,79,"Error Message",
  169.                  BORDER+BD2+CURSOR+CENTER+ACTIVE,5,80)) == -1)
  170.                     terror("main(): Error opening Error Message window (ERRWNO)\n");
  171.                 wselect(errwno);
  172.                 atsay(0,0,"Invalid Command....Please choose 1, 2, 3, 4 or Q");
  173.                 for (ii=0;ii<=2500;ii++)
  174.                     d_i = i * 1.01;
  175.                 wselect(wno);
  176.                 whide(errwno);
  177.                 wclose(errwno);
  178.                 wclose(wno1);
  179.                 break;
  180.             }  /* End of SWITCH */
  181.         }      /* End of WHILE */
  182. } /* End of MAIN */
  183.  
  184.  
  185. void set_plot(frac_mode)
  186. int frac_mode;
  187. {
  188.     int x_y_chosen, p_q_chosen, m_k_chosen, mode_chosen, deg_chosen, gr_m;
  189.     char line[80], *li_result, title[80], inp1[80], inp2[80],
  190.          inp3[80], inp4[80];
  191.  
  192.     v_lin_count = 0;
  193.     x_y_chosen = 0;
  194.     p_q_chosen = 0;
  195.     m_k_chosen = 0;
  196.     mode_chosen = 0;
  197.     deg_chosen = 0;
  198.     if (frac_mode == 1)
  199.         sprintf(title, "SETUP for a Julia Set plot");
  200.     else if (frac_mode == 2)
  201.         sprintf(title, "SETUP for a Mandelbrot Set plot");
  202.     if ((wno1 = wxopen(0,0,22,79,title,
  203.      ACTIVE+BORDER+BD2+SCROLL+CURSOR+CENTER,24,80)) == -1)
  204.         terror("set_plot():  Error opening main selection(s) window (WNO1)\n");
  205.     wselect(wno1);
  206.     while (!deg_chosen) {
  207.         sprintf(title,"Polynomial Degree Selection");
  208.         if ((wno2 = wxopen(5,0,9,79,title,
  209.          ACTIVE+BORDER+BD2+SCROLL+CURSOR+CENTER,24,80)) == -1)
  210.             terror("set_plot():  Error opening Degree Selection window (WNO2)\n");
  211.         wselect(wno2);
  212.         erase();
  213.         empty(inp1, 80);
  214.         atsayget(1, 0, "What degree is the polynomial (2-50)? ", inp1, "##");
  215.         readgets();
  216.         wclose(wno2);
  217.         degree = atoi(inp1);
  218.         if (degree >=2 && degree <= 50) {
  219.             if ((wno2 = wxopen(5,0,11,79,"Confirm (Y/N)",
  220.              ACTIVE+BORDER+BD1+SCROLL+CURSOR+CENTER,3,80)) == -1)
  221.                 terror("set_plot():  Error opening confirmation window.\n");
  222.             wselect(wno2);
  223.             sprintf(line,"Feedback polynomial is F(z)= z^%d + c.",degree);
  224.             atsay(0,0,line);
  225.             empty(inp2, 80);
  226.             atsayget(2, 0, "Is this correct (Y or N)? ", inp2, "A");
  227.             readgets();
  228.             wclose(wno2);
  229.             if (inp2[0] == 'Y')
  230.                 deg_chosen = 1;
  231.             else
  232.                 deg_chosen = 0;
  233.             }
  234.         }
  235.     if (frac_mode == 1) {                   /* Get Julia-Set parameters */
  236.         while (!x_y_chosen) {
  237.             sprintf(title,"X, Y Maximum and Minimum Selection");
  238.             if ((wno2 = wxopen(6, 0, 16, 79,title,
  239.              ACTIVE+BORDER+BD2+SCROLL+CURSOR+CENTER,24,80)) == -1)
  240.                 terror("set_plot():  Error opening X, Y Max & Min Selection window (WNO2)\n");
  241.             wselect(wno2);
  242.             erase();
  243.             empty(inp1, 80);
  244.             empty(inp2, 80);
  245.             empty(inp3, 80);
  246.             empty(inp4, 80);
  247.             atsayget(1, 0, "Enter X_MIN: ", inp1, "-#,###.##########");
  248.             atsayget(3, 0, "Enter X_MAX: ", inp2, "-#,###.##########");
  249.             atsayget(5, 0, "Enter Y_MIN: ", inp3, "-#,###.##########");
  250.             atsayget(7, 0, "Enter Y_MAX: ", inp4, "-#,###.##########");
  251.             readgets();
  252.             x_min = atof(inp1);
  253.             x_max = atof(inp2);
  254.             y_min = atof(inp3);
  255.             y_max = atof(inp4);
  256.             wclose(wno2);                 /* Close "X & Y-limits" Window */
  257.  
  258.             if ((wno2 = wxopen(5,0,17,79,"Confirm (Y/N)",
  259.              ACTIVE+BORDER+BD2+SCROLL+CURSOR+CENTER, 20, 80)) == -1)
  260.                 terror("set_plot():  Error opening X, Y Limits confirm window\n");
  261.             wselect(wno2);
  262.             woff();
  263.             atsay(0, 0, "The following are the X and Y limits chosen:");
  264.             sprintf(line, "              X_MIN = %-15.10lf", x_min);
  265.             atsay(2, 0, line);
  266.             sprintf(line, "              X_MAX = %-15.10lf", x_max);
  267.             atsay(4, 0, line);
  268.             sprintf(line, "              Y_MIN = %-15.10lf", y_min);
  269.             atsay(6, 0, line);
  270.             sprintf(line, "              Y_MAX = %-15.10lf", y_max);
  271.             atsay(8, 0, line);
  272.             won();
  273.             empty(line, 80);
  274.             atsayget(10, 0, "Are these correct (Y or N)? ", line, "A");
  275.             readgets();
  276.             if (line[0] == 'Y')
  277.                 x_y_chosen = 1;
  278.             else
  279.                 x_y_chosen = 0;
  280.             wclose(wno2);                 /* Close the "confirm" Window */
  281.             }
  282.         while (!p_q_chosen) {
  283.             sprintf(title,"P and Q Parameters Selection");
  284.             if ((wno2 = wxopen(7, 0, 13, 79,title,
  285.              ACTIVE+BORDER+BD2+SCROLL+CURSOR+CENTER,24,80)) == -1)
  286.                 terror("set_plot():  Error opening P & Q Selection window (WNO2)\n");
  287.             wselect(wno2);
  288.             erase();
  289.             empty(inp1, 80);
  290.             empty(inp2, 80);
  291.             atsay(0, 0, "For the complex value C = P + iQ");
  292.             atsayget(2, 0, "        Enter P: ", inp1, "-#,###.##########");
  293.             atsayget(4, 0, "        Enter Q: ", inp2, "-#,###.##########");
  294.             readgets();
  295.             p = atof(inp1);
  296.             q = atof(inp2);
  297.             wclose(wno2);                 /* Close "P-Q Window */
  298.  
  299.             if ((wno2 = wxopen(5,0,17,79,"Confirm (Y/N)",
  300.              ACTIVE+BORDER+BD2+SCROLL+CURSOR+CENTER, 20, 80)) == -1)
  301.                 terror("set_plot():  Error opening P, Q value confirm window\n");
  302.             wselect(wno2);
  303.             woff();
  304.             atsay(0, 0, "For the equation C = P + iQ the values are:");
  305.             sprintf(line, "          P = %-15.10lf", p);
  306.             atsay(2, 0, line);
  307.             sprintf(line, "          Q = %-15.10lf", q);
  308.             atsay(4, 0, line);
  309.             won();
  310.             empty(line, 80);
  311.             atsayget(6,0,"Are these correct (Y or N)? ", line, "A");
  312.             readgets();
  313.             if (line[0] == 'Y')
  314.                 p_q_chosen = 1;
  315.             else
  316.                 p_q_chosen = 0;
  317.             wclose(wno2);                 /* Close "confirm" Window */
  318.             }
  319.         }
  320.     else if (frac_mode == 2) {              /* Get Mendlebrot parameters */
  321.         while (!p_q_chosen) {
  322.             sprintf(title,"P, Q Maximum and Minimum Selection");
  323.             if ((wno2 = wxopen(6, 0, 16, 79,title,
  324.              ACTIVE+BORDER+BD2+SCROLL+CURSOR+CENTER,24,80)) == -1)
  325.                 terror("set_plot():  Error opening P, Q Max & Min Selection window (WNO2)\n");
  326.             wselect(wno2);
  327.             erase();
  328.             empty(inp1, 80);
  329.             empty(inp2, 80);
  330.             empty(inp3, 80);
  331.             empty(inp4, 80);
  332.             atsayget(1, 0, "Enter P_MIN: ", inp1, "-#,###.##########");
  333.             atsayget(3, 0, "Enter P_MAX: ", inp2, "-#,###.##########");
  334.             atsayget(5, 0, "Enter Q_MIN: ", inp3, "-#,###.##########");
  335.             atsayget(7, 0, "Enter Q_MAX: ", inp4, "-#,###.##########");
  336.             readgets();
  337.             p_min = atof(inp1);
  338.             p_max = atof(inp2);
  339.             q_min = atof(inp3);
  340.             q_max = atof(inp4);
  341.             wclose(wno2);                 /* Close "P & Q-limits" Window */
  342.  
  343.             if ((wno2 = wxopen(5,0,17,79,"Confirm (Y/N)",
  344.              ACTIVE+BORDER+BD2+SCROLL+CURSOR+CENTER, 20, 80)) == -1)
  345.                 terror("set_plot():  Error opening P, Q Limits confirm window\n");
  346.             wselect(wno2);
  347.             woff();
  348.             atsay(0, 0, "The following are the P and Q limits chosen:");
  349.             sprintf(line, "              P_MIN = %-15.10lf", p_min);
  350.             atsay(2, 0, line);
  351.             sprintf(line, "              P_MAX = %-15.10lf", p_max);
  352.             atsay(4, 0, line);
  353.             sprintf(line, "              Q_MIN = %-15.10lf", q_min);
  354.             atsay(6, 0, line);
  355.             sprintf(line, "              Q_MAX = %-15.10lf", q_max);
  356.             atsay(8, 0, line);
  357.             won();
  358.             empty(line, 80);
  359.             atsayget(10, 0, "Are these correct (Y or N)? ", line, "A");
  360.             readgets();
  361.             if (line[0] == 'Y')
  362.                 p_q_chosen = 1;
  363.             else
  364.                 p_q_chosen = 0;
  365.             wclose(wno2);                 /* Close the "confirm" Window */
  366.             }
  367.         }
  368.     else {
  369.         sprintf(line, "set_plot():  Invalid FRAC_MODE value -- %d\n",frac_mode);
  370.         terror(line);
  371.         return;
  372.         }
  373.  
  374.     while (!m_k_chosen) {
  375.         if ((wno2 = wxopen(5,0,13,79,"M & K parameter selection",
  376.          ACTIVE+BORDER+BD2+CURSOR+SCROLL+CENTER, 10, 80)) == -1)
  377.             terror("set_plot():  Error opening M & K parameter selection window\n");
  378.         wselect(wno2);
  379.         empty(inp1, 80);
  380.         empty(inp2, 80);
  381.         atsayget(0, 0, "Enter integer value for parameter M (suggest 100): ",
  382.          inp1, "#,###");
  383.  
  384.         atsay(2, 0, "Enter integer number of colors K to use,");
  385.         atsayget(3, 0, "  (suggest 200 even on CGA or EGA): ", inp2, "#,###");
  386.         readgets();
  387.         M = atoi(inp1);
  388.         K = atoi(inp2);
  389.         wclose(wno2);
  390.  
  391.         if ((wno2 = wxopen(5,0,13,79,"Confirm (Y/N)",
  392.          ACTIVE+BORDER+BD2+CURSOR+SCROLL+CENTER, 10, 80)) == -1)
  393.             terror("set_plot():  Error opening M & K confirmation window.\n");
  394.         wselect(wno2);
  395.         woff();
  396.         atsay(0, 0, "The values for M and K are:");
  397.         sprintf(line, "     M = %-4d", M);
  398.         atsay(2, 0, line);
  399.         sprintf(line, "     K = %-4d", K);
  400.         atsay(4, 0, line);
  401.         won();
  402.         empty(line, 80);
  403.         atsayget(6, 0, "Is this correct (Y or N)? ", line, "A");
  404.         readgets();
  405.         if (line[0] == 'Y')
  406.             m_k_chosen = 1;
  407.         else
  408.             m_k_chosen = 0;
  409.         wclose(wno2);
  410.         }
  411.  
  412.     while (!mode_chosen) {
  413.         if ((wno2 = wxopen(4,0,21,79,"Graphics Mode Selection",
  414.          ACTIVE+SCROLL+BORDER+CENTER+BD2+CURSOR,24, 80)) == -1)
  415.             terror("set_plot:  Error opening Mode Selection window\n");
  416.         wselect(wno2);
  417.         woff();
  418.         atsay(0 , 0, "Which graphics mode do you wish:");
  419.         atsay(2 , 0, "        1 = 320 X 200, 4-colors");
  420.         atsay(4 , 0, "        2 = 640 X 200, Black/White");
  421.         atsay(6 , 0, "        3 = 320 X 200, 16-color");
  422.         atsay(8 , 0, "        4 = 640 X 200, 16-color");
  423.         atsay(10, 0, "        5 = 640 X 350, 16-color");
  424.         won();
  425.         empty(line, 80);
  426.         atsayget(12, 0, "  Enter your choice (1, 2, 3, 4 or 5): ", line, "#");
  427.         readgets();
  428.         wclose(wno2);
  429.         USE_MODE = 0;
  430.         gr_m = atoi(line);
  431.         switch (gr_m) {
  432.             case 1:
  433.                 USE_MODE = 4;
  434.                 a = 320.0;
  435.                 b = 200.0;
  436.                 break;
  437.             case 2:
  438.                 USE_MODE = 6;
  439.                 a = 640.0;
  440.                 b = 200.0;
  441.                 break;
  442.             case 3:
  443.                 USE_MODE = 13;
  444.                 a = 320.0;
  445.                 b = 200.0;
  446.                 break;
  447.             case 4:
  448.                 USE_MODE = 14;
  449.                 a = 640.0;
  450.                 b = 200.0;
  451.                 break;
  452.             case 5:
  453.                 USE_MODE = 16;
  454.                 a = 640.0;
  455.                 b = 350.0;
  456.                 break;
  457.             }
  458.         if ((wno2 = wxopen(8,0,16,79,"Confirm (Y/N)",
  459.          CURSOR+BORDER+ACTIVE+BD2+CENTER+SCROLL, 10, 80)) == -1)
  460.             terror("set_plot():  Error opening resolution confirmation window\n");
  461.         wselect(wno2);
  462.         woff();
  463.         if (USE_MODE != 0) {
  464.             sprintf(line, "This plot will be done in %d X %d resolution",
  465.              (int)a, (int)b);
  466.             atsay(0, 0, line);
  467.             if (USE_MODE == 13 || USE_MODE == 14 || USE_MODE == 16)
  468.                 atsay(1, 0, "  (Requires EGA Card)");
  469.             empty(inp1, 80);
  470.             won();
  471.             atsayget(3, 0, "Is this correct (Y or N)? ", inp1, "A");
  472.             readgets();
  473.             if (inp1[0] == 'Y')
  474.                 mode_chosen = 1;
  475.             else
  476.                 mode_chosen = 0;
  477.             }
  478.         else {
  479.             sprintf(inp2, "Invalid USE_MODE assigned (%d) -- %d\n\"%s\"\n\n",
  480.              USE_MODE, gr_m, line);
  481.              terror (inp2);
  482.              }
  483.         wclose(wno2);
  484.         }
  485.     v_lin_max = (int)a - 1;
  486.     wclose(wno1);
  487. }  /* End of SET_PLOT */
  488.  
  489.  
  490. void plot_fractal(fi_name, frac_mode)
  491. char *fi_name;
  492. int frac_mode;
  493. {
  494.     register int k;
  495.     int i, ii, ij, j, save_mode, che, he_che, save_backg, get_che, ideg;
  496.     int gr_stuf[3];
  497.     double xy_stuf[4];
  498.     char line[80], *li_result;
  499.  
  500.     ideg = build_fctn(degree);      /* Generate polynomial vectors */
  501.     h_lin_count = 0;
  502.     mycolor = 0;
  503.     PALETTE = 1;                    /* Default is PALETTE = 0         */
  504.     get_mode();                     /* Save the current display stats */
  505.     save_mode = CURRENT_MODE;
  506.     set_mode(USE_MODE);             /* Turn on selected graphics mode */
  507.     if (USE_MODE == 4 || USE_MODE == 5) {
  508.     set_palette(PALETTE);
  509.     set_background(BACKGROUND);
  510.     }
  511.  
  512.     if (frac_mode == 1) {
  513.     delt_x = (x_max - x_min) / (a - 1);
  514.     delt_y = (y_max - y_min) / (b - 1);
  515.     c = p + q;
  516.     }
  517.     else if (frac_mode == 2) {
  518.     delt_p = (p_max - p_min) / (a - 1);
  519.     delt_q = (q_max - q_min) / (b - 1);
  520.     }
  521.  
  522.                    /* Now the work begins */
  523.  
  524. /* Step 1 */
  525.     for (n_x=0;n_x<a;n_x++) {
  526.     for (n_y=0;n_y<b;n_y++) {
  527.         k = 0;
  528.         if (frac_mode == 1) {
  529.         x[k] = x_min + (n_x * delt_x);
  530.         y[k] = y_min + (n_y * delt_y);
  531.         }
  532.         else if (frac_mode == 2) {
  533.         x[0] = 0;
  534.         y[0] = 0;
  535.         p = p_min + (n_x * delt_p);
  536.         q = q_min + (n_y * delt_q);
  537.         }
  538.  
  539. /* Step 2 (Iteration step) */
  540.         do {
  541.         x[k+1] = calc_nx(x[k],y[k]);    /* Feedback for next x using
  542.                         vectors generated by the
  543.                         function "build_fctn(int)" */
  544.  
  545.         y[k+1] = calc_ny(x[k],y[k]);    /* Feedback for next y using
  546.                         vectors generated by the
  547.                         function "build_fctn(int)" */
  548.  
  549.         k++;
  550.  
  551. /* Step 3 (Evaluation step) */
  552.         r = (x[k] * x[k]) + (y[k] * y[k]);
  553.         } while (r <= M && k < K);
  554.  
  555. /* Step 4 */
  556.         if (k == K)
  557.         mycolor = 0;
  558.         else {
  559.         if (USE_MODE == 6 || USE_MODE == 5)
  560.             mycolor = 1;             /* Black or White for 640*200 */
  561.         else if (USE_MODE == 13 || USE_MODE ==14 || USE_MODE == 16)
  562.             mycolor = (k % 15) + 1;
  563.         else if (USE_MODE == 4)
  564.             mycolor = (k % 3) + 1;   /* Adjust for 4 color 320*200 */
  565.         }
  566.         plot_point(n_x,((b - 1) - n_y),mycolor);         /* Plot point */
  567.     }    /* Next n_y */
  568.  
  569. /*
  570.     After plotting each vertical line, check for something from the keyboard.
  571.     The following are valid, the rest are discarded.
  572.  
  573.     <ESC>    -    Abort the current plot; do not save picture "as is".
  574.       Q      -    Quit the current plot; and save the picture "as is".
  575. */
  576.     if (kbhit()) {
  577.         get_che = getch();
  578.         if (get_che == 27) {
  579.         set_mode(save_mode);
  580.         return;
  581.         }
  582.         else if (get_che == 'q' || get_che == 'Q')
  583.         break;
  584.     }
  585.     }    /* Next n_x */
  586.  
  587. /*  Save the picture we just drew; then wait for a <CR> before continuing
  588.     or wait for a :
  589.  
  590.     ?    -    Display this menu
  591.     P    -    Change between color palette 0 and 1
  592.     B    -    Change background colors (from color 0 - 15)
  593.     D    -    Dump screen to printer (print plot)
  594.     C    -    Put a blank line on each odd numbered column.
  595.     H    -    Put a blank line on each odd numbered row (from the top down).
  596.     <CR>, <ESC> - Return to main menu
  597. */
  598.  
  599.     gr_stuf[0] = a;
  600.     gr_stuf[1] = b;
  601.     gr_stuf[2] = USE_MODE;
  602.     if (frac_mode == 1){
  603.     xy_stuf[0] = x_min;
  604.     xy_stuf[1] = x_max;
  605.     xy_stuf[2] = y_min;
  606.     xy_stuf[3] = y_max;
  607.     }
  608.     else {
  609.     xy_stuf[0] = p_min;
  610.     xy_stuf[1] = p_max;
  611.     xy_stuf[2] = q_min;
  612.     xy_stuf[3] = q_max;
  613.     }
  614.  
  615.     save_pic(fi_name,xy_stuf,gr_stuf);
  616.  
  617.     do {
  618.         che = getone();
  619.         readgets();
  620.         switch (che) {
  621.             case '?':
  622.                 wno2 = wxopen(5,20,17,58,"HELP",
  623.                  ACTIVE+CURSOR+CENTER+BORDER+BD3, 24, 80);
  624.                 wselect(wno2);
  625.                 woff();
  626.                 atsay(1,0," ? - Display this menu");
  627.                 atsay(2,0," P - Change color palette");
  628.                 atsay(3,0," B - Change background");
  629.                 atsay(4,0," D - Print screen");
  630.                 atsay(5,0," C - Blank line odd column.");
  631.                 atsay(6,0," H - Put a black line every 5 lines ");
  632.                 atsay(7,0,"     (from the top down)");
  633.                 atsay(8,0," <CR>, <ESC> - Return to main menu");
  634.                 atsay(9,0,"Press any key to exit");
  635.                 won();
  636.                 he_che = getone();
  637.                 readgets();
  638.                 wclose(wno2);
  639.                 break;
  640.             case 'p':
  641.             case 'P':
  642.                 if (PALETTE == 1)
  643.                     PALETTE = 0;
  644.                 else
  645.                     PALETTE = 1;
  646.                 if (USE_MODE == 4 || USE_MODE == 5)
  647.                     set_palette(PALETTE);
  648.                 break;
  649.             case 'b':
  650.             case 'B':
  651.                 BACKGROUND++;
  652.                 if (BACKGROUND > 15)
  653.                     BACKGROUND = 0;
  654.                 if (USE_MODE == 4 || USE_MODE == 5)
  655.                     set_background(BACKGROUND);
  656.                 break;
  657.             case 'd':
  658.             case 'D':
  659.                 if (USE_MODE == 4 || USE_MODE == 5) {
  660.                     save_backg = BACKGROUND;
  661.                     BACKGROUND = 0;
  662.                     set_background(BACKGROUND);
  663.                     }
  664.                 print_pic();
  665.                 if (USE_MODE == 4 || USE_MODE == 5) {
  666.                     BACKGROUND = save_backg;
  667.                     set_background(BACKGROUND);
  668.                     }
  669.                 break;
  670.             case 'c':
  671.             case 'C':
  672.                 if ((v_lin_count + 1) >= v_lin_max)
  673.                     break;
  674.                 v_lin_count++;
  675.                 for (i=0;i<=b-1;i++)
  676.                     plot_point(v_lin_count,i,0);
  677.                 v_lin_count++;
  678.                 break;
  679.             case 'h':
  680.             case 'H':
  681.                 ii = 4;
  682.                 while (ii<=b-1) {
  683.                     for (ij=0;ij<=25;ij++)
  684.                     plot_point(ij,ii,0);
  685.                     ii+=5;
  686.                     }
  687.                 break;
  688.             }
  689.         } while (che != '\033' && che != '\012' && che != '\015');
  690.  
  691.     set_mode(save_mode);
  692.  
  693. } /* End of PLOT_FRACTAL */
  694.  
  695.  
  696. void restore_picture()
  697. {
  698.     int i, ii, ij, j, k, fname_chosen, mo_chosen, save_mode, che, save_backg,
  699.         result, he_che, is_ega, max_row;
  700.     double d_i;
  701.     char line[80];
  702.     char *li_result, *fi_name;
  703.  
  704.     h_lin_count = 0;
  705.     v_lin_count = 0;
  706.     fname_chosen = 0;
  707.     mo_chosen = 0;
  708.     is_ega = 0;
  709.     if ((wno1 = wxopen(0,0,24,79,"Restore Previous Plot",
  710.      ACTIVE+BORDER+BD1+CURSOR+CENTER, 24, 80)) == -1)
  711.         terror("restore_picture():  Error opening restore main window\n");
  712.     erase();
  713.     if ((wno2 = wxopen(8,0,12,79,"Plot Type",
  714.      ACTIVE+BORDER+BD2+CURSOR+CENTER, 10, 80)) == -1)
  715.         terror("restore_picture():  Error opening PLOT TYPE window\n");
  716.     wselect(wno2);
  717.     empty(line, 80);
  718.     atsayget(0,0,"Is this an EGA plot (Y or N)? ", line, "A");
  719.     if (line[0] == 'Y')
  720.         is_ega = 1;
  721.     else
  722.         is_ega = 0;
  723.     wclose(wno2);
  724.     while (!fname_chosen) {
  725.         if ((wno2 = wxopen(6,0,10,79, "Filename selection",
  726.          ACTIVE+BORDER+BD2+CURSOR+CENTER, 5, 80)) == -1)
  727.             terror("restore_picture():  Error opening file name selection window\n");
  728.         empty(fi_name, 80);
  729.         wselect(wno2);
  730.         atsayget(1, 0, "Enter filename to load: ", fi_name, "XXXXXXXX.XXX");
  731.         readgets();
  732.         wclose(wno2);          /* Close "filename" Window */
  733.         wselect(wno1);
  734.         if (strlen(fi_name) == 0)
  735.             return;              /* Return on only <CR> as a response */
  736.  
  737. /* Check to see if file specified exists, if not, complain */
  738.  
  739.         result = stat(fi_name, &pic_buf);
  740.         if (result != 0) {
  741.             if ((wno2 = wxopen(20,0,24,79,"Error Message",
  742.              ACTIVE+BORDER+BD2+CENTER+CURSOR+SCROLL, 5, 80)) == -1)
  743.                 terror("restore_picture():  Error opening error message window\n");
  744.             sprintf(line, "Error opening filename (%s)", fi_name);
  745.             atsay(0,0,line);
  746.             for (i=0;i<=3000;i++)
  747.                 d_i = i * 1.01;
  748.             wclose(wno2);      /* Close "error" Window */
  749.             wselect(wno1);
  750.             }
  751.         else {
  752.             if (pic_buf.st_size == 64038 ||
  753.                pic_buf.st_size == 128038 ||
  754.                pic_buf.st_size == 224038) {
  755.                    fname_chosen = 1;
  756.                    if (pic_buf.st_size == 64038 ||
  757.                        pic_buf.st_size == 128038) {
  758.                            v_lin_max = (int)(pic_buf.st_size / 200) - 1;
  759.                            USE_MODE = 4;
  760.                            if (v_lin_max == 639)
  761.                                USE_MODE = 6;
  762.                            max_row = 199;
  763.                            }
  764.                        else if (pic_buf.st_size == 224038) {
  765.                            USE_MODE == 16;
  766.                            v_lin_max = 639;
  767.                            max_row = 349;
  768.                            }
  769.                    }
  770.             else {
  771.                 if ((wno2 = wxopen(20,0,24,79,"Error Message",
  772.                  ACTIVE+BORDER+BD2+CENTER+CURSOR+SCROLL, 5, 80)) == -1)
  773.                     terror("restore_picture():  Error opening error message window\n");
  774.                 wselect(wno2);
  775.                 sprintf(line, "Improper filesize for file (%s)", fi_name);
  776.                 atsay(0,0,line);
  777.                 fname_chosen = 0;
  778.                 for (i=0;i<=3000;i++)
  779.                     d_i = i * 1.01;
  780.                 wclose(wno2);  /* Close "filesize error" Window */
  781.                 wselect(wno1);
  782.                 }
  783.             }
  784.         }
  785.     if (is_ega) {
  786.         if (USE_MODE == 4)
  787.             USE_MODE = 13;
  788.         else if (USE_MODE == 6)
  789.             USE_MODE == 14;
  790.         }
  791.     mo_chosen = USE_MODE;
  792.     mycolor = 0;
  793.     PALETTE = 1;
  794.     BACKGROUND = 0;
  795.     get_mode();                    /* Save the current display stats */
  796.     save_mode = CURRENT_MODE;
  797.     set_mode(USE_MODE);           /* Turn on appropriate graphics mode */
  798.     if (USE_MODE == 4 || USE_MODE == 5) {
  799.     set_palette(PALETTE);          /* Select color palette */
  800.     set_background(BACKGROUND);    /* Default to Black background */
  801.     }
  802.  
  803.     load_pic(fi_name);
  804.  
  805.     do {
  806.         che = getone();
  807.         switch (che) {
  808.             case '?':
  809.                 wno2 = wxopen(5,20,17,58,"HELP",
  810.                  ACTIVE+CURSOR+CENTER+BORDER+BD3, 24, 80);
  811.                 wselect(wno2);
  812.                 woff();
  813.                 atsay(1,0," ? - Display this menu");
  814.                 atsay(2,0," P - Change color palette");
  815.                 atsay(3,0," B - Change background");
  816.                 atsay(4,0," D - Print screen");
  817.                 atsay(5,0," C - Blank line odd column.");
  818.                 atsay(6,0," H - Put a black line every 5 lines ");
  819.                 atsay(7,0,"     (from the top down)");
  820.                 atsay(8,0," <CR>, <ESC> - Return to main menu");
  821.                 atsay(9,0,"Press any key to exit");
  822.                 won();
  823.                 he_che = getone();
  824.                 readgets();
  825.                 wclose(wno2);
  826.                 break;
  827.             case 'p':
  828.             case 'P':
  829.                 if (PALETTE == 1)
  830.                     PALETTE = 0;
  831.                 else
  832.                     PALETTE = 1;
  833.                 if (USE_MODE == 4 || USE_MODE == 5)
  834.                     set_palette(PALETTE);
  835.                 break;
  836.             case 'b':
  837.             case 'B':
  838.                 BACKGROUND++;
  839.                 if (BACKGROUND > 15)
  840.                     BACKGROUND = 0;
  841.                 if (USE_MODE == 4 || USE_MODE == 5)
  842.                     set_background(BACKGROUND);
  843.                 break;
  844.             case 'd':
  845.             case 'D':
  846.                 if (USE_MODE == 4 || USE_MODE == 5) {
  847.                     save_backg = BACKGROUND;
  848.                     BACKGROUND = 0;
  849.                     set_background(BACKGROUND);
  850.                     }
  851.                 print_pic();
  852.                 if (USE_MODE == 4 || USE_MODE == 5) {
  853.                     BACKGROUND = save_backg;
  854.                     set_background(BACKGROUND);
  855.                     }
  856.                 break;
  857.             case 'c':
  858.             case 'C':
  859.                 if ((v_lin_count + 1) >= v_lin_max)
  860.                     break;
  861.                 v_lin_count++;
  862.                 for (i=0;i<=b-1;i++)
  863.                     plot_point(v_lin_count,i,0);
  864.                 v_lin_count++;
  865.                 break;
  866.             case 'h':
  867.             case 'H':
  868.                 ii = 4;
  869.                 while (ii<=b-1) {
  870.                     for (ij=0;ij<=25;ij++)
  871.                     plot_point(ij,ii,0);
  872.                     ii+=5;
  873.                     }
  874.                 break;
  875.             }
  876.         } while (che != '\033' && che != '\012' && che != '\015');
  877.  
  878.     set_mode(save_mode);
  879.     wclose(wno1);
  880.  
  881. } /* End of RESTORE_PICTURE */
  882.  
  883.  
  884. long factorial(fac_num)
  885. int fac_num;
  886. {
  887.     register int i;
  888.     long f_ret;
  889.  
  890.     if (fac_num <= 1)
  891.     return(1);
  892.     f_ret = 1;
  893.     for (i=1;i<=fac_num;i++)
  894.     f_ret *= ((long)i);
  895.     return(f_ret);
  896.  
  897. } /* End of FACTORIAL */
  898.  
  899. int build_fctn(degree)
  900. int degree;
  901. {
  902.     register int d_kount, k_adj, equ_loc;
  903.  
  904.     equ_loc = 2;
  905.     konst[0] = 1;
  906.  
  907. /*
  908.     Initialize the first and last elements of the constant and exponent
  909.     arrays.
  910. */
  911.     real_exp[0] = degree;
  912.     imag_exp[0] = 0;
  913.     konst[degree] = 1;
  914.     real_exp[degree] = 0;
  915.     imag_exp[degree] = degree;
  916.  
  917. /*
  918.     Now loop through for each individual element of the equation we are
  919.     generating and calculate the constant, and exponent arrays.
  920. */
  921.     for (d_kount=1;d_kount<degree;d_kount++) {
  922.     konst[d_kount] = 1;
  923.     for (k_adj=0;k_adj<d_kount;k_adj++)
  924.         konst[d_kount] *= (degree - k_adj);
  925.     konst[d_kount] /= factorial(d_kount);
  926.     real_exp[d_kount] = degree - d_kount;
  927.     imag_exp[d_kount] = d_kount;
  928.     }
  929.  
  930. /*
  931.     Now adjust the sign(s) of the equation.  It follows a "++--++--..."
  932.     pattern.
  933. */
  934.     do {
  935.     konst[equ_loc] *= -1;
  936.     if (equ_loc + 1 <= degree)
  937.         konst[equ_loc + 1] *= -1;
  938.     equ_loc += 4;
  939.     } while (equ_loc <= degree);
  940.     return(degree);
  941.  
  942. } /* End of BUILD_FCTN */
  943.  
  944.  
  945. double calc_nx(old_x,old_y)
  946. double old_x, old_y;
  947. {
  948.     register int i;
  949.     double new_x, r_exp, i_exp, t_value;
  950.  
  951.     new_x = 0.0;
  952.     for (i=0;i<=degree;i+=2) {
  953.     t_value = ((double)konst[i]) * pow_r(old_x, real_exp[i]);
  954.     t_value *= pow_r(old_y, imag_exp[i]);
  955.     new_x += t_value;
  956.     }
  957.     new_x += p;
  958.     return(new_x);
  959.  
  960. } /* End of CALC_NX */
  961.  
  962.  
  963. double calc_ny(old_x, old_y)
  964. double old_x, old_y;
  965. {
  966.     register int i;
  967.     double new_y, r_exp, i_exp, t_value;
  968.  
  969.     new_y = 0.0;
  970.     for (i=1;i<=degree;i+=2) {
  971.     t_value = ((double)konst[i]) * pow_r(old_x,real_exp[i]);
  972.     t_value *= pow_r(old_y, imag_exp[i]);
  973.     new_y += t_value;
  974.     }
  975.     new_y += q;
  976.     return(new_y);
  977.  
  978. } /* End of CALC_NY */
  979.  
  980.  
  981. /*
  982.     For some reason the "pow(double,double)" function included in the
  983.     Microsoft 'C' 4.00 compiler conflicts with the graphics routines
  984.     in many cases.  Thus the "pow_r(double,int)" function is designed
  985.     as a crude replacement for what we are doing.
  986. */
  987. double pow_r(num_r, exp_t)
  988. double num_r;
  989. int exp_t;
  990. {
  991.     register int i;
  992.     double temp_d;
  993.  
  994.     if (exp_t == 0)
  995.     return(1.0);
  996.     temp_d = 1.0;
  997.     for (i=1;i<=exp_t;i++)
  998.     temp_d *= num_r;
  999.     return(temp_d);
  1000.  
  1001. } /* End of POW_R */
  1002.  
  1003.