%% sinewaves7.ldf
%% Created by Laurence D. Finston (LDF) Fr 18. Aug 22:12:03 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=\magstep2\font\huge=cmbx12 scaled 4000"
             & "\font\Large=cmbx12 scaled 3000\font\large=cmbx12 scaled 1600";

bool do_png;
do_png := false; % true; % 

if do_png:
  verbatim_metapost   "outputformat:=\"png\";outputtemplate := \"%j_%3c.png\";"
                    & "outputformatoptions := \"format=rgb antialias=none\";";
else:
  verbatim_metapost "prologues := 3;outputtemplate := \"%j_%3c.eps\";dotlabeldiam:=2mm;";
fi;




%% * (1)

point p[];
path q[];
rectangle r[];
transform t[];

string s;

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

%% (* (/ 300 16.0) 9) 168.75

%% (* (/ 30.0 16) 9) 16.875


picture v[];

pen huge_pen;

huge_pen := pensquare scaled (2mm, 2mm, 2mm);


%% * (1)

pickup medium_pen;

%% * (1)

color_vector cv;

color C;

cv += black;
cv += red;
cv += blue;
cv += green;
cv += cyan;
cv += magenta;
cv += yellow;
cv += violet;
cv += orange;
C := (.5, .5, 0);
cv += C;
C := (.5, 0, .5);
cv += C;
C := (0, .5, .5);
cv += C;
C := (.25, .75, 0);
cv += C;
C := (.75, .25, 0);
cv += C;
C := (.75, 0, .25);
cv += C;
C := (.25, 0, .75);
cv += C;

%% * (1)

sinewave s[];

pickup medium_pen;

dot_pen := pencircle scaled (2mm, 2mm, 2mm);

bool do_labels;
do_labels := false; % true; %

set s0 with_amplitude 8 with_frequency 1 with_phase 0 with_increment .1
  with_period 8pi;


r0 := get_rectangle s0 with_scaled (1, 1.025);


% message "r0:";
% show r0;
% pause;

p11 := get_center r0;


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


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

rectangle frame;

n := magnitude(p2 - p1);

frame_ht := n;

frame_wd := n * 16/9;

frame := ((-.5frame_wd, -.5frame_ht), (.5frame_wd, -.5frame_ht),
          (.5frame_wd, .5frame_ht), (-.5frame_wd, .5frame_ht));

p10 := get_center frame;

shift frame by (p11 - p10);


%% * (1)

beginfig(300);
  draw frame with_pen big_square_pen;
  s := "\setbox0=\hbox{{\huge Sine Waves Nr.~7}}"
       &  "\vbox{\copy0\vskip2cm"
       & "\hbox to \wd0{\hss{\Large By}\hss}\vskip1cm"
       & "\hbox to \wd0{\hss{\Large Laurence Finston}\hss}\vskip2.5cm"
       & "\hbox to \wd0{\hss{\large Copyright (C) 2023 Laurence Finston}\hss}"
       & "}";
  label(s, p11);
  %dotlabel.top("$p_{10}$", p11);
endfig with_projection parallel_x_y;

beginfig(301);
  draw frame with_pen big_square_pen;
  s :=   "\hbox{\hfil{\huge Marbling Effect Nr.~1}\hfil}";
  label(s, p11);
  %dotlabel.top("$p_{11}$", p11);
endfig with_projection parallel_x_y;

beginfig(400);
  draw frame with_pen big_square_pen;
  s :=   "\hbox{\hfil{\huge The End}\hfil}";
  label(s, p11);
  %dotlabel.top("$p_{11}$", p11);
endfig with_projection parallel_x_y;

%% * (1)

k := 7.5;
for i = 1 upto 15:
  set s[i] with_amplitude k with_frequency 1 with_phase 0 with_increment .1
    with_period 8pi;
  k-=.5;
endfor;

% message "n:";
% show n;
%pause;

for i = 0 upto 6:
  n := length s15;
  n /= 8;
  n *= (i+1);
  n := floor n;
  p[12+i] := get_point (n) s15;
endfor;

%% * (1)

%% This works.  LDF 2023.08.18.

n := length s0;
n /= 8;
n := floor n;

for i = 0 upto 15:
  q[i] := s[i];
  q[i+20] := subpath(0, n) of q[i];
endfor;

%% q41--q55

for i = 1 upto 15:
  q[i+40] := p7 & q[i+20] & p12 & reversed q[i+20-1] & p7;
  q[i+40] += cycle;

  for j = 1 upto 3:
    q[i+40+20j] := q[i+40] shifted by (j*(p14 - p12));
  endfor;
endfor;


% message "q35:";
% show q35;
%pause;

%% * (1)

path Q[];

path_vector pv;

for j = 40 step 20 until 100:
  for i = 1 upto 15:
    q[200+j+i] := (q[j+i] rotated_around (p5, p7)) shifted by (p12 -p7);
  endfor;
endfor;


if false: % true; %
for j = 40 step 20 until 100:
  for i = 1 upto 15:
    pv := resolve q[j+i] to 500;
    q[j+i] := pv0;
    q[j+i] += cycle;
    clear pv;
  endfor;
endfor;
fi;


m := 30;
k := 0;
for i = 15 downto 0:
  beginfig(k);
    if do_png:
      filldraw frame with_pen big_square_pen;
      unfill r0;
    fi;
    draw r0 with_pen big_square_pen;
    draw frame with_pen big_square_pen;

    if do_labels:
      dotlabel.top("$p_{12}$", p12);
      dotlabel.top("$p_{13}$", p13);
      dotlabel.top("$p_{14}$", p14);
      dotlabel.top("$p_{15}$", p15);
      dotlabel.top("$p_{16}$", p16);
      dotlabel.top("$p_{17}$", p17);
      dotlabel.top("$p_{18}$", p18);
      dotlabel.lft("$p_{7}$", p7);
      dotlabel.rt("$p_{5}$", p5);
    fi;
    for j = 15 downto i:
      draw s[j] with_pen big_pen;
      % This works:
      % Q[j] := s[j];
      % rotate Q[j] (30, -40);
      % draw Q[j] with_pen big_pen with_color blue;
      % LDF 2023.08.19.
    endfor;

    if k == 15:
      for i = 41 step 20 until 101:
	fill q[i] with_color cv1;
	fill q[200+i] with_color cv1;
      endfor;
    fi;

    if k > 13:
      for i = 42 step 20 until 102:
	fill q[i] with_color cv2;
	fill q[200+i] with_color cv2;
      endfor;
    fi;

    if k > 12:
      for i = 43 step 20 until 103:
	fill q[i] with_color cv3;
	fill q[200+i] with_color cv3;
      endfor;
    fi;

    if k > 11:
      for i = 44 step 20 until 104:
	fill q[i] with_color cv4;
	fill q[200+i] with_color cv4;
      endfor;
    fi;

    if k > 10:
      for i = 45 step 20 until 105:
	fill q[i] with_color cv5;
	fill q[200+i] with_color cv5;
      endfor;
    fi;

    if k > 9:
      for i = 46 step 20 until 106:
	fill q[i] with_color cv6;
	fill q[200+i] with_color cv6;
      endfor;
    fi;

    if k > 8:
      for i = 47 step 20 until 107:
	fill q[i] with_color cv7;
	fill q[200+i] with_color cv7;
      endfor;
    fi;

    if k > 7:
      for i = 48 step 20 until 108:
	fill q[i] with_color cv8;
	fill q[200+i] with_color cv8;
      endfor;
    fi;

    if k > 6:
      for i = 49 step 20 until 109:
	fill q[i] with_color cv9;
	fill q[200+i] with_color cv9;
      endfor;
    fi;

    if k > 5:
      for i = 50 step 20 until 110:
	fill q[i] with_color cv10;
	fill q[200+i] with_color cv10;
      endfor;
    fi;

    if k > 4:
      for i = 51 step 20 until 111:
	fill q[i] with_color cv11;
	fill q[200+i] with_color cv11;
      endfor;
    fi;

    if k > 3:
      for i = 52 step 20 until 112:
	fill q[i] with_color cv12;
	fill q[200+i] with_color cv12;
      endfor;
    fi;

    if k > 2:
      for i = 53 step 20 until 113:
	fill q[i] with_color cv13;
	fill q[200+i] with_color cv13;
      endfor;
    fi;


    if k > 1:
      for i = 54 step 20 until 114:
	fill q[i] with_color cv14;
	fill q[200+i] with_color cv14;
      endfor;
    fi;

    if k > 0:
      for i = 55 step 20 until 115:
	fill q[i] with_color cv15;
	fill q[200+i] with_color cv15;
      endfor;
    fi;

    clear v0;
    v0 := current_picture;

  endfig with_projection parallel_x_y;

  if k <> 15:
    beginfig(m);
      current_picture := v0;
    endfig with_projection parallel_x_y;
    m-= 1;
  fi;
  
  k+=1;

endfor;

j := .5;
for i = 0 upto 15:
  set s[31+i] with_amplitude j with_frequency 1 with_phase pi with_increment .1
    with_period 8pi;
  j+=.5;
endfor;

for i = 0 upto 6:
  n := length s31;
  n /= 8;
  n *= (i+1);
  n := floor n;
  p[19+i] := get_point (n) s31;
