[Tex/LaTex] Connecting to a node towards a point other than the center

tikz-pgf

I'm trying to connect an edge to a node with the target point being off-center. I would like the edge to end with an arrow at the border of the node without going into the box.

It is easy to connect an edge to a side of a node:

\begin{tikzpicture}
\node (x) [draw,minimum size=1 cm] at (0,0) {x};
\node (y) [draw,minimum size=1 cm] at (2 cm,2 cm) {y};
\draw[->] (x) -- (y.west);
\end{tikzpicture}

But drawing to halfway between (x) and (x.west) continues into the node instead of stopping at the border:

\begin{tikzpicture}
\node (x) [draw,minimum size=1 cm] at (0,0) {x};
\node (y) [draw,minimum size=1 cm] at (2 cm,2 cm) {y};
\draw[->] (x) -- ($ (y.west)!0.5!(y) $);
\end{tikzpicture}

How can I make an edge with the same angle as (x) -- ($ (y.west)!0.5!(y) $) but which draws the arrow right at the border instead of entering into the node?

Best Answer

With the TikZ library intersections you can find the point on the node's rectangle quite easily.

We give the y node a path name: yborder.
We use one \path command to get the path from (x) to ($ (y.west)!0.5!(y) $) and name it xyline.

With name intersections={of=yborder and xyline} we get coordinates named intersections-1, intersections-2, …
In our case there's only one intersection, so intersection-1 will do.

(The PGF/TikZ manual shows how to deal with path intersections with more than one hit.)

Code

\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{calc,intersections}
\begin{document}
\begin{tikzpicture}[every node/.style={draw,minimum size=1 cm}]
    \node                     (x) at (0cm,0cm) {x};
    \node [name path=yborder] (y) at (2cm,2cm) {y};
    \path [name path=xyline ] (x) -- ($ (y.west)!0.5!(y) $);
    \fill [red]               ($(y.west)!0.5!(y)$) circle[radius=1pt]; % Where's my point?
    \draw [name intersections={of=yborder and xyline},->]
                              (x) -- (intersection-1);
\end{tikzpicture}
\begin{tikzpicture}[every node/.style={circle,draw,minimum size=1 cm}]
    \node                     (x) at (0cm,0cm) {x};
    \node [name path=yborder] (y) at (2cm,2cm) {y};
    \path [name path=xyline ] (x) -- ($ (y.west)!0.5!(y) $);
    \fill [red]               ($(y.west)!0.5!(y)$) circle[radius=1pt]; % Where's my point?
    \draw [name intersections={of=yborder and xyline},->]
                              (x) -- (intersection-1);
\end{tikzpicture}
\end{document}

Output

rectanglescircles