home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / pmattrac / duff.c < prev    next >
C/C++ Source or Header  |  1990-10-14  |  4KB  |  152 lines

  1. #include "common.h"
  2.  
  3. /*--------------------------------------------------------------------*
  4.  *  Process the Duffing dialog proc.                      *
  5.  *--------------------------------------------------------------------*/
  6. MRESULT EXPENTRY
  7. DuffingDlgProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  8. {
  9.     char a[10], b[10], c[10], F[10];
  10.     static double aval = 1.0, bval = 0.3, cval = 0.0, Fval = 10.0;
  11.  
  12.     switch (msg) {
  13.       case WM_INITDLG:
  14.     gcvt(aval, 5, a);
  15.     gcvt(bval, 5, b);
  16.     gcvt(cval, 5, c);
  17.     gcvt(Fval, 5, F);
  18.  
  19.     WinSetDlgItemText(hwnd, DID_DUFF_a, a);
  20.     WinSetDlgItemText(hwnd, DID_DUFF_b, b);
  21.     WinSetDlgItemText(hwnd, DID_DUFF_c, c);
  22.     WinSetDlgItemText(hwnd, DID_DUFF_F, F);
  23.  
  24.     WinSendDlgItemMsg(hwnd, DID_DUFF_a, EM_SETTEXTLIMIT,
  25.         MPFROM2SHORT(9, 0), NULL);
  26.     WinSendDlgItemMsg(hwnd, DID_DUFF_b, EM_SETTEXTLIMIT,
  27.         MPFROM2SHORT(9, 0), NULL);
  28.     WinSendDlgItemMsg(hwnd, DID_DUFF_c, EM_SETTEXTLIMIT,
  29.         MPFROM2SHORT(9, 0), NULL);
  30.     WinSendDlgItemMsg(hwnd, DID_DUFF_F, EM_SETTEXTLIMIT,
  31.         MPFROM2SHORT(9, 0), NULL);
  32.  
  33.       case WM_COMMAND:
  34.     switch (COMMANDMSG(&msg)->cmd) {
  35.       case DID_CANCEL:
  36.         WinDismissDlg(hwnd, TRUE);
  37.         return 0;
  38.       case DID_OK:
  39.         WinQueryDlgItemText(hwnd, DID_DUFF_a, sizeof(a), a);
  40.         WinQueryDlgItemText(hwnd, DID_DUFF_b, sizeof(b), b);
  41.         WinQueryDlgItemText(hwnd, DID_DUFF_c, sizeof(c), c);
  42.         WinQueryDlgItemText(hwnd, DID_DUFF_F, sizeof(F), F);
  43.  
  44.         tp.pvalue[0].d = aval = atof(a);
  45.         tp.pvalue[1].d = bval = atof(b);
  46.         tp.pvalue[2].d = cval = atof(c);
  47.         tp.pvalue[3].d = Fval = atof(F);
  48.         tp.exitflag = 0;
  49.  
  50.  
  51.         DosSemSet(&tp.triggerdraw);
  52.         DosSemClear(&tp.doingdraw);
  53.  
  54.         if (_beginthread(draw_duffing, threadstack,
  55.             STACKSIZE * sizeof(int), &tp) == -1) {
  56.         WinMessageBox(HWND_DESKTOP, hwnd,
  57.             "Cannot create second thread!", "Thread2",
  58.             0, MB_OK | MB_ICONEXCLAMATION);
  59.         return 0;
  60.         }
  61.         WinDismissDlg(hwnd, TRUE);
  62.         setrunning();
  63.         DosSemClear(&tp.triggerdraw);
  64.         return 0;
  65.     }
  66.     break;
  67.     }
  68.     return WinDefDlgProc(hwnd, msg, mp1, mp2);
  69. }
  70. /*--------------------------------------------------------------------*
  71.  *  Draw the Duffing Oscillator                       *
  72.  *--------------------------------------------------------------------*/
  73. void far
  74. draw_duffing(threadparms * tp)
  75. {
  76.     HAB hab;
  77.  
  78.     POINTL ptl;
  79.  
  80.     double x, y, z, nx, ny, nz, t;
  81.     double a, b, c, F;
  82.     register int i;
  83.  
  84.     MATRIXLF transform;
  85.  
  86.  
  87.     DosSemWait(&tp->triggerdraw, SEM_INDEFINITE_WAIT);
  88.     DosSemSet(&tp->doingdraw);
  89.  
  90.     setfloatscale(&transform, -10., -10., 10., 10.,
  91.     2000, 2000, tp->xmax, tp->ymax);
  92.  
  93.  
  94.     a = tp->pvalue[0].d;
  95.     b = tp->pvalue[1].d;
  96.     c = tp->pvalue[2].d;
  97.     F = tp->pvalue[3].d;
  98.  
  99.     hab = WinInitialize(0);
  100.  
  101.     GpiSetDefaultViewMatrix(tp->memhps, 9L, &transform,
  102.     TRANSFORM_REPLACE);
  103.     GpiErase(tp->memhps);
  104.     GpiSetColor(tp->memhps, CLR_RED);
  105.  
  106.     ptl.x = 0;
  107.     ptl.y = -10 * 2000;
  108.     GpiMove(tp->memhps, &ptl);
  109.  
  110.     ptl.x = 0;
  111.     ptl.y = 10 * 2000;
  112.     GpiLine(tp->memhps, &ptl);
  113.  
  114.     ptl.x = -10 * 2000;
  115.     ptl.y = 0;
  116.     GpiMove(tp->memhps, &ptl);
  117.  
  118.     ptl.x = 10 * 2000;
  119.     ptl.y = 0;
  120.     GpiLine(tp->memhps, &ptl);
  121.  
  122.     GpiSetColor(tp->memhps, CLR_DARKBLUE);
  123.  
  124.     x = -1.;
  125.     y = 1.;
  126.     z = 0;
  127.     t = .004;
  128.     for (i = 0; i < 10000; i++) {
  129.     if (tp->exitflag)
  130.         break;
  131.     nx = x + t * y;
  132.     ny = y + t * ( -(a*x*x*x + c*x + b*x) + F * cos(z));
  133.     nz = z + t;
  134.     x = nx;
  135.     y = ny;
  136.     z = nz;
  137.     ptl.x = (x * 2000);
  138.     ptl.y = (y * 2000);
  139.     GpiSetPel(tp->memhps, &ptl);
  140.     if( i % 100 == 0)
  141.         refresh(tp->hps, tp->memhps, tp->cxmax, tp->cymax, tp->xmax, tp->ymax);
  142.     }
  143.  
  144.     DosSemSet(&tp->triggerdraw);
  145.     DosSemClear(&tp->doingdraw);
  146.  
  147.     DosBeep(440, 200);
  148.     WinPostMsg(hwndClient, WM_USER + 1, NULL, NULL);
  149.     WinTerminate(hab);
  150.     _endthread();
  151. }
  152.