% Diagrams adapted from https://tikz.net/relativity_minkowski_diagram/

%
% MARK: Matt's tweaks
%

\newcommand{\emptydiagram}[1]{
	\begin{center}
		\begin{tikzpicture}[scale=2]
			\def\xmax{2}
			\def\xmaxp{2.2} % maximum of rotated axis
			\def\Nlines{6} % number of world lines (at constant x/t)
			\pgfmathsetmacro\ang{atan(1/2)} % angle between x and x' axes
			\pgfmathsetmacro\d{0.9*\xmax/\Nlines}refer % grid size
			\pgfmathsetmacro\D{\d/cos(\ang)/sqrt(1-tan(\ang)^2)} % grid size, boosted
			\coordinate (O) at (0,0);
			\coordinate (X) at (\xmax+0.2,0);
			\coordinate (T) at (0,\xmax+0.2);
			\coordinate (X') at (\ang:\xmaxp+0.2);
			\coordinate (T') at (90-\ang:\xmaxp+0.2);

			\foreach \i [evaluate={\x=\i*\d;}] in {1,...,\Nlines}{
				\draw[world line]   (-\x,-\xmax) -- (-\x,\xmax);
				\draw[world line]   ( \x,-\xmax) -- ( \x,\xmax);
				\draw[world line t] (-\xmax,-\x) -- (\xmax,-\x);
				\draw[world line t] (-\xmax, \x) -- (\xmax, \x);
			}

			% AXES
			\draw[->,thick] (0,-\xmax) -- (T) node[left=-1] {#1: $t$};
			\draw[->,thick] (-\xmax,0) -- (X) node[below=0] {$x$};
		\end{tikzpicture}
	\end{center}
}

\newcommand{\emptydiagramc}[1]{
	\begin{center}
		\begin{tikzpicture}[scale=2]
			\def\xmax{2}
			\def\xmaxp{2.2} % maximum of rotated axis
			\def\Nlines{6} % number of world lines (at constant x/t)
			\pgfmathsetmacro\ang{atan(1/2)} % angle between x and x' axes
			\pgfmathsetmacro\d{0.9*\xmax/\Nlines}refer % grid size
			\pgfmathsetmacro\D{\d/cos(\ang)/sqrt(1-tan(\ang)^2)} % grid size, boosted
			\coordinate (O) at (0,0);
			\coordinate (X) at (\xmax+0.2,0);
			\coordinate (T) at (0,\xmax+0.2);
			\coordinate (X') at (\ang:\xmaxp+0.2);
			\coordinate (T') at (90-\ang:\xmaxp+0.2);

			\foreach \i [evaluate={\x=\i*\d;}] in {1,...,\Nlines}{
				\draw[world line]   (-\x,-\xmax) -- (-\x,\xmax);
				\draw[world line]   ( \x,-\xmax) -- ( \x,\xmax);
				\draw[world line t] (-\xmax,-\x) -- (\xmax,-\x);
				\draw[world line t] (-\xmax, \x) -- (\xmax, \x);
			}

			% AXES
			\draw[->,thick] (0,-\xmax) -- (T) node[left=-1] {#1: $ct$};
			\draw[->,thick] (-\xmax,0) -- (X) node[below=0] {$x$};
		\end{tikzpicture}
	\end{center}
}

\newcommand{\halfdiagramc}[1]{
	\begin{center}
		\begin{tikzpicture}[scale=2]
			\def\xmax{2}
			\def\xmaxp{2.2} % maximum of rotated axis
			\def\Nlines{6} % number of world lines (at constant x/t)
			\pgfmathsetmacro\ang{atan(1/2)} % angle between x and x' axes
			\pgfmathsetmacro\d{0.9*\xmax/\Nlines}refer % grid size
			\pgfmathsetmacro\D{\d/cos(\ang)/sqrt(1-tan(\ang)^2)} % grid size, boosted
			\coordinate (O) at (0,0);
			\coordinate (X) at (\xmax+0.2,0);
			\coordinate (T) at (0,\xmax+0.2);
			\coordinate (X') at (\ang:\xmaxp+0.2);
			\coordinate (T') at (90-\ang:\xmaxp+0.2);

			% WORLD LINE GRID
			\message{  Making world lines...^^J}
			\foreach \i [evaluate={\x=\i*\d;}] in {1,...,\Nlines}{
			\message{  Running i/N=\i/\Nlines, x=\x...^^J}
			\draw[world line]   (-\x,-0.2) -- (-\x,\xmax);
			\draw[world line]   ( \x,-0.2) -- ( \x,\xmax);
			%\draw[world line t] (-\xmax,-\x) -- (\xmax,-\x);
			\draw[world line t] (-\xmax, \x) -- (\xmax, \x);
			}

			% AXES
			\draw[->,thick] (0,-0.2) -- (T) node[left=-1] {#1: $ct$};
			\draw[->,thick] (-\xmax,0) -- (X) node[below=0] {$x$};
		\end{tikzpicture}
	\end{center}
}

\newcommand{\halfdiagramcwide}[1]{
	\begin{center}
		\begin{tikzpicture}[scale=2]
			\def\xmax{3.5}
			\def\xmaxp{2.2} % maximum of rotated axis
			\def\Nlines{10} % number of world lines (at constant x/t)
			\pgfmathsetmacro\ang{atan(1/2)} % angle between x and x' axes
			\pgfmathsetmacro\d{0.9*\xmax/\Nlines}refer % grid size
			\pgfmathsetmacro\D{\d/cos(\ang)/sqrt(1-tan(\ang)^2)} % grid size, boosted
			\coordinate (O) at (0,0);
			\coordinate (X) at (\xmax+0.2,0);
			\coordinate (T) at (0,1.95);
			\coordinate (X') at (\ang:\xmaxp+0.2);
			\coordinate (T') at (90-\ang:\xmaxp+0.2);

			\foreach \i [evaluate={\x=\i*\d;}] in {1,...,\Nlines}{
				\draw[world line]   (-\x,-0.2) -- (-\x,1.95);
				\draw[world line]   ( \x,-0.2) -- ( \x,1.95);
				%\draw[world line t] (-\xmax,-\x) -- (\xmax,-\x);
				%\draw[world line t] (-\xmax, \x) -- (\xmax, \x);
			}

			\foreach \i [evaluate={\x=\i*\d;}] in {1,...,5}{
				% \draw[world line]   (-\x,-0.2) -- (-\x,\xmax);
				% \draw[world line]   ( \x,-0.2) -- ( \x,\xmax);
				%\draw[world line t] (-\xmax,-\x) -- (\xmax,-\x);
				\draw[world line t] (-\xmax, \x) -- (\xmax, \x);
			}

			% AXES
			\draw[->,thick] (0,-0.2) -- (T) node[left=-1] {#1: $ct$};
			\draw[->,thick] (-\xmax,0) -- (X) node[below=0] {$x$};
		\end{tikzpicture}
	\end{center}
}


%
% MARK: tikz setup
%

\usepackage{tikz}
\usetikzlibrary{patterns}

\usepackage{hyperref}
\usepackage{graphicx}
\usepackage{caption}

\usepackage{etoolbox} % ifthen
\usepackage[outline]{contour} % glow around text
\usetikzlibrary{calc} % for adding up coordinates
\usetikzlibrary{decorations.markings,decorations.pathmorphing}
\usetikzlibrary{angles,quotes} % for pic (angle labels)
\usetikzlibrary{arrows.meta} % for arrow size
\usepackage{xfp} % higher precision (16 digits?)
\contourlength{1.1pt}

\tikzset{>=latex} % for LaTeX arrow head
\colorlet{myred}{red!85!black}
\colorlet{mydarkred}{red!55!black}
\colorlet{mylightred}{red!85!black!12}
\colorlet{myfieldred}{mydarkred!5} % for S' background
\colorlet{myredhighlight}{myred!20} % highlights simultaneity in ladder paradox
\colorlet{myblue}{blue!80!black}
\colorlet{mydarkblue}{blue!50!black}
\colorlet{mylightblue}{blue!50!black!30}
\colorlet{mylightblue2}{myblue!10}
\colorlet{mygreen}{green!80!black}
\colorlet{mypurple}{blue!40!red!80!black}
\colorlet{mydarkgreen}{green!50!black}
\colorlet{mydarkpurple}{blue!40!red!50!black}
\colorlet{myorange}{orange!40!yellow!95!black}
\colorlet{mydarkorange}{orange!40!yellow!85!black}
\colorlet{mybrown}{brown!20!orange!90!black}
\colorlet{mydarkbrown}{brown!20!orange!55!black}
\colorlet{mypurplehighlight}{mydarkpurple!20} % highlights simultaneity in ladder paradox
\tikzstyle{world line}=[myblue!40,line width=0.3]
\tikzstyle{world line t}=[mypurple!50!myblue!40,line width=0.3]
\tikzstyle{world line'}=[mydarkred!40,line width=0.3]
\tikzstyle{mysmallarr}=[-{Latex[length=3,width=2]},thin]
\tikzstyle{mydashed}=[dash pattern=on 3 off 3]
\tikzstyle{rod}=[mydarkbrown,draw=mydarkbrown,double=mybrown,double distance=2pt,
                 line width=0.2,line cap=round,shorten >=1pt,shorten <=1pt]
%\tikzstyle{rod'}=[rod,draw=mydarkbrown!80!red!85,double=mybrown!80!red!85]
\tikzstyle{vector}=[->,line width=1,line cap=round]
\tikzstyle{vector'}=[vector,shorten >=1.2]
\tikzstyle{particle}=[mygreen,line width=0.9]
\tikzstyle{photon}=[-{Latex[length=5,width=4]},myorange,line width=0.8,decorate,
                    decoration={snake,amplitude=1.0,segment length=5,post length=5}]

\def\tick#1#2{\draw[thick] (#1) ++ (#2:0.06) --++ (#2-180:0.12)}
\def\tickp#1#2{\draw[thick,mydarkred] (#1) ++ (#2:0.06) --++ (#2-180:0.12)}
\def\Nsamples{100} % number samples in plot

% COMMON AXES
\pgfdeclarelayer{back} % to draw on background
\pgfsetlayers{back,main} % set order
\def\xmin{0.23}
\def\xmax{2}
\def\Nlines{6} % number of world lines (at constant x/t)
\def\DNxp{0}   % difference in number of world lines of x' axis
\def\DNyp{0}   % difference in number of world lines of ct' axis
\def\DNy{0}    % difference in number of world lines of ct axis
\def\ang{20}   % angle between x and x' axes
\def\xplabelang{180} % anchor angle of x' axis label
%\pgfmathsetmacro\ang{atan(0.44)} % angle between x and x' axes
\def\axes{
	\pgfmathsetmacro\d{\xmax/(\Nlines+0.4)} % grid size
	\pgfmathsetmacro\D{\d/cos(\ang)/sqrt(1-tan(\ang)^2)} % grid size, boosted
	\pgfmathsetmacro\ymax{\xmax+\DNy*\d} % maximum of y = ct axis
	\pgfmathsetmacro\xmaxp{(\xmax/\d+\DNxp)*\D} % maximum of x' axis
	\pgfmathsetmacro\ymaxp{(\xmax/\d+\DNyp)*\D} % maximum of y' = ct' axis
	\pgfmathsetmacro\Nylines{\Nlines+\DNy} % number of world lines at constant ct'
	\pgfmathsetmacro\Nxplines{\Nlines+\DNxp} % number of world lines at constant x'
	\pgfmathsetmacro\Nyplines{\Nlines+\DNyp} % number of world lines at constant ct'
	\coordinate (O) at (0,0);
	\coordinate (X) at (\xmax+0.15,0);
	\coordinate (T) at (0,\ymax+0.15);
	\coordinate (X') at (\ang:\xmaxp+0.2);
	\coordinate (T') at (90-\ang:\ymaxp+0.2);

	% FILL
	\begin{pgfonlayer}{back} % draw on back
	\fill[myfieldred]
		(\ang:-\xmin) -- (\ang:\xmaxp) --++ (90-\ang:\ymaxp) --++ (\ang:-\xmaxp)
		-- (90-\ang:-\xmin) -- cycle;
	\end{pgfonlayer}

	% WORLD LINE GRID
	\message{  Making world lines...^^J}
	\foreach \i [evaluate={\x=\i*\d;}] in {1,...,\Nlines}{
	\draw[world line]   (\x,0) -- (\x,\ymax);
	}
	\foreach \i [evaluate={\t=\i*\d;}] in {1,...,\Nylines}{
	\draw[world line t] (0,\t) -- (\xmax,\t);
	}

	% BOOSTED WORLD LINE GRID
	\foreach \i [evaluate={\x=\i*\D;}] in {1,...,\Nxplines}{
		\draw[world line'] (\ang:\x) --++ (90-\ang:\ymaxp);
	}
	\foreach \i [evaluate={\t=\i*\D;}] in {1,...,\Nyplines}{
		\draw[world line'] (90-\ang:\t) --++ (\ang:\xmaxp);
	}

	% AXES
	\draw[->,thick] (0,-\xmin) -- (T) node[left=-1] {$ct$};
	\draw[->,thick] (-\xmin,0) -- (X) node[below=0] {$x$};
	\draw[->,thick,mydarkred] (90-\ang:-\xmin) -- (T')
	node[right=5,above=-1] {$ct'$};
	\draw[->,thick,mydarkred] (\ang:-\xmin) -- (X')
	node[anchor=\xplabelang,inner sep=2] {$x'$};
}