home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / OTL-MC7.DMS / in.adf / cppdemo.lha / C++ / FPlot / plot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-29  |  2.3 KB  |  113 lines

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