請考慮這段程式碼
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,intersections}
\begin{document}
\begin{tikzpicture}[node distance=-\pgflinewidth]
\node[fill=black] (a) {};
\node[fill=black, xshift=\linewidth] (c) {};
\node[xshift=\linewidth*0.7] (b) {...};
\draw [name path=line1] (a) -- (b);
\draw (b) -- (c);
\node[draw, rectangle, above=of a, yshift=0.5cm, xshift=1cm] (foo1) {Foo 1};
\node[draw, rectangle, right=0.5cm of foo1] (foo2) {Foo 2};
\draw (foo1) -- TODO;
\draw (foo2) -- TODO;
\end{tikzpicture}
\end{document}
我無法從節點 foo1 和 foo2 向下繪製一條垂直直線到路徑 line1。我不想給出絕對座標。我試圖用交叉點來解決這個問題,但由於我的起始位置是一個節點,這似乎不起作用。有什麼建議麼?
答案1
像這樣?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,intersections}
\begin{document}
\begin{tikzpicture}[node distance=-\pgflinewidth]
\node[fill=black] (a) {};
\node[fill=black, xshift=\linewidth] (c) {};
\node[xshift=\linewidth*0.7] (b) {...};
\draw [name path=line1] (a) -- (b);
\draw (b) -- (c);
\node[draw, rectangle, above=of a, yshift=0.5cm, xshift=1cm] (foo1) {Foo 1};
\node[draw, rectangle, right=0.5cm of foo1] (foo2) {Foo 2};
\path[name path=fl1] (foo1.270)coordinate(f1)--++(-90:2);
\path[name path=fl2] (foo2.270)coordinate(f2)--++(-90:2);
\path[name intersections={of=fl1 and line1,by={a}}];
\path[name intersections={of=fl2 and line1,by={b}}];
\draw (f1)--(a);
\draw (f2)--(b);
% \draw (foo1) -- TODO;
% \draw (foo2) -- TODO;
\end{tikzpicture}
\end{document}
答案2
TikZ 可讓您定位位於穿過的水平線a
和您正在尋找的垂直線的交點處的點foo1
是語法(foo1|-a)
或兩者都與語法(a-|foo1)
。
我引用TikZ 3.1.4手冊
一般來說, 的意思
(p |- q)
是p
垂直線通過與水平線通過的交點q
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,intersections}
\begin{document}
\begin{tikzpicture}[node distance=-\pgflinewidth]
\node[fill=black] (a) {};
\node[fill=black, xshift=\linewidth] (c) {};
\node[xshift=\linewidth*0.7] (b) {...};
\draw [name path=line1] (a) -- (b);
\draw (b) -- (c);
\node[draw, rectangle, above=of a, yshift=0.5cm, xshift=1cm] (foo1) {Foo 1};
\node[draw, rectangle, right=0.5cm of foo1] (foo2) {Foo 2};
\draw (foo1) -- (a-|foo1);
\draw (foo2) -- (a-|foo2);
\end{tikzpicture}
\end{document}