home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / misc / math / fastplot / source / testplots.mod < prev   
Encoding:
Text File  |  1995-03-27  |  12.5 KB  |  522 lines

  1. (*********************************************************************)
  2. (*                                                                   *)
  3. (* Module TestPlots Copyright © 1994 by Computer Inspirations        *)
  4. (*                                                                   *)
  5. (*********************************************************************)
  6.  
  7. MODULE TestPlots;
  8.  
  9.  
  10. (* Amiga-specific operating system module *)
  11. FROM DOSProcess    IMPORT Delay;
  12.  
  13. (* General Modula-2 modules *)
  14. FROM MathLib0      IMPORT exp, sin, pi, cos, log;
  15. FROM RandomNumbers IMPORT Random;
  16.  
  17. (* Custom module developed to support PlotLibrary *)
  18. FROM PlotLibrary   IMPORT PlotType, PlotKindType, LineType,
  19.                           PositionType, InitPlot,
  20.                           PlotFx, LabelMinMax, DonePlot,
  21.                           InformationBox, SetPlotColour,
  22.                           SetColourMap, SetPlotOffset,
  23.                           SetLabelRoutine, SetTextColour,
  24.                           CenterLabelX, CenterLabelY,
  25.                           SetPlotLimits, ClearPlot,
  26.                           SetScatterPlot, SetZeroLine,
  27.                           SetPlotScale, InitOffsetPlot;
  28.  
  29.  
  30. CONST
  31.   LGrey   = 0;
  32.   Black   = 1;
  33.   White   = 2;
  34.   Blue    = 3;
  35.   Green   = 4;
  36.   Red     = 5;
  37.   Pink    = 6;
  38.   Grey    = 7;
  39.   Colours = 8;
  40.   Time    = 500;   (* 10 seconds *)
  41.  
  42. VAR
  43.   Plot  : PlotType;                (* plot variable *)
  44.   Title : ARRAY [0..80] OF CHAR;   (* screen title  *)
  45.  
  46.  
  47. PROCEDURE DefineColours;
  48. BEGIN
  49.   SetColourMap(Plot, LGrey, 10, 10, 10);
  50.   SetColourMap(Plot, Black,  0,  0,  0);
  51.   SetColourMap(Plot, White, 15, 15, 15);
  52.   SetColourMap(Plot, Blue,   0,  0, 15);
  53.   SetColourMap(Plot, Green,  0, 15,  0);
  54.   SetColourMap(Plot, Red,   15,  0,  0);
  55.   SetColourMap(Plot, Pink,  15,  3,  7);
  56.   SetColourMap(Plot, Grey,   8,  8,  8);
  57. END DefineColours;
  58.  
  59.  
  60. PROCEDURE DampSine(x : REAL) : REAL;
  61. VAR
  62.   xpi : REAL;
  63. BEGIN
  64.   xpi := x * pi;
  65.   RETURN exp(-xpi/10.0) * sin(xpi);
  66. END DampSine;
  67.  
  68.  
  69. PROCEDURE Cubic(x : REAL) : REAL;
  70. BEGIN
  71.   RETURN ((2.0 * x - 7.0) * x - 7.0) * x + 5.0;
  72. END Cubic;
  73.  
  74.  
  75. PROCEDURE RandomSine(x : REAL) : REAL;
  76. VAR
  77.   xpi, y : REAL;
  78. BEGIN
  79.   xpi := x * pi;
  80.   y := sin(xpi) / 1000.0;
  81.   SetPlotColour(Plot, Random(5) + 1);
  82.   RETURN FLOAT(Random(1000)) * y;
  83. END RandomSine;
  84.  
  85.  
  86. PROCEDURE AmpModulated(x : REAL) : REAL;
  87. VAR
  88.   xpi : REAL;
  89. BEGIN
  90.   xpi := x * pi;
  91.   RETURN sin(xpi) * sin(10.0 * xpi);
  92. END AmpModulated;
  93.  
  94.  
  95. PROCEDURE Response(x : REAL) : REAL;
  96. VAR
  97.   xi, y : REAL;
  98. BEGIN
  99.   xi := 2.0 * pi * (log(x) - 1.0) / 8.0;
  100.   y  := 30.0 * sin(xi);
  101.   RETURN y * y;
  102. END Response;
  103.  
  104.  
  105. PROCEDURE StockPrices(x : REAL) : REAL;
  106. BEGIN
  107.   RETURN 0.3 * (10.0 - x) * (FLOAT(Random(10)) - 5.0);
  108. END StockPrices;
  109.  
  110.  
  111. PROCEDURE RawProfit(x : REAL) : REAL;
  112. VAR
  113.   Gx : INTEGER;
  114. BEGIN
  115.   Gx := TRUNC(x);
  116.   CASE Gx OF
  117.     1  : RETURN 5.3   |
  118.     2  : RETURN 7.5   |
  119.     3  : RETURN 3.6   |
  120.     4  : RETURN -1.5  |
  121.     5  : RETURN -6.3  |
  122.     6  : RETURN 2.4   |
  123.     7  : RETURN 3.8   |
  124.     8  : RETURN 4.8   |
  125.     9  : RETURN 3.9   |
  126.     10 : RETURN 3.3   |
  127.     11 : RETURN -3.6  |
  128.     12 : RETURN 4.8   |
  129.     ELSE RETURN 0.0;
  130.   END;
  131. END RawProfit;
  132.  
  133.  
  134. PROCEDURE ProfitValues2(x : REAL) : REAL;
  135. VAR
  136.   Gx : INTEGER;
  137.   y  : REAL;
  138. BEGIN
  139.   Gx := TRUNC(x);
  140.   CASE Gx OF
  141.     1  : y := 6.2   |
  142.     2  : y := 8.0   |
  143.     3  : y := 4.3   |
  144.     4  : y := -2.1  |
  145.     5  : y := -8.0  |
  146.     6  : y := 3.1   |
  147.     7  : y := 4.3   |
  148.     8  : y := 5.2   |
  149.     9  : y := 4.2   |
  150.     10 : y := 5.6   |
  151.     11 : y := -3.9  |
  152.     12 : y := 6.0   |
  153.     ELSE y := 0.0;
  154.   END;
  155.   IF y > 0.0 THEN
  156.     SetPlotColour(Plot, Grey);  (* positive y's *)
  157.   ELSE
  158.     SetPlotColour(Plot, Pink);  (* negative y's *)
  159.   END;
  160.   RETURN y;
  161. END ProfitValues2;
  162.  
  163.  
  164. PROCEDURE ProfitValues(x : REAL) : REAL;
  165. VAR
  166.   y  : REAL;
  167. BEGIN
  168.   y := RawProfit(x);
  169.   IF y > 0.0 THEN
  170.     SetPlotColour(Plot, Black);  (* positive y's *)
  171.   ELSE
  172.     SetPlotColour(Plot, Red);    (* negative y's *)
  173.   END;
  174.   RETURN y;
  175. END ProfitValues;
  176.  
  177.  
  178. PROCEDURE BarLabels(index   : INTEGER;         (* x div *)
  179.                     VAR str : ARRAY OF CHAR);  (* label *)
  180. BEGIN
  181.   CASE index OF
  182.     1  : str := "Jan" |
  183.     2  : str := "Feb" |
  184.     3  : str := "Mar" |
  185.     4  : str := "Apr" |
  186.     5  : str := "May" |
  187.     6  : str := "Jun" |
  188.     7  : str := "Jul" |
  189.     8  : str := "Aug" |
  190.     9  : str := "Sep" |
  191.     10 : str := "Oct" |
  192.     11 : str := "Nov" |
  193.     12 : str := "Dec" |
  194.     ELSE str := "";
  195.   END;
  196. END BarLabels;
  197.  
  198.  
  199. PROCEDURE NoLabels(index   : INTEGER;         (* x div *)
  200.                    VAR str : ARRAY OF CHAR);  (* label *)
  201. BEGIN
  202.   str := "";
  203. END NoLabels;
  204.  
  205.  
  206. PROCEDURE LinePlot;
  207. VAR
  208.   LineArray : ARRAY [1..3] OF LineType;
  209.   Plot2 : PlotType;
  210. BEGIN
  211.   Title := "Line-Style Plot";
  212.   IF InitPlot(Plot, Title, Line, 0.0, 13.0, -10.0, 10.0,
  213.               640, 400, 13, 10, 5, 5, 0, 0, Colours) THEN
  214.     (* define the colours *)
  215.     DefineColours;
  216.  
  217.     (* draw the plot 1 *)
  218.     SetLabelRoutine(Plot, BarLabels);
  219.     PlotFx(Plot, StockPrices);
  220.  
  221.     (* label the axis *)
  222.     SetTextColour(Plot, Blue);
  223.     CenterLabelX(Plot, "Month", 395);
  224.     CenterLabelY(Plot, "Profit (Thousands $)", 15);
  225.  
  226.     (* draw the information box *)
  227.     LineArray[1] := "Albrania Stock Market";
  228.     LineArray[2] := "";
  229.     LineArray[3] := "January 1892 - December 1892";
  230.     SetTextColour(Plot, White);
  231.     InformationBox(Plot,UpperRight,LineArray,Black,Blue);
  232.  
  233.     (* delay and exit *)
  234.     Delay(Time);
  235.     DonePlot(Plot);
  236.   END;
  237. END LinePlot;
  238.  
  239.  
  240. PROCEDURE ScientificPlot;
  241. VAR
  242.   LineArray : ARRAY [1..6] OF LineType;
  243. BEGIN
  244.   Title := "Normal Plot";
  245.   IF InitPlot(Plot, Title, Normal, 0.0, 7.0, -1.0, 1.0,
  246.               640, 400, 7, 10, 5, 5, 0, 1, Colours) THEN
  247.     (* define the colours *)
  248.     DefineColours;
  249.  
  250.     (* draw the plot *)
  251.     PlotFx(Plot, DampSine);
  252.  
  253.     (* label minima and maxima *)
  254.     LabelMinMax(Plot, DampSine);
  255.  
  256.     (* draw the information box *)
  257.     LineArray[1] := "Dampened sine wave plot";
  258.     LineArray[2] := "";
  259.     LineArray[3] := "y=exp(-x·pi÷10)·sin(x·pi)";
  260.     LineArray[4] := "";
  261.     LineArray[5] := "Procedures imported from";
  262.     LineArray[6] := "the PlotLibrary module";
  263.     SetTextColour(Plot, White);
  264.     InformationBox(Plot,LowerRight,LineArray,Black,Blue);
  265.  
  266.     (* delay and exit *)
  267.     Delay(Time);
  268.     DonePlot(Plot);
  269.   END;
  270. END ScientificPlot;
  271.  
  272.  
  273. PROCEDURE ScaledPlot;
  274. VAR
  275.   LineArray : ARRAY [1..8] OF LineType;
  276. BEGIN
  277.   Title := "Scaled Plot";
  278.   IF InitPlot(Plot, Title, Normal, -6.0, 11.0,
  279.               -100.0, 100.0, 640, 400, 7, 10, 5, 5, 1, 0,
  280.               Colours) THEN
  281.     (* define the colours *)
  282.     DefineColours;
  283.  
  284.     (* draw the plot *)
  285.     PlotFx(Plot, Cubic);
  286.  
  287.     (* 2 x magnification on y-axis *)
  288.     SetPlotScale(Plot, 1.0, 2.0);
  289.     SetPlotColour(Plot, Red);
  290.     PlotFx(Plot, Cubic);
  291.  
  292.     (* 2 x magnification on x-axis *)
  293.     SetPlotScale(Plot, 2.0, 1.0);
  294.     SetPlotColour(Plot, Blue);
  295.     PlotFx(Plot, Cubic);
  296.  
  297.     (* 2 x magnification on both axes *)
  298.     SetPlotScale(Plot, 2.0, 2.0);
  299.     SetPlotColour(Plot, Green);
  300.     PlotFx(Plot, Cubic);
  301.  
  302.     (* draw the information box *)
  303.     LineArray[1] := "Cubic Function plot";
  304.     LineArray[2] := "";
  305.     LineArray[3] := "y = 2x³ - 7x² - 7x + 5";
  306.     LineArray[4] := "";
  307.     LineArray[5] := " Black - Unscaled";
  308.     LineArray[6] := " Red   - 2 x Y Mag";
  309.     LineArray[7] := " Blue  - 2 x X Mag";
  310.     LineArray[8] := " Green - 2 x X & Y Mag";
  311.     SetTextColour(Plot, White);
  312.     InformationBox(Plot,UpperLeft,LineArray,Black,Blue);
  313.  
  314.     (* delay and exit *)
  315.     Delay(Time);
  316.     DonePlot(Plot);
  317.   END;
  318. END ScaledPlot;
  319.  
  320.  
  321. PROCEDURE QuadPlot;
  322. VAR
  323.   LineArray : ARRAY [1..9] OF LineType;
  324.   OK        : BOOLEAN;
  325.   Cnt       : INTEGER;
  326.   Plots     : ARRAY [1..4] OF PlotType;
  327. BEGIN
  328.   (* create the initial plot canvas *)
  329.   Title := "Four Plots on a Screen";
  330.   IF InitPlot(Plot, Title, Normal, 0.0, 7.0, -1.0, 1.0,
  331.               640, 400, 7, 10, 5, 5, 0, 1, Colours) THEN
  332.     (* define the colours *)
  333.     DefineColours;
  334.  
  335.     (* set up the first plot in upper left of screen *)
  336.     OK := InitOffsetPlot(Plots[1], Normal, 0.0, 7.0,
  337.                          -1.0, 1.0, 320, 200, 3, 5, 0, 0,
  338.                          1, 1, 0, 0, Plot);
  339.     PlotFx(Plots[1], AmpModulated);
  340.  
  341.     (* set up the second plot below the first *)
  342.     OK := InitOffsetPlot(Plots[2], Normal, 0.0, 7.0,
  343.                          -1.0, 1.0, 320, 200, 3, 5, 0, 0,
  344.                          1, 1, 0, 200, Plot);
  345.     SetPlotScale(Plots[2], 6.0, 1.0);
  346.     SetPlotColour(Plots[2], Red);
  347.     PlotFx(Plots[2], AmpModulated);
  348.  
  349.     (* set up the third plot at the top right *)
  350.     OK := InitOffsetPlot(Plots[3], Normal, -6.0, 11.0,
  351.                          -100.0, 100.0, 320, 200,
  352.                          3, 5, 0, 0, 1, 0, 320, 0, Plot);
  353.     PlotFx(Plots[3], Cubic);
  354.  
  355.     (* set up the final plot at the bottom right *)
  356.     OK := InitOffsetPlot(Plots[4], Normal, -6.0, 11.0,
  357.                          -100.0, 100.0, 320, 200,
  358.                          3, 5, 0, 0, 1, 0, 320, 200, Plot);
  359.     SetPlotScale(Plots[4], 2.0, 2.0);
  360.     SetPlotColour(Plots[4], Red);
  361.     PlotFx(Plots[4], Cubic);
  362.  
  363.     (* delay and exit *)
  364.     Delay(Time);
  365.     FOR Cnt := 1 TO 4 DO
  366.       DonePlot(Plots[Cnt]);
  367.     END;
  368.     DonePlot(Plot);
  369.   END;
  370. END QuadPlot;
  371.  
  372.  
  373. PROCEDURE ShadowPlot;
  374. VAR
  375.   LineArray : ARRAY [1..6] OF LineType;
  376.   Cnt : CARDINAL;
  377. BEGIN
  378.   Title := "Shadow Plot";
  379.   IF InitPlot(Plot, Title, Normal, 0.0, 7.0, -1.0, 1.0,
  380.               640, 400, 7, 10, 5, 5, 0, 1, Colours) THEN
  381.     (* define the colours *)
  382.     DefineColours;
  383.  
  384.     (* draw a thick shadow plot *)
  385.     SetPlotColour(Plot, Black);
  386.     SetZeroLine(Plot, FALSE);       (* no zero line *)
  387.     FOR Cnt := 0 TO 2 DO
  388.       SetPlotOffset(Plot, Cnt, -1);
  389.       PlotFx(Plot, AmpModulated);
  390.     END;
  391.  
  392.     (* draw the plot *)
  393.     SetPlotOffset(Plot, 0, 0);
  394.     SetZeroLine(Plot, TRUE);        (* zero is drawn *)
  395.     SetPlotColour(Plot, Red);
  396.     PlotFx(Plot, AmpModulated);
  397.  
  398.     (* label the axis *)
  399.     SetTextColour(Plot, Blue);
  400.     CenterLabelX(Plot, "Modulation Time", 395);
  401.     CenterLabelY(Plot, "Modulated Amplitude", 15);
  402.  
  403.     (* delay and exit *)
  404.     Delay(Time);
  405.     DonePlot(Plot);
  406.   END;
  407. END ShadowPlot;
  408.  
  409.  
  410. PROCEDURE ResponsePlot;
  411. VAR
  412.   LineArray : ARRAY [1..1] OF LineType;
  413. BEGIN
  414.   Title := "Response Plot";
  415.   IF InitPlot(Plot, Title, Log, 10.0, 100000.0, 0.1, 1000.0,
  416.               640, 400, 4, 4, 10, 10, 0, 1, Colours) THEN
  417.     (* define the colours *)
  418.     DefineColours;
  419.  
  420.     (* draw the plot *)
  421.     PlotFx(Plot, Response);
  422.  
  423.     (* label the axis *)
  424.     SetTextColour(Plot, Blue);
  425.     CenterLabelX(Plot, "Frequency (Hz)", 395);
  426.     CenterLabelY(Plot, "Power Output (W)", 15);
  427.  
  428.     (* draw the information box *)
  429.     LineArray[1] := "Stereo Response Plot";
  430.     SetTextColour(Plot, White);
  431.     InformationBox(Plot,LowerMiddle,LineArray,Black,Blue);
  432.  
  433.     (* delay and exit *)
  434.     Delay(Time);
  435.     DonePlot(Plot);
  436.   END;
  437. END ResponsePlot;
  438.  
  439.  
  440. PROCEDURE BarChart;
  441. VAR
  442.   LineArray : ARRAY [1..7] OF LineType;
  443. BEGIN
  444.   Title := "Bar Chart Plot";
  445.   IF InitPlot(Plot, Title, Bar, 0.0, 13.0, -10.0, 10.0,
  446.               640, 400, 13, 10, 0, 0, 0, 0, Colours) THEN
  447.     (* define the colours *)
  448.     DefineColours;
  449.  
  450.     (* draw the plot 1 *)
  451.     SetLabelRoutine(Plot, BarLabels);
  452.     SetPlotOffset(Plot, -4, 0);
  453.     PlotFx(Plot, ProfitValues);
  454.  
  455.     (* draw the plot 2 *)
  456.     SetPlotOffset(Plot, 4, 0);
  457.     PlotFx(Plot, ProfitValues2);
  458.  
  459.     (* label minima and maxima *)
  460.     LabelMinMax(Plot, ProfitValues2);
  461.  
  462.     (* label the axis *)
  463.     SetTextColour(Plot, Blue);
  464.     CenterLabelX(Plot, "Month", 395);
  465.     CenterLabelY(Plot, "Profit (Thousands $)", 15);
  466.  
  467.     (* draw the information box *)
  468.     LineArray[1] := "XYZ Corporation";
  469.     LineArray[2] := "Monthly Profit Bar Graph";
  470.     LineArray[3] := "January 1991 - December 1992";
  471.     LineArray[4] := "   Black = Profit 1991";
  472.     LineArray[5] := "   Grey  = Profit 1992";
  473.     LineArray[6] := "   Red   = Loss 1991";
  474.     LineArray[7] := "   Pink  = Loss 1992";
  475.     SetTextColour(Plot, White);
  476.     InformationBox(Plot,LowerRight,LineArray,Black,Blue);
  477.  
  478.     (* delay and exit *)
  479.     Delay(Time);
  480.     DonePlot(Plot);
  481.   END;
  482. END BarChart;
  483.  
  484.  
  485. PROCEDURE ScatterPlot;
  486. VAR
  487.   LineArray : ARRAY [1..7] OF LineType;
  488. BEGIN
  489.   Title := "Scatter Plot";
  490.   IF InitPlot(Plot, Title, Normal, 0.0, 7.0, -1.0, 1.0,
  491.               640, 400, 7, 10, 0, 0, 0, 1, Colours) THEN
  492.     (* define the colours *)
  493.     DefineColours;
  494.  
  495.     (* set to a scatter plot *)
  496.     SetScatterPlot(Plot);
  497.  
  498.     (* draw the plot *)
  499.     PlotFx(Plot, RandomSine);
  500.  
  501.     (* label the axis *)
  502.     SetTextColour(Plot, Blue);
  503.     CenterLabelX(Plot, "Random Sinusoid", 395);
  504.     CenterLabelY(Plot, "Amplitude", 15);
  505.  
  506.     (* delay and exit *)
  507.     Delay(Time);
  508.     DonePlot(Plot);
  509.   END;
  510. END ScatterPlot;
  511.  
  512.  
  513. BEGIN
  514.   ScientificPlot;
  515.   ResponsePlot;
  516.   LinePlot;
  517.   BarChart;
  518.   ScatterPlot;
  519.   ShadowPlot;
  520.   ScaledPlot;
  521.   QuadPlot;
  522. END TestPlots.