Skip to content

Instantly share code, notes, and snippets.

@jamesamiller
Last active February 14, 2021 00:35
Show Gist options
  • Save jamesamiller/b0a6a9d4dc126ad17831143f1d3e0e9b to your computer and use it in GitHub Desktop.
Save jamesamiller/b0a6a9d4dc126ad17831143f1d3e0e9b to your computer and use it in GitHub Desktop.
Generate a spacetime or two-observer diagram in special relativity
\documentclass[crop=true, border=10pt]{standalone}
\usepackage{comment}
\begin{comment}
:Title: Spacetime diagrams
:Author: J A Miller (millerja@uah.edu), 2020/02/06
Generate spacetime or two-observer diagrams.
Parameters can be set by the user. The distance between neighboring grid lines is one.
Plot style and colors from McNees
https://gist.github.com/mcnees/45b9f53ad371c38ba6f3759df5880fb1
\end{comment}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
% colors
\definecolor{plum}{rgb}{0.36078, 0.20784, 0.4}
\definecolor{chameleon}{rgb}{0.30588, 0.60392, 0.023529}
\definecolor{cornflower}{rgb}{0.12549, 0.29020, 0.52941}
\definecolor{scarlet}{rgb}{0.8, 0, 0}
\definecolor{brick}{rgb}{0.64314, 0, 0}
\definecolor{sunrise}{rgb}{0.80784, 0.36078, 0}
\definecolor{lightblue}{rgb}{0.15,0.35,0.75}
\begin{document}
% ----------------------- user specifications ------------------------
% Define the lower left corner of the x-t region
\newcommand*\xmin{-1}
\newcommand*\tmin{-1}
% Define the upper right corner of the x-t region
\newcommand*\xmax{11}
\newcommand*\tmax{11}
% Define the range of the t^\prime gridlines
\newcommand*\tpmin{-3}
\newcommand*\tpmax{11}
% Define the range of the x^\prime gridlines
\newcommand*\xpmin{-4}
\newcommand*\xpmax{11}
% Velocity of primed frame (can be negative)
\newcommand*\vrel{0.5}
% --------------------- end user specifications ---------------------
% Some calculations
\pgfmathsetmacro{\gamrel}{divide(1,sqrt(1-pow(\vrel,2)))}
\pgfmathsetmacro{\etarel}{\vrel*\gamrel}
% The diagram
\begin{tikzpicture}[scale=1,domain=\xmin:\xmax]
\tikzstyle{axisarrow} = [-{Latex[inset=0pt,length=8pt]}]
\coordinate (origin) at (0,0);
% Draw the grid.
%\draw [cornflower!30,step=0.2,thin] (\xmin,\tmin) grid (\xmax,\tmax); % minor grid lines
\draw [cornflower!60,step=1.0,thin] (\xmin,\tmin) grid (\xmax,\tmax); % major grid lines
% Clip all lines that would fall outside the grid
\clip(\xmin,\tmin) rectangle (\xmax,\tmax);
% Draw unprimed axes. May need to fiddle with label placement.
\newcommand*{\eps}{0.25} % fiddling value
\draw[thick,axisarrow,cornflower] (0,\tmin) -- (0,\tmax) node (tup) {};
\node[inner sep=0pt] at (\eps,\tmax-\eps) {$t$};
\draw[thick,axisarrow,cornflower] (\xmin,0) -- (\xmax,0) node (xup) {};
\node[inner sep=0pt] at (\xmax-\eps,-\eps) {$x$};
% Draw primed axes
\draw[thick,axisarrow,scarlet] (0,0) -- (\vrel*\tmax,\tmax) node (tp) {};
\node[inner sep=0pt] at (\vrel*\tmax+\eps,\tmax-\eps) {$t^\prime$};
\draw[thick,axisarrow,scarlet] (0,0) -- (\xmax,\vrel*\xmax) node (xp) {};
\node[inner sep=0pt] at (\xmax-\eps,\vrel*\xmax-\eps) {$x^\prime$};
% Draw lines of constant xprime
\foreach \xprime in {\xpmin,...,\xpmax}
{
\draw[scarlet!60,thin] plot (\x,\x/\vrel - \xprime/\etarel);
}
% Draw lines of constant tprime
\foreach \tprime in {\tpmin,...,\tpmax}
{
\draw[scarlet!60,thin] plot (\x,\x*\vrel + \tprime/\gamrel);
}
\end{tikzpicture}
\end{document}
@jamesamiller
Copy link
Author

Example for a relative velocity of +1/2.

vrel-positive

@jamesamiller
Copy link
Author

Example for a relative velocity of -1/2.
vrel-negative

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment