home *** CD-ROM | disk | FTP | other *** search
/ The AGA Experience 2 / agavol2.iso / software / utilities / demos / stormc-demo / examples / fplot / plot.cc < prev    next >
C/C++ Source or Header  |  1996-01-18  |  2KB  |  111 lines

  1. /*
  2.  
  3.   Funktionsplotter in Storm C
  4.  
  5. */
  6.  
  7. #include <exec/types.h>
  8. #include <intuition/intuition.h>
  9. #include <functions.h>
  10.  
  11. #include <stream.h>
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <stdlib.h>
  15. #include <ctype.h>
  16.  
  17. #include "fclass.h"
  18.  
  19. // ******** Zeichenroutine ********
  20.  
  21. // Windowdimensionen:
  22. #define WW 640
  23. #define WH 190
  24.  
  25. #pragma break -
  26.  
  27. void plot(Func *f, double minx, double maxx, double miny, double maxy)
  28. {
  29.   NewWindow mynewwin =
  30.    { 0, 0, WW, WH+10, 2, 1, CLOSEWINDOW,
  31.      ACTIVATE | WINDOWSIZING | WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE | GIMMEZEROZERO,
  32.      NULL, NULL, " Plot ++ ", NULL, NULL, 100, 50, 640, 200, WBENCHSCREEN};
  33.  
  34.   struct Window* mywin = OpenWindow(&mynewwin);
  35.   if (!mywin) exit(1);
  36.  
  37.   RastPort *RP = mywin->RPort;
  38.  
  39.   double XS = WW / (maxx-minx), YS = WH / (maxy-miny);
  40.  
  41.   int X0 = int(-XS*minx), Y0 = int(-YS*miny);
  42.  
  43.   SetAPen(mywin->RPort, 3);
  44.   if (miny < 0 && maxy > 0)
  45.     { // y-Achse zeichnen:
  46.       Move(RP,  0, WH-Y0);
  47.       Draw(RP, WW, WH-Y0);
  48.     }
  49.  
  50.   if (minx < 0 && maxx > 0)
  51.     { // x-Achse zeichnen:
  52.       Move(RP, X0, 0);
  53.       Draw(RP, X0, WH);
  54.     }
  55.  
  56.   SetAPen(mywin->RPort, 0);
  57.   Move(mywin->RPort, 0, 0);
  58.  
  59.   for (int i=0; i<WW; i++)
  60.     { double x = minx+i/XS, y = f->eval(x);
  61.       int yp = int(YS*y+Y0);
  62.  
  63.       if (yp > -WH && yp < 2*WH) Draw(RP, i, WH-yp);
  64.       SetAPen(RP, 1);
  65.     }
  66.  
  67.   WaitPort(mywin->UserPort);
  68.   CloseWindow(mywin);
  69. }
  70.  
  71. #pragma break +
  72.  
  73. void main()
  74. {
  75.   char input[100], buf[200];
  76.  
  77.   cout << "\nPlot ++\nGeschrieben von Jochen Becher Storm C\n\n";
  78.  
  79.   cout << "Funktion: f(x) = "; cin.getline(input, 100);
  80.  
  81.   Func *MyExp = Parse(input);
  82.  
  83.   if (MyExp)
  84.     { double minX, maxX, minY, maxY;
  85.  
  86.       for(;;)
  87.        { cout << "X-Bereich: von "; cin >> minX;
  88.          cout << "           bis "; cin >> maxX;
  89.          if (minX < maxX) break;
  90.          cout << "Das Minimum muß kleiner als das Maximum sein.\n";
  91.        }
  92.  
  93.       for(;;)
  94.        { cout << "Y-Bereich: von "; cin >> minY;
  95.          cout << "           bis "; cin >> maxY;
  96.          if (minY < maxY) break;
  97.          cout << "Das Minimum muß kleiner als das Maximum sein.\n";
  98.        }
  99.  
  100.       cout << "\nFunktion: "; MyExp->print(buf); cout << "\n";
  101.  
  102.       plot(MyExp, minX, maxX, minY, maxY);
  103.  
  104.       delete MyExp;
  105.     }
  106.   else
  107.     cout << "Versuch's doch gleich noch mal!\n";
  108.  
  109. }
  110.  
  111.