7

I am trying to create parallel lines in tikz. Although I can create those lines, I can't seem to be able to control the length of those lines, which I want them to stop in the red box's face.

My code is

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

My output is

enter image description here

Any idea on how to control the length and end point of the parallel line?

2 Answers 2

11

Assuming the parallel lines are orthogonal to the "detector" then the projection modifiers can be used (see "The Syntax of Projection Modifiers" in the manual), which are drawn in blue below. I've left the original dashed lines in for comparison.

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

enter image description here

1
  • 1
    Except the foreach part, I managed to write the identical code. But too slow :P
    – percusse
    Aug 19, 2014 at 16:28
4

For a dirty and quick fix, I would suggest using polar coordinates to keep the lines parallel. Then you can use the second parameter to approximate the needed length to hit the detector:

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

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .