使用乳膠中的選票序列產生加泰隆尼亞語路徑圖

使用乳膠中的選票序列產生加泰隆尼亞語路徑圖

這是加泰隆尼亞路徑的圖片:

在此輸入影像描述

它是 Z^2 中的一個格子路徑,從某一點 (a,b) 開始,並使用步驟 (1,1) 和 (1,-1)。它可以被解碼(知道起始點)為選票序列 111-1-1....,其中 1 對應於 up(1,1),-1 對應於 down(1,-1)。有沒有辦法使用 tex 只輸入選票序列並獲得相應的加泰羅尼亞路徑?

答案1

一個簡單的foreach就可以做到:

\documentclass[tikz,border=2mm]{standalone}

\begin{document}
\begin{tikzpicture}
\draw[->] (0,-1)--++(90:4);
\draw[->] (-1,0)--++(0:12);
\coordinate (aux) at (0,0);
\foreach \i in {1,-1,1,1,1,-1,1,-1,-1,1,-1}
    \draw[->] (aux)--++(1,\i) coordinate (aux);
\end{tikzpicture}
\end{document}

在此輸入影像描述

答案2

差不多做到了(儘管沒有軸):

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\tikzset{arrow path/.style={decoration={show path construction,
  lineto code={
    \path [->, every lineto/.try] 
      (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
  }}, decorate},
  every lineto/.style={draw, thick, ->},
  lineto/.style={every lineto/.append style={#1}},
  ballot sequence/.style={arrow path, .. ballot sequence=#1@@;},
  .. ballot sequence/.code args={#1#2#3;}{%
    \if#1@\else
      \if#1-
      \tikzset{symbol #1#2/.try, .. ballot sequence=#3;}%
    \else
      \tikzset{symbol #1/.try, .. ballot sequence=#2#3;}%
    \fi\fi%
  },
  symbol -1/.style={insert path={ -- ++(1,-1) }},
  symbol 1/.style={insert path={ -- ++(1,1) }}
}
\begin{document} 
\begin{tikzpicture}[>=stealth]
\path [lineto=red]   (5,0) [ballot sequence={1-1}];
\path [lineto=green] (3,3) [ballot sequence={111-1-1-1}];
\path [lineto=blue]  (0,6) [ballot sequence={1-1111-11-1-11-1-1}];
\end{tikzpicture}
\end{document}

在此輸入影像描述

相關內容