%% sinewaves3.ldf
%% Created by Laurence D. Finston (LDF) So 13. Aug 20:15:09 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

input "plainldf.lmc";

medium_pen := pencircle scaled (.333mm, .333mm, .333mm);

verbatim_tex "\magnification=3000";

verbatim_metapost "dotlabeldiam := 3mm;";

%verbatim_metapost   "prologues := 3;outputtemplate := \"%j_%3c.eps\";";

verbatim_metapost   "outputformat:=\"png\";outputtemplate := \"%j_%3c.png\";"
                  & "outputformatoptions := \"antialias=none\";";

%% * (1)

point p[];
point a[];
path Q;
path q[];
reg_polygon r[];
circle c[];
ellipse e[];

picture v[];

transform t[];

string s;

boolean b;

bool_point bp;
bool_point_vector bpv;

focus f;

set f with_position (0, 10, -40) with_direction (0, 10, 10) 
       with_distance 45;


pen huge_pen;

huge_pen := pencircle scaled (5mm, 5mm, 5mm);


numeric frame_wd;
numeric frame_ht;

% \special{papersize=400mm, 225mm} %% Frame size, proportion 16x9
% \hsize=400mm
% \vsize=225mm

%% (* (/ 400 16.0) 9) 225.0

frame_wd := 400mm;
frame_ht := 225mm;

path frame[];

frame1 :=    (-.5frame_wd, -.5frame_ht) -- (.5frame_wd, -.5frame_ht)
          -- (.5frame_wd, .5frame_ht)
          -- (-.5frame_wd, .5frame_ht) -- cycle;


frame0 := frame1 scaled (.9, .9);

% (* 400 .9) 360.0
% (* 225 .9) 202.5


%% * (1)



pickup medium_pen;


plane M;
line L[];

input "sinewaves.lmc";

%% * (1)

for j = 0 upto 3:
  p[j] := get_point (j) frame0;
endfor;

for j = 0 upto 3:
  p[4+j] := mediate(p[j], p[(j+1) mod 4]);
endfor;

pickup big_pen;

save_offset := 0.03125pi;
offset := save_offset;

increment := pi/120;
period := 5pi-1.5increment;
amplitude := 5;
frequency_val := 4.5;

amplitude_sign_val := 1;
increment_sign_val := 1;

boolean do_labels;
do_labels := false; % true; %

%% * (1)

if false:
beginfig(400);
  draw frame0;
  draw frame1;
  fill p0 -- p1 -- p5 -- p7 -- cycle with_color red;
  fill p2 -- p3 -- p7 -- p5 -- cycle with_color blue;
endfig with_projection parallel_x_y;
fi

%% * (1)

for i = 0 upto 300:
  
  beginfig(i);

    draw frame0;

    %draw p7 -- p5;

    if do_labels:
      dotlabel.bot("$p_0$", p0);
      dotlabel.bot("$p_1$", p1);
      dotlabel.top("$p_2$", p2);
      dotlabel.top("$p_3$", p3);
      dotlabel.bot("$p_4$", p4);
      dotlabel.rt("$p_5$", p5);
      dotlabel.top("$p_6$", p6);
      dotlabel.lft("$p_7$", p7);
    fi;

    % if (i == 238) or (i == 239):
    %   message "increment:  " & decimal increment;
    %   pause;
    % fi;

    sinewave (Q) {p7, p5, period, amplitude, increment, frequency_val, offset};

    q0 := Q;
    
    dot_pen := pencircle scaled (3mm, 3mm, 3mm);  
    
    p8 := get_point 0 q0;
    %drawdot p8 with_color red with_pen dot_pen;
    
    p9 := get_last_point q0;

    %drawdot p9 with_color red with_pen dot_pen;

    p10 := (xpart p5, ypart p9);

    %drawdot p10 with_color green with_pen dot_pen;
    
    q1 := q0 .. p10 -- p2 -- p3 -- p8;
    q1 += cycle;

    draw frame1;
    draw frame0;
    
    %draw q1 with_color blue;
    fill frame0 with_color blue;
    fill q1 with_color red;

    q2 := q0 .. p10 -- p1 -- p0 -- p8;
    q2 += cycle;

    %draw q2 with_color red;
    % fill frame0 with_color red on_picture v2;
    % fill q2 with_color blue on_picture v2;

    path_vector pv;

    pv := resolve q2 to 300;
    
    q4 := pv0;
    
    n := length q4;
    n /= 3;
    n := floor n;

    draw subpath(0, n) of q4 with_color orange with_pen huge_pen;
    
  endfig with_projection parallel_x_y;

%% ** (2)

  
  offset += save_offset;

  if magnitude(amplitude) >= 9:
    amplitude_sign_val *= -1;
  fi;

  amplitude += .125 * amplitude_sign_val;

  if magnitude(increment) > pi:
    increment_sign_val *= -1;
  fi;
    
  increment += (pi/120) * increment_sign_val;

  if increment == 0:
    increment := pi/120;
  fi;

  message "Fig. " & decimal i;
    
endfor;

bye;

%% ** (2) 

%% * (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:
