%% sinewaves.ldf
%% Created by Laurence D. Finston (LDF) Do 10. Aug 09:53:53 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;

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;
    
    %draw q0 with_color red;

    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;


  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;


% beginfig(1);
%   draw frame0;
%   draw frame1;
% endfig;








%% * (1) Sine wave with moving dot, three colors.


%% * (1) Fig. 1

pen big_dot_pen;
big_dot_pen := pencircle scaled (.25, .25, .25);

%show length q0;
%pause;


beginfig(1);
  draw frame1;
  fill frame0 with_color dark_green;
  fill q1 with_color red;

  v0 := current_picture;

  clip v0 to frame0;

  output v0 with_projection parallel_x_y;
  clear current_picture;
  clear v0;
    
  draw frame0;
  draw frame1;

endfig with_projection parallel_x_y;

%% * (1) Fig. 2--66

n := length q0;
n -= 1;

for i = 0 upto n:
  beginfig(i+2);
    fill frame0 with_color dark_green;
    fill q1 with_color red;
    
    if i > 0:
      draw subpath(0, i) of q0 with_color blue with_pen big_dot_pen;
    fi;

    drawdot p[i+8] with_color blue with_pen big_dot_pen;

    v0 := current_picture;

    clip v0 to frame0;

    output v0 with_projection parallel_x_y;
    clear current_picture;
    clear v0;
    
    draw frame0;
    draw frame1;
    
    
    message "Fig. " & decimal (i+2);
  endfig with_projection parallel_x_y;
endfor;

%pause;

%% * (1) Fig. 67--131

for i = 0 upto n:
  beginfig(67+i);
    fill frame0 with_color dark_green;
    fill q1 with_color red;
    draw q0 with_color blue with_pen big_dot_pen;

    if i > 0:
      draw subpath(0, i) of q0 with_color orange with_pen big_dot_pen;
    fi;
    drawdot p[i+8] with_color orange with_pen big_dot_pen;

    v0 := current_picture;

    clip v0 to frame0;

    output v0 with_projection parallel_x_y;
    clear current_picture;
    clear v0;
    
    draw frame0;
    draw frame1;
    
    message "Fig. " & decimal (67+i);
  endfig with_projection parallel_x_y;
endfor;

%% * (1) Fig. 132-- 196

for i = 0 upto n:
  beginfig(132+i);
    fill frame0 with_color dark_green;
    fill q1 with_color red;
    draw q0 with_color orange with_pen big_dot_pen;

    if i > 0:
      draw subpath(0, i) of q0 with_color violet with_pen big_dot_pen;
    fi;
    drawdot p[i+8] with_color violet with_pen big_dot_pen;

    v0 := current_picture;

    clip v0 to frame0;

    output v0 with_projection parallel_x_y;
    clear current_picture;
    clear v0;
    
    draw frame0;
    draw frame1;
    
    message "Fig. " & decimal (132+i);
  endfig with_projection parallel_x_y;
endfor;


bye;



%% * (1) Fig. 1. 

beginfig(1);

  rectangle r[];

  r0 := unit_rectangle scaled (4, 0, 4);

  for i = 0 upto 3:
    p[i] := get_point (i) r0;
  endfor;

  dotlabel.bot("$p_0$", p0);
  dotlabel.bot("$p_1$", p1);
  dotlabel.top("$p_2$", p2);
  dotlabel.top("$p_3$", p3);
    
  p4 := (1, -2);
  p5 := (-2, 2);

  draw r0;

  q0 := p4 -- p5;
  
  draw q0;
  draw r0;

  dotlabel.lft("$p_4$", p4);
  dotlabel.lft("$p_5$", p5);

  b := q2 is_colinear r0;

  % message "b:";
  % show b;
   
  p7 := p2 shifted (1, 1);
  p8 := p1 shifted (2, -1);

  dotlabel.rt("$p_7$", p7);
  dotlabel.rt("$p_8$", p8);

  q1 := p7 -- p8;

  draw q1;

  L0 := get_line q0;

  % message "L0:";
  % show L0;

  L1 := get_line q1;

  p9 := mediate(p8, p7);
  p10 := mediate(p8, p7, 1.5);

  dotlabel.lft("$p_{9}$", p9) with_color blue;
  dotlabel.lft("$p_{10}$", p10) with_color blue;

  q2 := p9 -- p10; 

  draw q2 with_color blue;

  L2 := get_line q2;

  % message "L1:";
  % show L1;

  % message "L2:";
  % show L2;

  %p11 := get_direction L1;
  % message "p11:";
  % show p11;

  % p12 := get_direction L2;
  % message "p12:";
  % show p12;

  % b := L1 ~= L2;
  % message "b:";
  % show b;
  
  % if L1 ~= L2:
  %   message "Equivalent.";
  % else:
  %   message "Not equivalent.";
  % fi;
  
  % pause;
  
  b := L1 is_parallel L2;
  message "b:";
  show b;
  pause;

  q3 := q2 shifted (1, 0);
  L3 := get_line q3;
  
  % b := L2 is_parallel L3;
  % message "b:";
  % show b;
  % pause;
  
  
endfig with_focus f;

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:
