# Line to intersection in TikZ

I want to reproduce the following figure in TikZ:

The two vectors are $(-1/5, 2/5), (2/5, 1/5)$. Is there a way to have TikZ draw the dotted lines till the intersection with the thick outer box, and avoid having to actually calculate the intersection points and explicitly input the nodes? So far I have:

\begin{tikzpicture}
\node (a) at (-1, 2) {};
\node (b) at (2, 1) {};

\draw [very thick] (-5, 0) -- (5, 0);
\draw [very thick] (-5, -5) -- (5, -5);
\draw [very thick] (-5, 5) -- (5, 5);

\draw [very thick] (-5, -5) -- (-5, 5);
\draw [very thick] (0, -5) -- (0, 5);
\draw [very thick] (5, -5) -- (5, 5);

\draw [very thick, ->] (0, 0) -- (a);
\draw [very thick, ->] (0, 0) -- (b);

\node[label=right:{$1$}] at (5,0) {};
\node[label=above:{$1$}] at (0,5) {};
\end{tikzpicture}

which produces

• I'd simply clip the image Sep 14 at 16:55

Probably you'd get a better result using polar coordinates (and especially not using node to define points a and b, but coordinate instead).

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}

\begin{scope}
\clip (-5,-5) rectangle (5,5);
\draw[rotate=30,dotted,orange,cap=round] (-10,-10) grid[step=2cm] (10,10);
\end{scope}
\coordinate (a) at (30:2) {};
\coordinate (b) at (120:2) {};

\draw [very thick]  (-5, -5) rectangle (5, 5)
(-5,0) -- (5,0) node[right] {$1$}
(0,-5) -- (0,5) node [above] {$1$};

\draw [very thick, ->] (0, 0) -- (a);
\draw [very thick, ->] (0, 0) -- (b);

\end{tikzpicture}
\end{document}
• The vectors are no longer at the coordinates given in the question. atan(1/2)=26.57° Sep 14 at 17:15
• Yes, I know, and your version is more accurate on this point, but it was not clear if their coordinates were mandatory. The whole thing gives the idea. Sep 14 at 17:27

Here is a solution using grid:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (-1, 2);
\coordinate (b) at (2, 1);
\draw [very thick, ->] (0, 0) -- (a);
\draw [very thick, ->] (0, 0) -- (b);
\node[label=right:{$(1,0)$}] at (5,0) {};
\node[label=above:{$(0,1)$}] at (0,5) {};
\draw[very thick, step=5] (-5,-5) grid (5,5);
\clip (-5,-5) rectangle (5,5);
\draw[dashed, very thick, step=sqrt(5), rotate=atan(1/2)] (-10,-10) grid (10,10);
\end{tikzpicture}
\end{document}

• dashed can be replaced by dotted Sep 14 at 17:17