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

  1. //+-------------------------------------------------------------------+
  2.  
  3. //+ Program KAMTORUS.CPP, version 0.3                                 +
  4.  
  5. //+ Plots *hundreds* of Kamtorus fractals.                            +
  6.  
  7. //+ (when you think it's all over... a new one is produced!)          +
  8.  
  9. //+                                                                   +
  10.  
  11. //+ By Ramiro Perez (Panama), RPEREZ@UTPVM1.BITNET                    +
  12.  
  13. //+ and Fausto A. A. Barbuto (Brazil), BJ06@C53000.PETROBRAS.ANRJ.BR  +
  14.  
  15. //+ April 9, 1994.                                                    +
  16.  
  17. //+                                                                   +
  18.  
  19. //+ New formulation, colour palette and random number generator added +
  20.  
  21. //+ by Michael Sargent (USA), msargent@moose.uvm.edu, on April 28,    +
  22.  
  23. //+ 1994.                                                             +
  24.  
  25. //+                                                                   +
  26.  
  27. //+ Press any key to stop execution or PAUSE to freeze.               +
  28.  
  29. //+ SVGA256 version; supports up to five video screens.               +
  30.  
  31. //+ Authorized version for spanky.triumf.ca site.                     +
  32.  
  33. //+                                                                   +
  34.  
  35. //+-------------------------------------------------------------------+
  36.  
  37. #include <time.h>
  38.  
  39. #include <graphics.h>
  40.  
  41. #include <math.h>
  42.  
  43. #include <conio.h>
  44.  
  45. #include <stdlib.h>
  46.  
  47. #include <stdio.h>
  48.  
  49. #include <dos.h>
  50.  
  51. #include "Svga256.h"
  52.  
  53.     
  54.  
  55. void setVGApalette(char *buffer);
  56.  
  57. double qsrandom(void);
  58.  
  59.     
  60.  
  61. char palette[2][256][3]={{
  62.  
  63. { 0, 0, 0},{ 63, 0, 32},{ 63, 0, 32},{ 63, 0, 33},
  64.  
  65. { 63, 0, 33},{ 63, 0, 34},{ 63, 0, 34},{ 63, 0, 35},
  66.  
  67. { 63, 0, 35},{ 63, 0, 36},{ 63, 0, 36},{ 63, 0, 37},
  68.  
  69. { 63, 0, 37},{ 63, 0, 38},{ 63, 0, 38},{ 63, 0, 39},
  70.  
  71. { 63, 0, 39},{ 63, 0, 40},{ 63, 0, 40},{ 63, 0, 41},
  72.  
  73. { 63, 0, 41},{ 63, 0, 42},{ 63, 0, 42},{ 63, 0, 43},
  74.  
  75. { 63, 0, 43},{ 63, 0, 44},{ 63, 0, 44},{ 63, 0, 45},
  76.  
  77. { 63, 0, 45},{ 63, 0, 46},{ 63, 0, 46},{ 63, 0, 47},
  78.  
  79. { 63, 0, 47},{ 63, 0, 48},{ 63, 0, 48},{ 63, 0, 49},
  80.  
  81. { 63, 0, 49},{ 63, 0, 50},{ 63, 0, 50},{ 63, 0, 51},
  82.  
  83. { 63, 0, 51},{ 63, 0, 52},{ 63, 0, 52},{ 63, 0, 53},
  84.  
  85. { 63, 0, 53},{ 63, 0, 54},{ 63, 0, 54},{ 63, 0, 55},
  86.  
  87. { 63, 0, 55},{ 63, 0, 56},{ 63, 0, 56},{ 63, 0, 57},
  88.  
  89. { 63, 0, 57},{ 63, 0, 58},{ 63, 0, 58},{ 63, 0, 59},
  90.  
  91. { 63, 0, 59},{ 63, 0, 60},{ 63, 0, 60},{ 63, 0, 61},
  92.  
  93. { 63, 0, 61},{ 63, 0, 62},{ 63, 0, 62},{ 63, 0, 63},
  94.  
  95. { 63, 0, 63},{ 62, 0, 63},{ 61, 0, 63},{ 60, 0, 63},
  96.  
  97. { 59, 0, 63},{ 58, 0, 63},{ 57, 0, 63},{ 56, 0, 63},
  98.  
  99. { 55, 0, 63},{ 54, 0, 63},{ 53, 0, 63},{ 52, 0, 63},
  100.  
  101. { 51, 0, 63},{ 50, 0, 63},{ 49, 0, 63},{ 48, 0, 63},
  102.  
  103. { 47, 0, 63},{ 46, 0, 63},{ 45, 0, 63},{ 44, 0, 63},
  104.  
  105. { 43, 0, 63},{ 42, 0, 63},{ 41, 0, 63},{ 40, 0, 63},
  106.  
  107. { 39, 0, 63},{ 38, 0, 63},{ 37, 0, 63},{ 36, 0, 63},
  108.  
  109. { 35, 0, 63},{ 34, 0, 63},{ 33, 0, 63},{ 32, 0, 63},
  110.  
  111. { 31, 0, 63},{ 30, 0, 63},{ 29, 0, 63},{ 28, 0, 63},
  112.  
  113. { 27, 0, 63},{ 26, 0, 63},{ 25, 0, 63},{ 24, 0, 63},
  114.  
  115. { 23, 0, 63},{ 22, 0, 63},{ 21, 0, 63},{ 20, 0, 63},
  116.  
  117. { 19, 0, 63},{ 18, 0, 63},{ 17, 0, 63},{ 16, 0, 63},
  118.  
  119. { 15, 0, 63},{ 14, 0, 63},{ 13, 0, 63},{ 12, 0, 63},
  120.  
  121. { 11, 0, 63},{ 10, 0, 63},{ 9, 0, 63},{ 8, 0, 63},
  122.  
  123. { 7, 0, 63},{ 6, 0, 63},{ 5, 0, 63},{ 4, 0, 63},
  124.  
  125. { 3, 0, 63},{ 2, 0, 63},{ 1, 0, 63},{ 0, 0, 63},
  126.  
  127. { 0, 0, 63},{ 1, 0, 63},{ 2, 0, 63},{ 3, 0, 63},
  128.  
  129. { 4, 0, 63},{ 5, 0, 63},{ 6, 0, 63},{ 7, 0, 63},
  130.  
  131. { 8, 0, 63},{ 9, 0, 63},{ 10, 0, 63},{ 11, 0, 63},
  132.  
  133. { 12, 0, 63},{ 13, 0, 63},{ 14, 0, 63},{ 15, 0, 63},
  134.  
  135. { 16, 0, 63},{ 17, 0, 63},{ 18, 0, 63},{ 19, 0, 63},
  136.  
  137. { 20, 0, 63},{ 21, 0, 63},{ 22, 0, 63},{ 23, 0, 63},
  138.  
  139. { 24, 0, 63},{ 25, 0, 63},{ 26, 0, 63},{ 27, 0, 63},
  140.  
  141. { 28, 0, 63},{ 29, 0, 63},{ 30, 0, 63},{ 31, 0, 63},
  142.  
  143. { 32, 0, 63},{ 33, 0, 63},{ 34, 0, 63},{ 35, 0, 63},
  144.  
  145. { 36, 0, 63},{ 37, 0, 63},{ 38, 0, 63},{ 39, 0, 63},
  146.  
  147. { 40, 0, 63},{ 41, 0, 63},{ 42, 0, 63},{ 43, 0, 63},
  148.  
  149. { 44, 0, 63},{ 45, 0, 63},{ 46, 0, 63},{ 47, 0, 63},
  150.  
  151. { 48, 0, 63},{ 49, 0, 63},{ 50, 0, 63},{ 51, 0, 63},
  152.  
  153. { 52, 0, 63},{ 53, 0, 63},{ 54, 0, 63},{ 55, 0, 63},
  154.  
  155. { 56, 0, 63},{ 57, 0, 63},{ 58, 0, 63},{ 59, 0, 63},
  156.  
  157. { 60, 0, 63},{ 61, 0, 63},{ 62, 0, 63},{ 63, 0, 63},
  158.  
  159. { 63, 0, 63},{ 63, 0, 63},{ 63, 0, 62},{ 63, 0, 62},
  160.  
  161. { 63, 0, 61},{ 63, 0, 61},{ 63, 0, 60},{ 63, 0, 60},
  162.  
  163. { 63, 0, 59},{ 63, 0, 59},{ 63, 0, 58},{ 63, 0, 58},
  164.  
  165. { 63, 0, 57},{ 63, 0, 57},{ 63, 0, 56},{ 63, 0, 56},
  166.  
  167. { 63, 0, 55},{ 63, 0, 55},{ 63, 0, 54},{ 63, 0, 54},
  168.  
  169. { 63, 0, 53},{ 63, 0, 53},{ 63, 0, 52},{ 63, 0, 52},
  170.  
  171. { 63, 0, 51},{ 63, 0, 51},{ 63, 0, 50},{ 63, 0, 50},
  172.  
  173. { 63, 0, 49},{ 63, 0, 49},{ 63, 0, 48},{ 63, 0, 48},
  174.  
  175. { 63, 0, 47},{ 63, 0, 47},{ 63, 0, 46},{ 63, 0, 46},
  176.  
  177. { 63, 0, 45},{ 63, 0, 45},{ 63, 0, 44},{ 63, 0, 44},
  178.  
  179. { 63, 0, 43},{ 63, 0, 43},{ 63, 0, 42},{ 63, 0, 42},
  180.  
  181. { 63, 0, 41},{ 63, 0, 41},{ 63, 0, 40},{ 63, 0, 40},
  182.  
  183. { 63, 0, 39},{ 63, 0, 39},{ 63, 0, 38},{ 63, 0, 38},
  184.  
  185. { 63, 0, 37},{ 63, 0, 37},{ 63, 0, 36},{ 63, 0, 36},
  186.  
  187. { 63, 0, 35},{ 63, 0, 35},{ 63, 0, 34},{ 63, 0, 34},
  188.  
  189. { 63, 0, 33},{ 63, 0, 33},{ 63, 0, 32},{ 63, 0, 32}
  190.  
  191. },
  192.  
  193.     
  194.  
  195. {
  196.  
  197. { 0, 0, 0},{ 63, 16, 0},{ 63, 15, 0},{ 63, 15, 0},
  198.  
  199. { 63, 14, 0},{ 63, 13, 0},{ 63, 12, 0},{ 63, 11, 0},
  200.  
  201. { 63, 10, 0},{ 63, 9, 0},{ 63, 8, 0},{ 63, 7, 0},
  202.  
  203. { 63, 6, 0},{ 63, 5, 0},{ 63, 4, 0},{ 63, 3, 0},
  204.  
  205. { 63, 2, 0},{ 63, 1, 0},{ 63, 0, 0},{ 63, 0, 0},
  206.  
  207. { 62, 0, 0},{ 61, 0, 0},{ 61, 0, 0},{ 60, 0, 0},
  208.  
  209. { 59, 0, 0},{ 58, 0, 0},{ 58, 0, 0},{ 57, 0, 0},
  210.  
  211. { 56, 0, 0},{ 56, 0, 0},{ 55, 0, 0},{ 54, 0, 0},
  212.  
  213. { 53, 0, 0},{ 53, 0, 0},{ 52, 0, 0},{ 51, 0, 0},
  214.  
  215. { 51, 0, 0},{ 50, 0, 0},{ 49, 0, 0},{ 48, 0, 0},
  216.  
  217. { 48, 0, 0},{ 47, 0, 0},{ 46, 0, 0},{ 46, 0, 0},
  218.  
  219. { 45, 0, 0},{ 44, 0, 0},{ 43, 0, 0},{ 43, 0, 0},
  220.  
  221. { 42, 0, 0},{ 41, 0, 0},{ 40, 0, 0},{ 40, 0, 0},
  222.  
  223. { 39, 1, 1},{ 38, 1, 1},{ 37, 2, 2},{ 36, 2, 2},
  224.  
  225. { 35, 3, 3},{ 35, 3, 3},{ 34, 4, 4},{ 33, 4, 4},
  226.  
  227. { 33, 5, 5},{ 33, 5, 5},{ 33, 6, 6},{ 33, 6, 6},
  228.  
  229. { 33, 7, 7},{ 33, 7, 7},{ 33, 8, 8},{ 33, 8, 8},
  230.  
  231. { 33, 8, 8},{ 33, 8, 8},{ 33, 8, 8},{ 33, 8, 8},
  232.  
  233. { 33, 8, 8},{ 33, 8, 8},{ 33, 8, 8},{ 33, 8, 8},
  234.  
  235. { 33, 8, 8},{ 33, 8, 8},{ 33, 8, 8},{ 33, 8, 8},
  236.  
  237. { 33, 8, 8},{ 33, 8, 8},{ 33, 8, 8},{ 33, 8, 8},
  238.  
  239. { 35, 8, 8},{ 34, 8, 8},{ 35, 8, 8},{ 36, 8, 8},
  240.  
  241. { 36, 8, 8},{ 37, 8, 8},{ 37, 8, 8},{ 37, 8, 8},
  242.  
  243. { 37, 8, 8},{ 37, 8, 8},{ 38, 8, 8},{ 38, 8, 8},
  244.  
  245. { 38, 8, 8},{ 38, 8, 8},{ 39, 8, 8},{ 41, 8, 8},
  246.  
  247. { 42, 7, 7},{ 44, 6, 6},{ 45, 6, 6},{ 47, 5, 5},
  248.  
  249. { 49, 5, 5},{ 50, 4, 4},{ 52, 4, 4},{ 53, 3, 3},
  250.  
  251. { 55, 3, 3},{ 56, 2, 2},{ 58, 2, 2},{ 59, 1, 1},
  252.  
  253. { 61, 1, 1},{ 63, 0, 0},{ 63, 1, 0},{ 63, 3, 0},
  254.  
  255. { 63, 5, 0},{ 63, 7, 0},{ 63, 9, 0},{ 63, 11, 0},
  256.  
  257. { 63, 13, 0},{ 63, 15, 0},{ 63, 17, 0},{ 63, 19, 0},
  258.  
  259. { 63, 21, 0},{ 63, 23, 0},{ 63, 25, 0},{ 63, 27, 0},
  260.  
  261. { 63, 29, 0},{ 63, 31, 0},{ 63, 33, 0},{ 63, 35, 0},
  262.  
  263. { 63, 37, 0},{ 63, 39, 0},{ 63, 41, 0},{ 63, 43, 0},
  264.  
  265. { 63, 45, 0},{ 63, 47, 0},{ 63, 49, 0},{ 63, 51, 0},
  266.  
  267. { 63, 53, 0},{ 63, 55, 0},{ 63, 57, 0},{ 63, 59, 0},
  268.  
  269. { 63, 61, 0},{ 63, 63, 0},{ 63, 63, 1},{ 63, 63, 3},
  270.  
  271. { 63, 63, 5},{ 63, 63, 7},{ 63, 63, 9},{ 63, 63, 11},
  272.  
  273. { 63, 63, 13},{ 63, 63, 15},{ 63, 63, 17},{ 63, 63, 19},
  274.  
  275. { 63, 63, 21},{ 63, 63, 23},{ 63, 63, 25},{ 63, 63, 27},
  276.  
  277. { 63, 63, 29},{ 63, 63, 31},{ 63, 63, 33},{ 63, 63, 35},
  278.  
  279. { 63, 63, 37},{ 63, 63, 39},{ 63, 63, 41},{ 63, 63, 43},
  280.  
  281. { 63, 63, 45},{ 63, 63, 47},{ 63, 63, 49},{ 63, 63, 51},
  282.  
  283. { 63, 63, 53},{ 63, 63, 55},{ 63, 63, 57},{ 63, 63, 59},
  284.  
  285. { 63, 63, 61},{ 63, 63, 61},{ 63, 63, 63},{ 63, 62, 62},
  286.  
  287. { 63, 62, 61},{ 63, 61, 60},{ 63, 61, 59},{ 63, 60, 58},
  288.  
  289. { 63, 60, 57},{ 63, 59, 56},{ 63, 59, 55},{ 63, 58, 54},
  290.  
  291. { 63, 58, 53},{ 63, 57, 52},{ 63, 57, 51},{ 63, 56, 50},
  292.  
  293. { 63, 56, 49},{ 63, 55, 48},{ 63, 55, 47},{ 63, 54, 46},
  294.  
  295. { 63, 54, 45},{ 63, 53, 44},{ 63, 53, 43},{ 63, 52, 42},
  296.  
  297. { 63, 52, 41},{ 63, 51, 40},{ 63, 51, 39},{ 63, 50, 38},
  298.  
  299. { 63, 50, 37},{ 63, 49, 36},{ 63, 49, 35},{ 63, 48, 34},
  300.  
  301. { 63, 48, 33},{ 63, 47, 32},{ 63, 46, 31},{ 63, 46, 30},
  302.  
  303. { 63, 45, 29},{ 63, 45, 28},{ 63, 44, 27},{ 63, 44, 26},
  304.  
  305. { 63, 43, 25},{ 63, 43, 24},{ 63, 42, 23},{ 63, 42, 22},
  306.  
  307. { 63, 41, 21},{ 63, 41, 20},{ 63, 40, 19},{ 63, 40, 18},
  308.  
  309. { 63, 39, 17},{ 63, 39, 16},{ 63, 38, 15},{ 63, 38, 14},
  310.  
  311. { 63, 37, 13},{ 63, 37, 12},{ 63, 36, 11},{ 63, 36, 10},
  312.  
  313. { 63, 35, 9},{ 63, 35, 8},{ 63, 34, 7},{ 63, 34, 6},
  314.  
  315. { 63, 33, 5},{ 63, 33, 4},{ 63, 32, 3},{ 63, 32, 2},
  316.  
  317. { 63, 31, 1},{ 63, 30, 0},{ 63, 30, 0},{ 63, 29, 0},
  318.  
  319. { 63, 28, 0},{ 63, 27, 0},{ 63, 26, 0},{ 63, 25, 0},
  320.  
  321. { 63, 24, 0},{ 63, 23, 0},{ 63, 22, 0},{ 63, 21, 0},
  322.  
  323. { 63, 20, 0},{ 63, 19, 0},{ 63, 18, 0},{ 63, 17, 0}
  324.  
  325. }};
  326.  
  327.     
  328.  
  329. int Vid;  //Global variable
  330.  
  331.  
  332.  
  333. int huge DetectSVGA256()
  334.  
  335. {
  336.  
  337.   printf("\n Which video mode would you like to use? \n\n");
  338.  
  339.   printf(" 1 - 640x400x256\n");
  340.  
  341.   printf(" 2 - 640x480x256\n");
  342.  
  343.   printf(" 3 - 800x600x256\n");
  344.  
  345.   printf(" 4 - 1024x768x256\n\n ===> ");
  346.  
  347.   scanf("%d",&Vid);
  348.  
  349.   if((Vid<1) || (Vid)>4) Vid = 2;
  350.  
  351.   return Vid;
  352.  
  353. }
  354.  
  355.  
  356.  
  357. void main(void)
  358.  
  359. {
  360.  
  361.   int a, c, nx, ny, palflag=0, iopt;
  362.  
  363.   unsigned long k;
  364.  
  365.   double an, can, san, can1, san1, r, ax, ay, mult1, mult2;
  366.  
  367.   double x, xa, x1, x2, x3, y, y1, y2, y3, rand1, rand2;
  368.  
  369.   int graphdriver=DETECT, graphmode;
  370.  
  371.  
  372.  
  373.   clrscr();
  374.  
  375.   printf("\n Program KAMTORUS.CPP \n\n");
  376.  
  377.   printf("\n Select the multipliers: \n");
  378.  
  379.   printf("\n   0.990 and 1.010 (Ramiro's defaults)      --> Enter 1\n");
  380.  
  381.   printf("\n   0.995 and 1.005 (Ramiro's suggestion)    --> Enter 2\n");
  382.  
  383.   printf("\n   0.999 and 1.001 (Mike's new multipliers) --> Enter 3\n\n> ");
  384.  
  385.  
  386.  
  387.   scanf("%d",&iopt);
  388.  
  389.   if ((iopt > 3) || (iopt<1)) iopt = 2;
  390.  
  391.   if (iopt == 1) {mult1 = 0.990 ; mult2 = 1.010;}
  392.  
  393.   if (iopt == 2) {mult1 = 0.995 ; mult2 = 1.005;}
  394.  
  395.   if (iopt == 3) {mult1 = 0.999 ; mult2 = 1.001;}
  396.  
  397.   clrscr();
  398.  
  399.  
  400.  
  401.   installuserdriver("Svga256",DetectSVGA256);
  402.  
  403.   initgraph(&graphdriver,&graphmode,"C:\\BORLANDC\\BGI");
  404.  
  405.     
  406.  
  407.   if (Vid == 1) { nx = 320; ny = 200; ax = 640; ay = 533;}
  408.  
  409.   if (Vid == 2) { nx = 320; ny = 240; ax = ay = 640;}
  410.  
  411.   if (Vid == 3) { nx = 400; ny = 300; ax = 640; ay = 533;}
  412.  
  413.   if (Vid == 4) { nx = 512; ny = 384; ax = ay = 640;}
  414.  
  415.  
  416.  
  417.   randomize();
  418.  
  419.   do {
  420.  
  421.     cleardevice();
  422.  
  423.     palflag = (palflag + 1) %2;
  424.  
  425.     setVGApalette(palette[palflag][0]);
  426.  
  427.     c = 1;  
  428.  
  429.     rand1 = qsrandom();
  430.  
  431.     rand2 = qsrandom();
  432.  
  433.     an = 10.0*(rand1-rand2);
  434.  
  435.     can = mult1*cos(an);
  436.  
  437.     san = mult1*sin(an);
  438.  
  439.     can1 = mult2*cos(an);
  440.  
  441.     san1 = mult2*sin(an);
  442.  
  443.     x3 = 0.01;
  444.  
  445.     y3 = 0.01;
  446.  
  447.     do {
  448.  
  449.       xa = x3*x3 - y3;
  450.  
  451.       x2 = x3*can1 + xa*san1;
  452.  
  453.       y2 = x3*san1 - xa*can1;
  454.  
  455.       x3 = x2;
  456.  
  457.       y3 = y2;
  458.  
  459.       x = x2;
  460.  
  461.       y = y2;
  462.  
  463.       a = 0;
  464.  
  465.       do {
  466.  
  467.     xa = x*x - y;
  468.  
  469.     x1 = x*can + xa*san;
  470.  
  471.     y1 = x*san - xa*can;
  472.  
  473.     x  = x1;
  474.  
  475.     y  = y1;
  476.  
  477.     a++;
  478.  
  479.     putpixel((int)(ax*x+nx),(int)(ay*y+ny),c);
  480.  
  481.       }  while ((fabs(x1)<=2.0e3) && (fabs(y1)<=2.0e3) && a<=100);
  482.  
  483.       c++ %255 + 1;
  484.  
  485.     } while ((fabs(x2) <= 2.0e3) && (fabs(y2) <= 2.0e3));
  486.  
  487.     delay(1500);
  488.  
  489.   } while (!kbhit());
  490.  
  491.   getch();
  492.  
  493.   closegraph();
  494.  
  495. }
  496.  
  497.     
  498.  
  499. //==========================================================================
  500.  
  501. // Function to set all 256 color registers
  502.  
  503. //==========================================================================
  504.  
  505. #pragma warn -eff
  506.  
  507. void setVGApalette(char *buffer)
  508.  
  509. {
  510.  
  511.    int i, j;
  512.  
  513.     
  514.  
  515.    for (i=0;i<256;i++)
  516.  
  517.    {
  518.  
  519.       outp(0x3c8, i);
  520.  
  521.       for (j=0;j<3;j++)
  522.  
  523.      outp(0x3c9, *buffer++);
  524.  
  525.    }
  526.  
  527. }
  528.  
  529. #pragma warn +eff
  530.  
  531.     
  532.  
  533. //==========================================================================
  534.  
  535. // Function to generate "random" numbers of 9 decimal places between 0 & 1
  536.  
  537. //==========================================================================
  538.  
  539. double qsrandom(void)
  540.  
  541. {
  542.  
  543.    int random_integer, temp_integer;
  544.  
  545.    double random_double, temp_double;
  546.  
  547.     
  548.  
  549.    random_integer = random(RAND_MAX);
  550.  
  551.    random_double = (double)random_integer / RAND_MAX;
  552.  
  553.    temp_integer = random(30519);
  554.  
  555.    temp_double = (double)temp_integer / 1000000000L;
  556.  
  557.    random_double += temp_double;
  558.  
  559.     
  560.  
  561.    return(random_double);
  562.  
  563. }
  564.  
  565.