endfor;



k := 61;
for i = 0 upto 15:
  %message "i+31: " & decimal (i+31);
  beginfig(31+i);
    if do_png:
      filldraw frame with_pen big_square_pen;
      unfill r0;
    fi;
    draw frame with_pen big_square_pen;
    draw r0 with_pen big_square_pen;
    draw s[31+i] with_pen big_pen;
    if do_labels:
      dotlabel.rt("$p_5$", p5);
      dotlabel.lft("$p_7$", p7);
      dotlabel.top("$p_{19}$", p19);
      dotlabel.top("$p_{20}$", p20);
      dotlabel.top("$p_{21}$", p21);
      dotlabel.top("$p_{22}$", p22);
      dotlabel.top("$p_{23}$", p23);
      dotlabel.top("$p_{24}$", p24);
      dotlabel.top("$p_{25}$", p25);
    fi;
    n := 31+i;
    for j = n downto 31:
      draw s[j] with_pen big_pen;
    endfor;

    if i == 15:
      for m = 41 step 20 until 101:
	fill q[m] rotated_around (p5, p7) with_color cv1;
	fill q[200+m] rotated_around (p5, p7) with_color cv1;
      endfor;
    fi;

    if i > 13:
      for m = 42 step 20 until 102:
	fill q[m] rotated_around (p5, p7) with_color cv2;
	fill q[200+m] rotated_around (p5, p7) with_color cv2;
      endfor;
    fi;

    if i > 12:
      for m = 43 step 20 until 103:
	fill q[m] rotated_around (p5, p7) with_color cv3;
	fill q[200+m] rotated_around (p5, p7) with_color cv3;
      endfor;
    fi;

    if i > 11:
      for m = 44 step 20 until 104:
	fill q[m] rotated_around (p5, p7) with_color cv4;
	fill q[200+m] rotated_around (p5, p7) with_color cv4;
      endfor;
    fi;

    if i > 10:
      for m = 45 step 20 until 105:
	fill q[m] rotated_around (p5, p7) with_color cv5;
	fill q[200+m] rotated_around (p5, p7) with_color cv5;
      endfor;
    fi;

    if i > 9:
      for m = 46 step 20 until 106:
	fill q[m] rotated_around (p5, p7) with_color cv6;
	fill q[200+m] rotated_around (p5, p7) with_color cv6;
      endfor;
    fi;

    if i > 8:
      for m = 47 step 20 until 107:
	fill q[m] rotated_around (p5, p7) with_color cv7;
	fill q[200+m] rotated_around (p5, p7) with_color cv7;
      endfor;
    fi;

    if i > 7:
      for m = 48 step 20 until 108:
	fill q[m] rotated_around (p5, p7) with_color cv8;
	fill q[200+m] rotated_around (p5, p7) with_color cv8;
      endfor;
    fi;

    if i > 6:
      for m = 49 step 20 until 109:
	fill q[m] rotated_around (p5, p7) with_color cv9;
	fill q[200+m] rotated_around (p5, p7) with_color cv9;
      endfor;
    fi;

    if i > 5:
      for m = 50 step 20 until 110:
	fill q[m] rotated_around (p5, p7) with_color cv10;
	fill q[200+m] rotated_around (p5, p7) with_color cv10;
      endfor;
    fi;

    if i > 4:
      for m = 51 step 20 until 111:
	fill q[m] rotated_around (p5, p7) with_color cv11;
	fill q[200+m] rotated_around (p5, p7) with_color cv11;
      endfor;
    fi;

    if i > 3:
      for m = 52 step 20 until 112:
	fill q[m] rotated_around (p5, p7) with_color cv12;
	fill q[200+m] rotated_around (p5, p7) with_color cv12;
      endfor;
    fi;

    if i > 2:
      for m = 53 step 20 until 113:
	fill q[m] rotated_around (p5, p7) with_color cv13;
	fill q[200+m] rotated_around (p5, p7) with_color cv13;
      endfor;
    fi;


    if i > 1:
      for m = 54 step 20 until 114:
	fill q[m] rotated_around (p5, p7) with_color cv14;
	fill q[200+m] rotated_around (p5, p7) with_color cv14;
      endfor;
    fi;

    if i > 0:
      for m = 55 step 20 until 115:
	fill q[m] rotated_around (p5, p7) with_color cv15;
	fill q[200+m] rotated_around (p5, p7) with_color cv15;
      endfor;
    fi;


    
    clear v0;
    v0 := current_picture;
  endfig with_projection parallel_x_y;
  if i <> 15:
    %message "k: " & decimal k;
    beginfig(k);
      current_picture := v0;
    endfig with_projection parallel_x_y;
    k -= 1;
  fi;
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:
