自動連接水平同色線

自動連接水平同色線

如何自動化繪製如下圖的圖?

我懷疑這將是一個挑戰。

這些圖畫遵循一些規則。如上所示,有一個頁麵包含編號的兩個軸字段的清單。每個領域都是一個個體\begin{tikzpicture}\blank ... \connectlogically\end{tikzpicture}

(A)每個字段中都有水平箭頭。每個箭頭由巨集\pointX #1,#2|#3|#4|#5(黑色)或\pointK #1,#2|#3|#4|#5(灰色)繪製。這將繪製一個從 座標 開始(#1,#2)#3長度為單位的箭頭(該數字也標記在箭頭上方,居中),並且在其下方或上方有一個帶有座標 的點,以及箭頭尖端右側的(#1 + 0.5*#3 ,#4)標籤。 #5MWE 上有此類命令的定義範例。

(二)如果可能,應執行另外兩個命令,即\pointXo和。\pointKo這些命令需要 4 個而不是 5 個參數。它們使用x coord最後發布的箭頭尖端的 x 座標\point<K/X>加上最後發布的箭頭尖端標籤上的數字\point<K/X>。這相當於一個座標,例如(#1+#3+#5,<y coord>)從最後發出的參數中檢索參數\point<K/X>,並且<y coord>是 的第一個參數\point<K/X>o

(c) 主要問題:假設Ki在 y 位置有一個箭頭yKi,其尾部位於xKi-1,尖端位於xKi-2。圍繞該箭頭還有其他箭頭,即 arrows Kj,遵循相同的結構。現在,這些箭頭應透過垂直線連接到min(abs(yKi - yKj))滿足以下條件的垂直較近的箭頭 ( ):xKi-1 <= xKj-1 < xKi-2。我的話:如果有箭頭j,其j尾部位於i箭頭尾部和頭部之間,則最接近的箭頭i是垂直連接的。目標是有一個命令可以自動執行此操作,因為當您有大量箭頭和大量繪圖時,這會變得非常詳盡,該命令在這裡稱為\connectlogically

(四)理想情況下,您還應該能夠透過給出要連接的一對步驟的起始節點座標,手動指示 LaTeX 在相同顏色的線段之間繪製連接。然而,這透過一條彎曲線連接它們的中心。

* 如果最容易實現,所有長度和位置都可以是離散的。那就只需要 1/8 的整數倍即可,例如 4 3/4 或 -2 1/8。

下面是一個 MWE,其中包含巨集的定義\pointX和一個涵蓋所有(我認為)可能場景的測試案例,以下是當前輸出和發出命令後所需的輸出\connectlogically

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

\tikzset{dot/.style={circle, fill, minimum size=.4em, outer sep=0pt, inner sep=0pt}}

\def\pointX #1,#2|#3|#4|#5.{%
    \draw[black, ->] (#1,#2) -- node[above]{#3} (#1+#3,#2) node[right]{#5} (#1+#3/2,#2+#4) node[dot]{};
}

\def\pointK #1,#2|#3|#4|#5.{%
    \draw[gray, ->] (#1,#2) -- node[above]{#3} (#1+#3,#2) node[right]{#5} (#1+#3/2,#2+#4) node[dot]{};
}

\newcount\blankcount
\def\blank{%
    \advance\blankcount by 1
    \draw[black!64,->](0,0)--node[left]{$x\atop y$} (0,8);
    \draw[black!32](0,3)--(16,3)node{};
    \draw[black!64,->](0,0)--node[below]{$a\atop b$} (16,0);
    \node (x\the\blankcount) at (16,8) {\the\blankcount};
}

\begin{document}
    \begin{tikzpicture}
        \blank
        \pointX 1/2,1|3|1|0.
        \pointX 7/2,1/2|2|2|-1/2. % This should be \pointXo 1/2|2|2|-1/2.
        \pointX 5,2|2|-1|0.       % This should be \pointXo 2|2|-1|0.
        \pointX 7,3/2|1|3|0.      % This should be \pointXo 3/2|1|3|0.
        \pointX 1/2,2|5/2|1|19/4.
        \pointK 2,4|2|-2|-3/4.
        \pointK 13/4,5|1|-4|0.    % This should be \pointKo 5|1|-4|0.
        \pointX 31/4,3|3|3|3.     % This should be \pointXo 3|3|3|3.
        %\connectlogically        % This should connect everything automatically
    \end{tikzpicture}
\end{document}

目前的MWE

所需輸出

編輯:有兩個很好的答案; 50 (ED) 和 100 (GS) 都值得賞金。

答案1

艾瑞克說這是一個演算法問題,他是對的。因此,有多種方法可以解決這個問題。這裡我要介紹一個TikZ+etoolbox方式,廣泛使用etoolbox一般測試能力和TikZ 循環語句。

背後的邏輯是測試每個類型( )i的箭頭,其尾部座標 ( ) 是否位於尾部和頭部之間,以及其他類型的箭頭(, -是尾部和頭部)。如果符合,則測試它們之間的垂直距離 ( ) 並與最小垂直距離(初始)進行比較,如果當前垂直距離小於且。對每個箭頭執行此操作,循環結束後即可。就是這樣。KKixKi-1jKKjj≠iKj-1Kj-2Kjabs(yi-yj)minvdist100cmminvdist\let\minvdist{abs(yi-yj)}\let\j\closerjj\draw (Kcloserj-1) |- (Ki-1);\autoconnectK

除了座標Ki-1之外,2還有Ki箭頭中心位置的座標和Ki-node箭頭尖端位置的座標加上命令最後輸入的數字,該座標用於執行命令\pointKo

這個巨集\pointKo使用最後定義的Ki-nodex 座標作為自己的輸入<x coord>,這就是為什麼它沒有第一個輸入作為\pointK。此外,程式碼的棘手之處在於,\foreach循環內部的變數在每次迭代後都被完全刪除,因此當需要儲存某個變數以供下一次迭代時,它必須以前綴\global或全域定義,否則變更對於迴圈將毫無意義。

作為來自 OP 的請求,箭頭由名為 的巨集繪製,\pointK該巨集需要 5 個參數,並且\pointKo需要 4 個參數:

\pointK <x coord>,<y coord>|<h lenght>|<over dot v lenght>|<right label>.
\pointKo <y coord>|<h lenght>|<over dot v lenght>|<right label>.

將問題帶到了一個新的程度,\newpoint{<K>}{tikz style}創建了一個巨集。該宏創建了前面提到的宏,再加上一個名為的宏,該宏以字體和紅色顯示箭頭尖端處\showKs所有類型箭頭的名稱,這有助於手動繪製彎曲的連接。這是一個完整的範例,顯示了一切如何運作和輸出:K\tiny

\documentclass[11pt]{article}\usepackage{geometry,xcolor,tikz,etoolbox}\usetikzlibrary{calc}
\geometry{paperwidth=16in,paperheight=10in,left=1in,right=1in,top=1in,bottom=1in}
\colorlet{AXEScolor}{blue!64!red!32}\colorlet{LINEcolor}{red!32}
\newcount\blankcount\blankcount=0
\def\blank{\global\advance\blankcount by 1\setcounter{pointX}{-1}\setcounter{pointK}{-1}
\draw[AXEScolor,line cap=round,->](0,0)--node[left]{$a\atop b$}(0,16);
\draw[AXEScolor,line cap=round,->](0,0)--node[below]{${}\atop time$}(32,0);
\draw[LINEcolor](0,6)--(32,6)node{};
\node (x\the\blankcount) at (-1/2,-1/2) {\LARGE\textbf{\the\blankcount}};}

\tikzset{dot/.style={circle,fill,minimum size=3pt}, inner sep=0pt, outer sep=2pt} 
\newdimen\xlast\newdimen\ylast 
\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\xlast}{\ylast}} 

