我正在尝试创建平行线tikz
。虽然我可以创建这些线,但我似乎无法控制这些线的长度,我希望它们停在红色框的表面上。
我的代码是
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.5]
%target
\draw[fill=gray!30,gray!30] (0,3)rectangle(2,-3);
%tracks
\draw[->,thick] (-2,2)--(0,2);
\draw[->,dashed] (0,2)--(-3,-3);
\draw[->,thick] (-2,1)--(2,1);
\draw[->,dashed] (2,1)-- +($(-3,-3)-(0,2)$);
\draw[->,thick] (-2,-1)--(2,-1);
\draw[->,dashed] (2,-1)-- +($(-3,-3)-(0,2)$);
\draw[->,thick] (-2,-2)--(0,-2);
\draw[->,dashed] (0,-2)-- +($(-3,-3)-(0,2)$);
%detector
\draw[red, rotate around={60:(-1,-3)}] (-3,0) rectangle (-2,-6);
\end{tikzpicture}
\end{document}
我的输出是
关于如何控制平行线的长度和终点有什么想法吗?
答案1
假设平行线与“探测器”正交,则可以使用投影修改器(请参阅手册中的“投影修改器的语法”),它们在下面以蓝色绘制。我保留了原始虚线以供比较。
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[scale=0.5]
%target
\draw[fill=gray!30,gray!30] (0,3) rectangle(2,-3);
%detector
\draw[red, rotate around={60:(-1,-3)}]
(-3,0) rectangle (-2,-6)
(-2,0) coordinate (a)
(-2,-6) coordinate (b);
%tracks
\draw[->,thick] (-2,2)--(0,2);
\draw[->,dashed] (0,2)--(-3,-3);
\draw[->,thick] (-2,1)--(2,1);
\draw[->,dashed] (2,1)-- +($(-3,-3)-(0,2)$);
\draw[->,thick] (-2,-1)--(2,-1);
\draw[->,dashed] (2,-1)-- +($(-3,-3)-(0,2)$);
\draw[->,thick] (-2,-2)--(0,-2);
\draw[->,dashed] (0,-2)-- +($(-3,-3)-(0,2)$);
\draw [blue, solid, ->] (0,2) -- ($(a)!(0,2)!(b)$);
\draw [blue, solid, ->] (2,1) -- ($(a)!(2,1)!(b)$);
\draw [blue, solid, ->] (2,-1) -- ($(a)!(2,-1)!(b)$);
\draw [blue, solid, ->] (0,-2) -- ($(a)!(0,-2)!(b)$);
\end{tikzpicture}
\end{document}
答案2
对于一个粗略而快速的解决方法,我建议使用极坐标来保持线平行。然后,您可以使用第二个参数来近似计算击中检测器所需的长度:
\begin{tikzpicture}[scale=0.5]
%target
\draw[fill=gray!30,gray!30] (0,3)rectangle(2,-3);
%tracks
\draw[->,thick] (-2,2)--(0,2);
\draw[->,dashed] (0,2)--+(240:5.8);
\draw[->,thick] (-2,1)--(2,1);
\draw[->,dashed] (2,1)-- +(240:5.9);
\draw[->,thick] (-2,-1)--(2,-1);
\draw[->,dashed] (2,-1)-- +(240:4.2);
\draw[->,thick] (-2,-2)--(0,-2);
\draw[->,dashed] (0,-2)-- +(240:2.3);
%detector
\draw[red, rotate around={60:(-1,-3)}] (-3,0) rectangle (-2,-6);
\end{tikzpicture}