从节点画垂直线直到与水平线相交

从节点画垂直线直到与水平线相交

请考虑此代码

\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}

相关内容