\newcommand*{\closerj}{} 
\newcommand*{\findcloserj}[1]{% Serves as input for second autoconnect loop (finds the closer arrow) 
    \ifnumequal{\j}{\i}{}% If is \j the same arrow as \i do nothing, else: 
    {\ExtractCoordinate{$(#1\j-1)$};\let\jtail\xlast% 
        \ExtractCoordinate{$(#1\j-2)$};\let\jhead\xlast% 
        \ifboolexpr{test {\ifboolexpr{test {\ifdimcomp{\itail}{>}{\jtail}} or test {\ifdimcomp{\itail}{=}{\jtail}}}}% If itail is after jtail 
            and% 
            test {\ifdimcomp{\itail}{<}{\jhead}}% If itail is before jhead 
        }% If both are true do: 
        {\ifdimgreater{\yi}{\ylast}% Checks if yj is above or below yi 
            {\ifdimless{\yi-\ylast}{\minvdist}{\dimgdef\minvdist{\yi-\ylast}\global\let\closerj\j}{}}% If above, checks if yi-yj < minvdist 
            {\ifdimless{\ylast-\yi}{\minvdist}{\dimgdef\minvdist{\ylast-\yi}\global\let\closerj\j}{}}}% If below, checks if yj-yi < minvdist 
        {}% If any fails, do nothing 
    }% end of if i=j 
} 

\newcommand{\newpoint}[2]{% Creates new \point#1 commands and its friends (\point#1o and \autoconnect#1) 
    \newcounter{point#1}\setcounter{point#1}{-1}\tikzset{#1/.style={#2}}% Sets counter and style of the point 
    \expandafter\def\csname point#1\endcsname ##1,##2|##3|##4|##5.{% Creates the \point#1 command which draws the arrows 
        \stepcounter{point#1}% Steps point counter 
        \draw[#1] (##1,##2) coordinate (#1\csname thepoint#1\endcsname-1) % Arrow start position 
        (##1+##3/2,##4) node[dot]{} % Goes right halfway the arrow lenght (##3/2) and ##4 up, draws the dot 
        +(##3+##5,0) coordinate (#1\csname thepoint#1\endcsname-node) % Places a coordinate ##5 in front of the arrow 
        (#1\csname thepoint#1\endcsname-1) -- node[above]{$##3$} coordinate (#1\csname thepoint#1\endcsname) ++(##3,0) node[right]{$##5$} coordinate (#1\csname thepoint#1\endcsname-2); % Draws the arrow 
    }% 
    \expandafter\def\csname point#1o\endcsname ##1|##2|##3|##4.{% Creates the variant \point#1o 
        \ExtractCoordinate{$(#1\csname thepoint#1\endcsname-node)$};% Extract last point#1-node coordinate 
        \stepcounter{point#1}% Steps the point counter 
        \draw[#1] (\xlast,##1) coordinate (#1\csname thepoint#1\endcsname-1) % Arrow x start position comes from last point#1-node coordinate 
        (\xlast+##2/2,##3) node[dot]{} % Goes right halfway the arrow lenght (##2/2) and ##3 up, draws the dot 
        +(##2+##4,0) coordinate (#1\csname thepoint#1\endcsname-node) % Places a coordinate ##4 in front of the arrow 
        (#1\csname thepoint#1\endcsname-1) -- node[above]{$##2$} coordinate (#1\csname thepoint#1\endcsname) ++(##2,0) node[right]{$##4$} coordinate (#1\csname thepoint#1\endcsname-2); % Draws the arrow 
    }% 
    \expandafter\def\csname autoconnect#1\endcsname{% Sistematically checks for the closest tail above (if any) and connects it 
        \ifnumgreater{\csname thepoint#1\endcsname}{0}{% Checks if there were more than 1 \point#1 used 
            \foreach \i in {0,...,\csname thepoint#1\endcsname}{% Loops through all arrows i of kind #1 
                \ExtractCoordinate{$(#1\i-1)$};% Gets the arrow coordinates 
                \let\itail\xlast\let\yi\ylast\dimgdef\minvdist{100cm}% 
                \foreach \j in {0,...,\csname thepoint#1\endcsname}{\findcloserj{#1}};% Loops through all other arrows besides i and retrives the closer one 
                \ifdefempty{\closerj}{}{\draw[#1] (#1\closerj-1) -| (#1\i-1) -- (#1\i-2);}% Draws the connection if it exists 
                \gdef\closerj{}% clear the variable for next iteration 
            };\setcounter{point#1}{-1}\renewcommand*{\closerj}{}% Resets the counter and clear the variable for the next loop 
        }{}}% 
    \expandafter\def\csname show#1s\endcsname{% 
        \foreach \i in {0,...,\csname thepoint#1\endcsname}{\node[above left, font=\tiny, red] at (#1\i-2) {#1\i};}; 
    }% 
}

\newpoint{X}{-latex, black} 
\newpoint{K}{-latex, gray} 

\begin{document} 
    \begin{tikzpicture} 
      \blank
      \pointX 0,2|5|4|1.
      \pointX 3,1|5|4|1.
      \pointX 1,4|6|4|3.
      \pointX 1.5,3.5|4|3|5.
      \pointX 4,2.5|4|4|0.
      \pointXo 3|2|3|0.
      \pointXo 2|2|3|1.
      \pointK 2,7|3|-2|1.
      \pointK 4,6|3|1|2.
      \pointKo 7|2|1|0.
      \pointKo 6.5|2|-1|2.
      \pointK 6,5|4|2|5.
      \pointK 10.5,4|2|-2|1.
      \pointXo 2.5|2|3|1.
      \showKs
      \showXs
      \autoconnectK
      \autoconnectX
    \end{tikzpicture} 
\end{document}

編譯輸出

答案2

您的主要問題是自動連接到最近的水平台階。這主要是演算法問題。

1)建立一個清單L,其中包含一種(X或K)步驟的所有起點和終點。 2)首先透過增加x對該清單進行排序,對於相同的x,將終點放在起點之前。 3)建立一個空列表S。此步驟c) 將起點新增至 S

每次,清單S都包含尚未完成的步驟的起點。

這是一個基本上執行此操作的程式碼。我沒有回答這個問題手動的連接。這應該是最簡單的部分。

\documentclass[11pt]{article}

\usepackage{geometry}
\geometry{paperwidth=16in,paperheight=10in,left=1in,right=1in,top=1in,bottom=1in}

\usepackage{xcolor}
\usepackage{tikz}
\usepackage{etoolbox}

\newcount\blankcount \blankcount=0

\newcount\Xsteps
\newcount\Ksteps

\def\clearsteps#1{%
  \csdef{#1points}{}%
  \csuse{#1steps}=0
}

\colorlet{Xcolor}{black}
\colorlet{Kcolor}{black!50}

%\def\pointX{\step X{\LARGE.}}
\def\pointX{\step X{$\bullet$}}
\def\pointK{\step K{o}}


% compare two points X = (n,se,x,y) and X' = (n',se',x',y')
% where:
%   n is the step number
%   se is 1 for a start point, 0 for an end point
%   x and y are the coordinates of the points
% X < X' iff x < x' or (x = x' and (se < se' or (se = se' and y < y')))

\newif\iflessthan
\def\compare(#1,#2,#3,#4)(#5,#6,#7,#8){%
  \ifdim #3pt<#7pt\relax \lessthantrue  \else
  \ifdim #3pt>#7pt\relax \lessthanfalse \else
  % both points have the same x coordinate
  % an end point is smaller than a start point with the same x coordinate
  \ifnum #2<#6\relax \lessthantrue  \else
  \ifnum #2>#6\relax \lessthanfalse \else
  % both point are either start points or end points
  % the smaller one is the one with the smaller y coordinate
  \ifdim #4pt<#8pt\relax \lessthantrue
  \else \lessthanfalse
  \fi\fi\fi\fi\fi
}

% insert the point #2=(n,se,x,y) in the list #1points: #1 = X or K
\newtoks\lsttoks
\def\insertpoint#1#2{%
  \def\lst{#1points}%
  \let\do\relax
  \lsttoks{}%
  \edef\next{\xinsertpoint{#2}\csuse\lst\do.\relax}%
  \next
}
\protected\def\xinsertpoint#1\do#2{%
  \ifx.#2% 
    % we reached the end of the list: insert #1
    % the rest is '\relax'.
    \lsttoks\expandafter{\the\lsttoks\do{#1}}%
    \let\next\finishinsert
  \else
    % the rest of the list is '\do{p}...\do{p}\do.\relax'
    \compare #1#2%
    \iflessthan 
      % (x,se,y) <_lex (x',se',y')
      % insert #1, then #2 and finish
      \lsttoks\expandafter{\the\lsttoks\do{#1}\do{#2}}%
      \let\next\xfinishinsert
    \else
      % (x,se,y) >=_lex (x',se',y')
      % insert #2 then continue
      \lsttoks\expandafter{\the\lsttoks\do{#2}}%
      \let\next\xinsertpoint
    \fi
  \fi
  \next{#1}%
}
% finish the insertion by inserting at once the rest of the list
\def\finishinsert#1\relax{\csedef\lst{\the\lsttoks}}
\def\xfinishinsert#1#2\do.\relax{\csedef\lst{\the\lsttoks #2}}

% connect steps
\def\connectlogically#1{%
  \begingroup
  \def\splist{}% list of start points of unfinished steps
  \colorlet{stepcolor}{#1color}%
  \let\do\connectpoint
  \csuse{#1points}%
  \endgroup
}

% If #1 is a start point, connect it to the nearest overlapping step, if any,
% and add #1 to \splist.
% Otherwise, #1 is an end point: remove the start point from \splist
\def\connectpoint#1{\xconnectpoint#1} % remove the braces around the point
\newif\iffound
\def\xconnectpoint(#1,#2,#3,#4){%
  \ifnum#2=1 % start step: connect to other steps and add the starting point to \splist
    \def\xdo{\yconnect(#1,#3,#4)}%
    \foundfalse
    \splist\relax
    \iffound \draw[line width=2pt, stepcolor] (#3,#4) -- (#3,\ystep); \fi
    \let\xdo\relax
    \edef\splist{\splist\xdo(#1,#3,#4)}%
  \else % end step: remove the starting point from \splist
    \def\removesp##1\xdo(#1,##2,##3)##4\relax{\def\splist{##1##4}}%
    \expandafter\removesp\splist\relax
  \fi
}

\newdimen\yabsdiff
\newdimen\newyabsdiff
\newif\ifnewy

\iftrue

% Search for a step to connect to

% version for connecting only steps with different starting points
\def\yconnect(#1,#2,#3)(#4,#5,#6){%
  \newyfalse
  \ifdim#5pt<#2pt % the step #4 starts strictly before the step #1 and ends after #2
    \newyabsdiff=\dimexpr#3pt-#6pt\relax
    \ifdim\newyabsdiff<0pt \newyabsdiff=-\newyabsdiff\fi
    \newytrue
    \iffound
      \ifdim\newyabsdiff<\yabsdiff \else
        \newyfalse
      \fi
    \fi
    \foundtrue
  \fi
  \ifnewy
    \yabsdiff=\newyabsdiff
    \def\ystep{#6}%
  \else
    % If there are some start points left in \splist, they all have the current x coordinate.
    % The corresponding steps cannot overlap the current point.
    % We can discard all the remaining points.
    \expandafter\endconnect
  \fi
}

\else

% version for connecting steps with identical starting points
\def\yconnect(#1,#2,#3)(#4,#5,#6){%
%  \ifdim#5pt<#2pt % the step #4 starts strictly before the step #1 and ends after #2
    \newyabsdiff=\dimexpr#3pt-#6pt\relax
    \ifdim\newyabsdiff<0pt \newyabsdiff=-\newyabsdiff\fi
    \newytrue
    \iffound
      \ifdim\newyabsdiff<\yabsdiff \else
        \newyfalse
      \fi
    \fi
    \foundtrue
%  \fi
  \ifnewy
    \yabsdiff=\newyabsdiff
    \def\ystep{#6}%
  \else
    % If there are some start points left in \splist, they all have the current x coordinate.
    % We already found a step to connect to and the new one is further:
    % The other ones are even further because we sorted them in increasing y's.
    % We can discard all the remaining points.
    \expandafter\endconnect
  \fi
}

\fi

% discards all remaining points in \splist
\def\endconnect#1\relax{}

\def\blank{%
    \global\advance\blankcount by 1
    \clearsteps X%
    \clearsteps K%
    \draw[black!64,->](0,0)--node[left]{$x\atop y$} (0,8);
    \draw[black!32](0,3)--(16,3)node{};
    \draw[black!64,->](0,0)--node[below]{$a\atop b$} (16,0);
    % I didn't understand {$\csname num:\the\blankcount\endcsname$}.
    % Is it defined somewhere else?
    \node (x\the\blankcount) at (16,8) {\the\blankcount};
}

% Draw a step and add the start point and the end point to the list #1points
% Note: an additional parameter should be supplied for the textual form of
% the label above the edge.
\def\step #1#2#3,#4|#5|#6|#7.{%
   \advance\csuse{#1steps} by 1
   \edef\stepnum{\the\csuse{#1steps}}
   \draw [line width = 2pt, ->, #1color] 
     (#3,#4) node (#1start\stepnum) {} 
     -- node[above] {$#5$} 
     ++(#5,0) node[right] (#1end\stepnum) {$#7$};
   \pgfmathparse{#3+.5*#5} \let\X\pgfmathresult
   \pgfmathparse{#4+#6} \let\Y\pgfmathresult
   \node at (\X,\Y) {#2};
   \pgfmathparse{#3+#5}
   \insertpoint{#1}{(\stepnum,1,#3,#4)}
   \insertpoint{#1}{(\stepnum,0,\pgfmathresult,#4)}
}% step

\begin{document}
\begin{tikzpicture}
\blank
\pointX 0,2|5|4|1.
\pointX 3,1|5|4|1.
\pointX 1,4|6|4|3.
\pointX 1.5,3.5|4|3|5.
\pointX 4,2.5|4|4|2.
\pointK 2,7|3|2|1.
\pointK 4,6|3|1|2.
\pointK 6,5|4|2|5.
\connectlogically X
\connectlogically K
\end{tikzpicture}

\end{document}

在此輸入影像描述

相關內容