画一条线(A),然后从线 A 的中间开始画另一条线

画一条线(A),然后从线 A 的中间开始画另一条线

我对一个非常基本的问题感到困惑。我的目标是画一个支架,但要让支架看起来是矩形

我从起点 A 到终点 B 绘制一条路径/线,并在起点和终点处绘制一些线以形成括号的末端。现在我想在括号的中心绘制一条线,如下所示:

在此处输入图片描述

为了实现这一点,我使用一种非常简单的形式在路径中间创建一个节点,并创建另一个具有负 xshift 的节点,然后绘制一条连接这两个节点的路径:

\documentclass{article}
\usepackage{tikz,amsmath, amssymb,bm,color}
\usepackage[active,tightpage]{preview}
\usepackage[margin=0cm,nohead]{geometry}
\usetikzlibrary{shapes,arrows}
\usepackage{tikz-3dplot}
% needed for BB
\usetikzlibrary{calc}
\PreviewEnvironment{tikzpicture}
\begin{document}

\begin{tikzpicture}

\draw(-.7,0,3) to (-1,0,3) to node (mid){mid} node [xshift=-3cm](midleft) 
{midleft} (-1,3,3) to (-.7,3,3);
\draw (mid.center) to (midleft);

\end{tikzpicture}

\end{document}

有没有更简单的替代方法来绘制路径并通过类似 path.center 的方式“使用”该路径的中心点?提前致谢。

答案1

不确定这是否是您所需要的,但这里有一种使用calc库或通过节点的明确定位来计算中间点的方法。

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}

\begin{document}

% Solution 1 with calc
\begin{tikzpicture}
\coordinate (A) at (-0.7,0,3);
\coordinate (B) at (-0.7,3,3);
\coordinate (M) at ($(A)!0.5!(B)+ (-0.3,0)$);
\coordinate (L) at ($(M)+ (-0.5,0) $);
\draw (A) --++ (-0.3,0,0) -- (M) ;
\draw (B) --++ (-0.3,0,0) -- (M);
\draw (M) -- (L);
\end{tikzpicture}

% Solution 2 with midway node positioning
\begin{tikzpicture}
\coordinate (A) at (-0.7,0,3);
\coordinate (B) at (-0.7,3,3);
% Option pos=0.5 can be replaced by midway
\draw (A) --++ (-0.3,0,0) --++ (0,3,0)  node[pos=0.5,inner sep=0pt,outer sep=0pt](M){} -- (B);
\draw (M) --++ (-0.5,0,0);
\end{tikzpicture}
\end{document}

相关内容