home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / NewDynamic 1.0.1 / NewDynamic.c < prev    next >
Encoding:
Text File  |  1995-12-01  |  12.1 KB  |  571 lines  |  [TEXT/CWIE]

  1. // NewDynamic
  2. // version 1.0.1
  3. // translated to THINK C by Ken Long <kenlong@netcom.com>
  4. // ported to CW7 on 951201
  5.  
  6. //• Another old, dead source code brought back to life by
  7. //• Kenneth A. Long, at itty bitty bytes(tm)!
  8.  
  9. //• Originally a MacPascal source, from Northwestern University,
  10. //• from March 22, 1985!  I translated it to Think C and perked it 
  11. //• up a little.
  12.  
  13. //• I was interested in the drawing - not the console crap, so I
  14. //• got rid of all of it.  It should be replaced by a dialog or some
  15. //• on-screen controls.
  16.  
  17. //• I added some colorization and a vertical line drawing routine.  I
  18. //• also added a grow box and handler, but the window doesn't update.
  19.  
  20. //• Other than that it's pretty much the same.
  21.  
  22. //• I don't know what sort of a tangent the shrinks were on that had
  23. //• something to do with the supposed purpose of these graphs, but it
  24. //• has some use as a value graphing thing - if you know what to
  25. //• change and where.  Biorythm?
  26.  
  27. //• Enjoy!
  28.  
  29. //• kenlong@netcom.com
  30.  
  31. #include <stdio.h>
  32. #include <packages.h>
  33. #include <math.h>
  34.  
  35. #define windowID 256
  36. #define lastMenu                3     /* number of menus */
  37. #define appleMenu             1     /* menu ID for desk accessory menu */
  38. #define fileMenu                256     /* menu ID for File menu */
  39. #define editMenu                257     /* menu ID for Edit menu */
  40.  
  41. //#define maxValue 512
  42. #define maxVectSize         8
  43. short scale_V = 1;
  44. #define scale_H 1
  45.  
  46. typedef double matrixType[maxVectSize][maxVectSize];
  47. typedef double vectorType[maxVectSize];
  48. typedef char titletype[21];
  49.  
  50. Boolean quit = false;
  51.  
  52. Rect Trect, dragRect, growIconRect;
  53. EventRecord myEvent;
  54. WindowPtr myWindow, whichWindow;
  55. WindowRecord wRecord;
  56. short code;
  57. MenuHandle myMenus[lastMenu];     /*Handles to all of the menus*/
  58. DialogPtr theDialog;
  59.  
  60. vectorType need, deltaNeed, response, deltaResponse, alphaVector,
  61.             stimulation, consumVector, tempVct1, tempVct2;
  62. matrixType generalization, consumation, excitation, inhibition,
  63.             tempmatrix, ident;
  64. short vectSize, loop, stimuli;
  65. double alpha, personality;
  66. char answer;
  67.  
  68. //• 
  69. void SetForeColor (short red, short green, short blue);
  70. void SetUpMenus (void);
  71. void Startup (void);
  72. void MatrixVector_X (double *a, double (*b)[maxVectSize], double *c, short n, 
  73.     short m);
  74. void AddVectors (double *a, double *b, double *c, short n);
  75. void SubtractVectors (double *a, double *b, double *c, short n);
  76. void ZeroVector (double *a, short n);
  77. void VectorInit (double *a, short n);
  78. void DrawVectors (double *a, double *b, short n, short loop, short offset, 
  79.     short scale_V);
  80. void Vector_X_Scalar (double *a, double s, double *b, short n);
  81. void Matrix_X_Scalar (double (*a)[maxVectSize], double s, 
  82.     double (*b)[maxVectSize], short n);
  83. void Identity (double (*temp)[maxVectSize], short vectSize);
  84. void DynamicSimulation (void);
  85. void Message (short dialogNumber);
  86. void DrawTimeLines (void);
  87. void FakeGrowIcon (WindowPtr wPtr, Boolean visible);
  88. void MyGrowWindow (WindowPtr w, Point p);
  89. short DoCommand (long mResult);
  90. short HandleMouseDown (short windowPart, WindowPtr whichWindow, 
  91.     EventRecord *myEvent);
  92. void WaitForCommand (void);
  93. Boolean ColorCheck (void);
  94. void main (void);
  95. //• 
  96. void SetForeColor(short red, short green, short blue)
  97. {
  98.     RGBColor hue;
  99.  
  100.     hue.red = red;
  101.     hue.green = green;
  102.     hue.blue = blue;
  103.  
  104.     RGBForeColor (&hue);
  105. }
  106.  
  107. void SetUpMenus(void)
  108. {
  109.     short i;
  110.  
  111.     InitMenus ();
  112.     myMenus[0] = GetMenu(appleMenu);
  113.     AddResMenu(myMenus[0], 'DRVR');  /*add desk accessories*/
  114.     myMenus[1] = GetMenu(fileMenu);
  115.     myMenus[2] = GetMenu(editMenu);   /*not really used,  but just for practice*/
  116.     for (i = 0; i <= lastMenu - 1; i++)
  117.         InsertMenu(myMenus[i], 0);
  118.     DrawMenuBar ();
  119. }
  120.  
  121. void Startup(void)
  122. {
  123.     InitGraf(&qd.thePort);        /*start initialization sequence*/
  124.     InitFonts ();
  125.     InitWindows ();
  126.     TEInit ();
  127.     InitDialogs(0L);
  128.     InitCursor ();  /*make cursor an arrow*/
  129.     FlushEvents(everyEvent, 0);
  130.  
  131.     /*Writelns and Readlns text will go to my window*/
  132.  
  133.     /*     WWINIT;*/
  134.     /*initialize the WritelnWindow unit*/
  135.     /*not used anymore*/
  136.     /*with Trect do*/
  137.     /*begin*/
  138.     /*top := 40;*/
  139.     /*right := 480;*/
  140.     /*bottom := 330;*/
  141.     /*left := 40;*/
  142.     /*end;*/
  143.     /*GetFNum('Geneva', familyID);*/
  144.     /*WWNew(Trect, 'Animals', false, true, 80, FamilyID, 9);*/
  145. }
  146.  
  147. void MatrixVector_X(double *a, double (*b)[maxVectSize], double *c,
  148.                 short n, short m)
  149. {
  150.     /*A:= B C                                    A is a m dimensional vector*/
  151.     /*B is a m x n dimensional matrix*/
  152.     /*C is a n dimensional vector*/
  153.     short i, j;
  154.     double aij;
  155.  
  156.     for (i = 0; i <= n - 1; i++)
  157.     {     /*rows*/
  158.         aij = 0.0;
  159.         for (j = 0; j <= m - 1; j++)     /*columns*/
  160.             aij += b[i][j] * c[j];
  161.         a[i] = aij;
  162.     }
  163. }    /*MatrixVector_X*/
  164.  
  165. void AddVectors(double *a, double *b, double *c, short n)
  166. {
  167.     /*A:=B+ C                     A,B,C are n dimensional vectors*/
  168.     short i;
  169.  
  170.     for (i = 0; i <= n - 1; i++)
  171.         a[i] = b[i] + c[i];
  172. }
  173.  
  174. void SubtractVectors(double *a, double *b, double *c, short n)
  175. {
  176.     short i;
  177.  
  178.     for (i = 0; i <= n - 1; i++)
  179.         a[i] = b[i] - c[i];
  180. }
  181.  
  182. void ZeroVector(double *a, short n)
  183. {
  184.     short i;
  185.  
  186.     for (i = 0; i <= n - 1; i++)
  187.     {
  188.         if (a[i] < 0.0)
  189.             a[i] = 0.0;
  190.     }
  191. }
  192.  
  193. void VectorInit(double *a, short n)
  194. {
  195.     short i;
  196.  
  197.     for (i = 0; i <= n - 1; i++)
  198.         a[i] = 0.0;
  199. }
  200.  
  201. void DrawVectors(double *a, double *b, short n, short loop,
  202.                      short offset, short scale_V)
  203. {
  204.     short i, v, dv;
  205.  
  206.     for (i = 1; i <= n; i++)
  207.     {
  208.         if (a[i-1] == 0.0)
  209.         {     /*look out for zeroed out responses*/
  210.             v = offset;
  211.             dv = 0;
  212.         } else
  213.             {
  214.                 v = offset - ceil(a[i-1] * scale_V);
  215.                 dv = - ceil(b[i-1] * scale_V);
  216.         }
  217.         if (i == 1)
  218.             SetForeColor (0, 0, 0xffff);
  219.         if (i == 2)
  220.             SetForeColor (0, 0x8888, 0);
  221.         if (i == 3)
  222.             SetForeColor (0xffff, 0, 0);
  223.  
  224.         /* normal condition*/
  225.         MoveTo(loop * (long) scale_H, v);
  226.  
  227.         /*change pen size to indicate which activity is occuring*/
  228.         PenSize(2, 2);  
  229.         Line((long) scale_H, dv);
  230.     }
  231. }
  232.  
  233. void Vector_X_Scalar(double *a, double s, double *b, short n)
  234. {
  235.     short i;
  236.  
  237.     for (i = 0; i <= n - 1; i++)
  238.         a[i] = s * b[i];
  239. }
  240.  
  241. void Matrix_X_Scalar(double (*a)[maxVectSize], double s,
  242.                 double (*b)[maxVectSize], short n)
  243. {
  244.     short i, j;
  245.  
  246.     for (i = 0; i <= n - 1; i++)
  247.     {
  248.         for (j = 0; j <= n - 1; j++)
  249.             a[i][j] = s * b[i][j];
  250.     }
  251. }
  252.  
  253. void Identity(double (*temp)[maxVectSize], short vectSize)
  254. {
  255.     short i, j;
  256.  
  257.     for (i = 1; i <= vectSize; i++)
  258.     {
  259.         if (i > 1)
  260.     {
  261.             for (j = 0; j <= i - 2; j++)
  262.             {
  263.                 temp[i-1][j] = 0.0;
  264.                 temp[j][i-1] = 0.0;
  265.             }
  266.         }
  267.         temp[i-1][i-1] = 1.0;
  268.     }
  269. }
  270.  
  271. void DynamicSimulation(void)
  272. {
  273.     short stimuli, loop, maxValue, factor_H;
  274.     Rect writeRect;
  275.     long ticks;
  276.     
  277.     maxValue = myWindow->portRect.right / scale_H;
  278.         
  279.     /*write('enter alpha, personality ');*/
  280.     /*readln(alpha, personality);*/
  281.     alpha = 0.2;
  282.     personality = 1.0;
  283.  
  284.     if (answer == '\n')
  285.         answer = ' ';
  286.     
  287.     vectSize = 3;
  288.     Identity(ident, vectSize);
  289.  
  290.         for (stimuli = 1; stimuli <= vectSize; stimuli++)
  291.             stimulation[stimuli-1] = stimuli * 0.5;
  292.  
  293.     Matrix_X_Scalar(consumation, 0.2, ident, vectSize);
  294.         
  295.     Matrix_X_Scalar(excitation, alpha, ident, vectSize);
  296.  
  297.         for (loop = 0; loop <= vectSize - 1; loop++)
  298.         {
  299.             for (stimuli = 0; stimuli <= vectSize - 1; stimuli++)
  300.                 inhibition[loop][stimuli] = 1.0;
  301.             inhibition[loop][loop] = 0.1;
  302.         }
  303.  
  304.     /*zero out initial values of need and response*/
  305.     VectorInit(need, vectSize);
  306.     VectorInit(response, vectSize);
  307.     Matrix_X_Scalar(generalization, personality, ident, vectSize);
  308.     
  309.     SetForeColor (0x9999, 0x9999, 0x9999);
  310.     PenPat (&qd.gray);
  311.     PenSize (1, 1);
  312.     DrawTimeLines ();
  313.     PenNormal ();
  314.     SetForeColor (0, 0, 0);
  315.  
  316.     factor_H = myWindow->portRect.bottom / 10;
  317.     
  318.     MoveTo(10, factor_H);
  319.     DrawString("\pTime -->");
  320.     MoveTo(10, (factor_H * 5));
  321.     DrawString("\pNeeds");
  322.     MoveTo(10, (factor_H * 7));
  323.     DrawString("\pResponses");
  324.  
  325.     /*begin the main loop*/
  326.     for (loop = 1; loop <= maxValue; loop++)
  327.     {
  328.         /*if loop = 250 then*/
  329.         /*        ShowDrawing;*/
  330.         MatrixVector_X(tempVct1, generalization, stimulation, vectSize, vectSize);
  331.         MatrixVector_X(consumVector, consumation, response, vectSize, vectSize);
  332.  
  333.         SubtractVectors(deltaNeed, tempVct1, consumVector, vectSize);
  334.  
  335.         MatrixVector_X(tempVct1, inhibition, response, vectSize, vectSize);
  336.         MatrixVector_X(tempVct2, excitation, need, vectSize, vectSize);
  337.  
  338.         SubtractVectors(deltaResponse, tempVct2, tempVct1, vectSize);
  339.  
  340.         DrawVectors(need, deltaNeed, vectSize, loop, 125, scale_V);
  341.  
  342.         AddVectors(need, need, deltaNeed, vectSize);
  343.  
  344.         DrawVectors(response, deltaResponse, vectSize, loop, 300, scale_V);
  345.  
  346.         AddVectors(response, response, deltaResponse, vectSize);
  347.  
  348.         ZeroVector(response, vectSize);
  349.     }
  350.     PenNormal ();
  351.     SetForeColor (0, 0, 0);
  352.     FakeGrowIcon (myWindow, true);
  353. }
  354.  
  355. /*the remaining code was added to allow for windows, menus, etc*/
  356.  
  357. void Message(short dialogNumber)
  358. {
  359.     DialogPtr theDialog;
  360.     EventRecord myEvent;
  361.     short itemHit;
  362.  
  363.     theDialog = GetNewDialog(dialogNumber, 0L, (WindowPtr)-1L); /*display dialog # */
  364.     ModalDialog(0L, &itemHit); /*show the dialog and wait for a button*/
  365.     DisposDialog(theDialog);  /*get rid of the dialog and show the screen*/
  366.     SetPort(myWindow);        /*put back the original display*/
  367.     DynamicSimulation ();
  368. }
  369.  
  370. void DrawTimeLines ()
  371. {
  372.     short i, amount, start, end;
  373.     
  374.     amount = myWindow->portRect.right;
  375.     start = 20;
  376.     end = myWindow->portRect.bottom;
  377.  
  378.     for (i = 0; i < amount; i++)
  379.     {
  380.         MoveTo (start, 0);
  381.         LineTo (start, end);
  382.         start += 20;
  383.     }
  384. }
  385.  
  386. void FakeGrowIcon (WindowPtr wPtr, Boolean visible)
  387. {
  388.     short bot, rght;
  389.     Rect r;
  390.     PenState pState;
  391.  
  392.     bot = wPtr->portRect.bottom;
  393.     rght = wPtr->portRect.right;
  394.     SetRect (&growIconRect, rght - 15, bot - 15, rght + 1, bot + 1);
  395.     if (visible)
  396.     {
  397.         GetPenState (&pState);
  398.         EraseRect (&growIconRect);
  399.         FrameRect (&growIconRect);
  400.         SetRect (&r, rght - 10, bot - 10, rght - 1, bot - 1);
  401.         FrameRect (&r);
  402.         SetRect (&r, rght - 12, bot - 12, rght - 5, bot - 5);
  403.         EraseRect (&r);
  404.         FrameRect (&r);
  405.         SetPenState (&pState);
  406.     }
  407. }
  408.  
  409. void MyGrowWindow (WindowPtr w, Point p)
  410. {
  411.     GrafPtr savePort;
  412.     long theResult;
  413.     Rect oldHorizBar;
  414.     Rect r;
  415.  
  416.     GetPort (&savePort);
  417.     SetPort (w);
  418.  
  419.     SetRect (&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
  420.     theResult = GrowWindow (w, p, &r);
  421.     if (theResult == 0)
  422.         return;
  423.     SizeWindow (w, LoWord (theResult), HiWord (theResult), false);
  424.  
  425.     InvalRect (&w->portRect);
  426.  
  427.     ValidRect (&r);
  428.  
  429.     SetPort (savePort);
  430. }
  431.  
  432. short DoCommand (long mResult)
  433. {
  434.     Str255 name;
  435.     short theMenu, theItem, refNum;
  436.     short AlertId = 1;
  437.  
  438.     theMenu = HiWord(mResult);
  439.     theItem = LoWord(mResult);
  440.     switch (theMenu)
  441.     {
  442.  
  443.         case appleMenu:
  444.             if (theItem == 1)     /*throw up message window*/
  445.             {
  446.                 Message(1);
  447.                 quit = false;
  448.             }
  449.             else
  450.                 {
  451.                     GetMenuItemText(myMenus[0], theItem, name);
  452.                     refNum = OpenDeskAcc(name);
  453.             }
  454.         break;
  455.             
  456.         case fileMenu:
  457.             switch (theItem)
  458.             {
  459.     
  460.                 case 1:
  461.                     EraseRect (&myWindow->portRect);
  462.                     DynamicSimulation ();
  463.                 break;
  464.                 
  465.                 case 2:
  466.                 break;
  467.                 
  468.                 case 3:
  469.                 break;
  470.     
  471.                 /* AlertNum:=CautionAlert(AlertId,nil);*/
  472.     
  473.                 case 5:
  474.                     quit = true;
  475.                 break;
  476.             }
  477.             break;
  478.     
  479.             case editMenu:
  480.                 if (!SystemEdit(theItem-1))
  481.                     SysBeep(5);
  482.             break;
  483.     }
  484.     HiliteMenu (0);
  485.     return (1);
  486. }
  487.  
  488. short HandleMouseDown (short windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  489. {
  490.     switch (windowPart)
  491.     {
  492.         case inGoAway:
  493.             CloseWindow (whichWindow);
  494.         break;
  495.  
  496.         case inMenuBar:
  497.             DoCommand (MenuSelect (myEvent->where));
  498.         break;
  499.         
  500.         case inSysWindow:
  501.             SystemClick (myEvent, whichWindow);
  502.         break;
  503.  
  504.         case inDrag:
  505.         break;
  506.  
  507.         case inGrow:
  508.             MyGrowWindow (whichWindow, myEvent->where);
  509.         break;
  510.  
  511.         case inContent:
  512.             if (whichWindow != FrontWindow ())
  513.                 SelectWindow (whichWindow);
  514.         break;
  515.     }
  516. }
  517.  
  518. void WaitForCommand(void)
  519. {
  520.     GrafPtr savePort;
  521.     EventRecord myEvent;
  522.     WindowPtr whichWindow;
  523.     short windowPart;
  524.     Rect r;
  525.  
  526.     SystemTask ();
  527.  
  528.     if (GetNextEvent (everyEvent, &myEvent))
  529.     {
  530.         switch (myEvent.what)
  531.         {
  532.             case mouseDown:
  533.                 windowPart = FindWindow (myEvent.where, &whichWindow);
  534.                 HandleMouseDown (windowPart, whichWindow, &myEvent);
  535.             break;
  536.  
  537.             case keyDown:
  538.             case autoKey:
  539.                 DoCommand(MenuKey((char) (myEvent.message & charCodeMask)));
  540.             break;
  541.         }
  542.     }
  543. }
  544.  
  545. Boolean ColorCheck (void)
  546. {
  547.     SysEnvRec theWorld;
  548.     Boolean gotColor = false;
  549.  
  550.     if (theWorld.hasColorQD)
  551.         gotColor = true;
  552.  
  553.     return true;
  554. }
  555.  
  556. void main()
  557. {
  558.     Startup();
  559.     SetUpMenus ();
  560.  
  561.     if (ColorCheck ())
  562.     {
  563.         SetPort ((myWindow = GetNewCWindow (windowID, &wRecord, (WindowPtr)-1L)));
  564.         FakeGrowIcon (myWindow, true);
  565.         while (! quit)
  566.             WaitForCommand();     /*the Main Event Loop*/
  567.     }
  568.     DisposeWindow (myWindow);
  569. }
  570.  
  571.