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

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