%% sinewaves1.lmc
%% Created by Laurence D. Finston (LDF) Do 10. Aug 19:31:43 CEST 2023

%% * (1) Copyright and License.

%%%% Copyright (C) 2023 Laurence Finston

%%%% This file is free software; you can redistribute it and/or modify 
%%%% it under the terms of the GNU General Public License as published by 
%%%% the Free Software Foundation; either version 3 of the License, or 
%%%% (at your option) any later version.  

%%%% This file is distributed in the hope that it will be useful, 
%%%% but WITHOUT ANY WARRANTY; without even the implied warranty of 
%%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
%%%% GNU General Public License for more details.  

%%%% You should have received a copy of the GNU General Public License 
%%%% along with this file; if not, write to the Free Software 
%%%% Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 

%%%% Please send bug reports to Laurence.Finston@gmx.de

%%%% The author can be contacted at: 

%%%% Laurence Finston 
%%%% Laurence.Finston@gmx.de

%% * (1) Macro sinewave



def Sinewave (wwave_path) {point sstart_pt,
              point eend_pt,
              numeric pperiod,
              numeric aamplitude,
              numeric iincrement,
              numeric ffrequency_val,
              numeric ooffset} =

%% ** (2)
    
  %message "Entering `Sinewave'.";

  numeric j;
  j := 8;
  numeric k;
  k := 0;

  point A;

  numeric x_coord;
  numeric y_coord;


  
  % message "pperiod        = " & decimal pperiod;
  % message "aamplitude     = " & decimal aamplitude;
  % message "iincrement     = " & decimal iincrement;
  % message "ffrequency_val = " & decimal ffrequency_val;
  % message "ooffset        = " & decimal ooffset;


  
%% ** (2)

  wwave_path := null_path;

  wwave_path += ..;
  
  for i = 0 step iincrement until pperiod:
    % message "i:";
    % show i;

    % message "k*iincrement:";
    % show (k*iincrement);

    % message "sin i:";
    % show sin i;
    
    n := xpart sstart_pt + k*iincrement*ffrequency_val;
    exit_if (n > (xpart eend_pt));

    A := sstart_pt shifted by ((k*iincrement*ffrequency_val, sin (i+ooffset))
                    scaled (1, aamplitude));

    %drawdot A with_pen pencircle scaled (5mm, 5mm, 5mm);
    
    if (xpart A) < (xpart sstart_pt):
      x_coord := xpart sstart_pt;
      message "xpart A < xpart sstart_pt.";
      message "xpart A:";
      show xpart A;
      message "x_coord:";
      show x_coord;
      pause;
    else:
      x_coord := xpart A;
    fi;

    y_coord := ypart A;

    wwave_path += (x_coord, y_coord);



    
    if (sin (i+ooffset) == 0):
      drawdot sstart_pt shifted (k*iincrement*ffrequency_val, 0)
        with_pen pencircle scaled (2mm, 2mm, 2mm);
    elseif (magnitude(sin (i+ooffset)) == 1):
      draw sstart_pt shifted (k*iincrement*ffrequency_val, -.5)
           -- sstart_pt shifted (k*iincrement*ffrequency_val, .5);
    fi;
    j += 1;
    k += 1;
  endfor;

%% ** (2)

enddef;

endinput;


%% * (1) Emacs-Lisp code for use in indirect buffers when using the          
%%       GNU Emacs editor.  The local variable list is not evaluated when an 
%%       indirect buffer is visited, so it's necessary to evaluate the       
%%       following s-expression in order to use the facilities normally      
%%       accessed via the local variables list.                              
%%       LDF 2004.02.12.

%% (progn (metafont-mode) (outline-minor-mode t) (setq fill-column 80) (ignore '(  
%% )) (setq outline-regexp "%% [*\f]+"))

%% * (1) Local variables for Emacs.

%% Local Variables:
%% mode:Metapost
%% eval:(outline-minor-mode t)
%% eval:(read-abbrev-file abbrev-file-name)
%% outline-regexp:"%% [*\f]+"
%% End:
