使线延伸到端点之外并与线相交

使线延伸到端点之外并与线相交

是否有可能让一条线穿过两个坐标,然后延伸直到与另一条线相交?所讨论的线既不是水平的也不是垂直的(否则我可以使用坐标系perpendicular)。

我现在的代码是

\begin{tikzpicture}

\coordinate[label={left:$(x, y, z)$}] (w) at (-6,2);
\coordinate (c) at (0,0);
\coordinate[label={right:$(x,y)$}] (b) at (3,-1);
% I want to avoid hard-coding this coordinate

\draw[ultra thick,-stealth] (w |- c) -- (w);
\draw[ultra thick,-stealth] (c -| b) -- (b);

\draw   (c) +(0,1.5) -- ++(0,-1.5);

\draw[dashed] (w |- c) -- (c -| b);
\draw[dashed] (w) -- (c); % Currently two paths to emulate the result
\draw[dashed] (c) -- (b);

\end{tikzpicture}

因此,我想要指定一条经过点w和的路径,c并延伸直到它与 (假想的) 垂直线相交(3,y)。我不想对点的位置进行硬编码b,而是让 tikz 帮我计算。

答案1

\pgfmathsetmacro您可以借助和库来解决(非常简单的)线性方程calc

在此处输入图片描述

代码:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
  % initial points: a, b, c
  \fill
  (-6,2) coordinate (a) circle(1pt) node[above]{a}
  (1,-1) coordinate (b) circle(1pt) node[above right]{b}
  (3,0)  coordinate (c) circle(1pt) node[above right]{c};

  % d intersects the line through (a) and (b) and the vertical line through (c)
  \path let \p1=(a), \p2=(b), \p3=(c) in
  \pgfextra{\pgfmathsetmacro{\y}{(\y1-\y2)/(\x1-\x2)*(\x3-\x2)+\y2}}
   (c |- 0,\y pt) coordinate (d) node[below]{d};

  \draw[ultra thick,-stealth] (a |- b) -- (a);
  \draw[ultra thick,-stealth] (b -| d) -- (d);
  \draw   (b) +(0,1.5) -- ++(0,-1.5);
  \draw[dashed] (a |- b) -- (b -| d);
  \draw[dashed] (a) -- (d);
\end{tikzpicture}
\end{document}

答案2

您也可以使用,to path尽管我没有彻底测试过,所以这可能会在某些非常尖锐的角度下发生故障,或者无法通过给定的坐标到达目标,例如,通过坐标在左侧,而目标在右侧相对于给定的起点等。

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
    passthrough/.style={
        to path={ let \p1=($ #1 - (\tikztostart)$),
        \p2=($ (\tikztotarget) - #1 $),
        \n1={atan2(\x1,\y1)},
        \n2={veclen(\x1,\y1) + veclen(\x2,0)/cos(\n1)}
        in (\tikztostart) -- ++(\n1:\n2)
        },
    }
}

\begin{document}
\begin{tikzpicture}

\coordinate[label={left:$(x, y, z)$}] (w) at (-6,2);
\coordinate (c) at (0,0);
\draw   (c) ++(0,1.5) -- ++(0,-3);

\draw[ultra thick,-stealth] (w |- c) -- (w);



\draw[line width=2mm,red] (w) to[passthrough=(c)] (4,0);
\draw[line width=1mm,blue] (w) to[passthrough=(c)] (3,0);
\draw[dashed] (-6,0) -- (3,0);     % Horizontal Test line for blue
\draw[dashed] (-6,-1) -- (4,-1);   % Horizontal Test line for red


\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容