# Draw line from a point to another line (along x-axis or y-axis) in TikZ

I would like to make use of the intersection tools to draw a line to another line in a way that it only reaches along the x-axis or y-axis. I mean I currently think that I would achieve this via the intersection part... but I'm not entirely sure on that. I hope the attached picture clears it up properly. Is this possible?

Note: The coordinate values are only that simple for the sake of demonstration. edit: Edited with better code and screenshot to clarify the problem, a thank you to Jake.

MWE

    \documentclass[
a4paper
]{scrartcl}

\usepackage{
lmodern,
}
\usepackage[T1]{fontenc}
\usepackage{
tikz
}
\usetikzlibrary{calc}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
%
\draw[style=help lines,step=0.5cm] (0,0) grid (6.2,6.2);
%
\draw[->,thick] (-0.1,0) -- (6.5,0) node[anchor=west]{x};
\draw[->,thick] (0,-0.1) -- (0,6.5) node[anchor=south]{y};
%
\foreach \x in {0,1,...,6} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
\foreach \y in {0,1,...,6} \draw [thick](-2pt,\y) -- (2pt,\y);
%
\foreach \x in {0,1,...,6} \draw (\x cm, 0 cm) node[anchor=north]{\x};
\foreach \y in {0,1,...,6}  \draw (0 cm, \y cm) node[anchor=east]{\y};
%
\begin{scope}[color=black]
\filldraw (1,1) circle (0.08cm) node (A) {} node[anchor=north,fill=white,yshift=-0.1cm] {A};
\filldraw (6,6) circle (0.08cm) node (B) {} node[anchor=west,fill=white,xshift=5pt] {B};
\filldraw (4,2) circle (0.08cm) node (C) {} node[anchor=south,fill=white,yshift=0.1cm] {C};
\end{scope}
\draw[very thick] (A) -- (B);
\draw[->,>=latex,very thick,dashed] (C.center) -- ($(A)!(C)!(B)$);
\draw (2,5) node[fill=white] {\textcolor{red}{from C\ldots}};
\draw[color=red,very thick] (2,2) circle (0.1cm) node[above,yshift=5pt,fill=white]{to this point};
\draw[color=red,very thick] (4,4) circle (0.1cm) node[below,yshift=-5pt,fill=white]{or this one};
%\node (Ex) at (....)?
%\draw[->,>=latex',very thick,dashed] (E.center) -- ($(A)!(E.135)!(B)$);
\end{tikzpicture}
\end{center}
\end{document}

• Do I understand that right, you want to draw a horizontal or vertical line starting from (C) and ending at the diagonal line?
– Jake
Jun 24 '13 at 13:08
• @Jake Yes, exactly, you understood right. Jun 24 '13 at 13:11

After loading the intersections library, you name the involved paths using name path=<name>, then you can use the name intersections={<options>} key to find intersection points (details in Section 13.3.2 Intersections of Arbitrary Paths of the pgf manual).

\documentclass[a4paper]{scrartcl}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}

\begin{document}

\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
%
\draw[style=help lines,step=0.5cm] (0,0) grid (6.2,6.2);
%
\draw[->,thick] (-0.1,0) -- (6.5,0) node[anchor=west]{x};
\draw[->,thick] (0,-0.1) -- (0,6.5) node[anchor=south]{y};
%
\foreach \x in {0,1,...,6} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
\foreach \y in {0,1,...,6} \draw [thick](-2pt,\y) -- (2pt,\y);
%
\foreach \x in {0,1,...,6} \draw (\x cm, 0 cm) node[anchor=north]{\x};
\foreach \y in {0,1,...,6}  \draw (0 cm, \y cm) node[anchor=east]{\y};
%
\begin{scope}[color=black]
\filldraw (1,1) circle (0.08cm) node (A) {} node[anchor=north,fill=white,yshift=-0.1cm] {A};
\filldraw (6,6) circle (0.08cm) node (B) {} node[anchor=west,fill=white,xshift=5pt] {B};
\filldraw (4,2) circle (0.08cm) node (C) {} node[anchor=south,fill=white,yshift=0.1cm] {C};
\end{scope}
\draw[name path=diagonal,very thick] (A) -- (B);
\draw[->,>=latex,very thick,dashed] (C.center) -- ($(A)!(C)!(B)$);
\draw (2,5) node[fill=white] {\textcolor{red}{from C\ldots}};
\draw[color=red,very thick] (2,2) circle (0.1cm) node[above,yshift=5pt,fill=white]{to this point};
\draw[color=red,very thick] (4,4) circle (0.1cm) node[below,yshift=-5pt,fill=white]{or this one};

\path[name path=line1] (C) -- +(-3,0);
\path[name path=line2] (C) -- +(0,3);
\draw[thick,blue,name intersections={of=diagonal and line1,by={Int1}}] (C) -- (Int1);
\draw[thick,green,name intersections={of=diagonal and line2,by={Int2}}] (C) -- (Int2);
\end{tikzpicture}
\end{center}
\end{document}


• Oh, now I see how I could have used that. Thank you for showing me. This is the code I am going to use. Jun 24 '13 at 16:36

These points are best found via an intersection. I used the intersection of syntax (which is a wrapper for the intersection cs). The |-/-| short hands are used to find a coordinate in the perpendicular cs.

I took the liberty to clean up a little bit in the code and use styles. Also the labels A, B, and so on are in fact labels.

## Code

\documentclass[tikz,convert=false]{standalone}
\usepackage{lmodern}\usepackage[T1]{fontenc}
\usetikzlibrary{arrows}
\tikzset{
dot/.style={shape=circle,inner sep=+0pt,minimum size=+1.6mm,label={#1}},
dot/.default=,
dot*/.style={dot={#1},fill=black},
dot*/.default=,
doto/.style={dot={#1},draw=red,solid,thick},
doto/.default=,
shorten/.style={shorten >={#1},shorten <={#1}}
}
\begin{document}
\begin{tikzpicture}[font=\sffamily\small]
\draw[style=help lines,step=0.5cm] (0,0) grid (6.2,6.2);

\draw[->,thick] (-0.1,0) -- (6.5,0) node[right]{x};
\draw[->,thick] (0,-0.1) -- (0,6.5) node[left] {y};

\foreach \x in {0,1,...,6} {
\draw [thick](\x,-2pt) -- (\x,2pt) node[midway,below] {\x};
\draw [thick](-2pt,\x) -- (2pt,\x) node[midway,left]  {\x};
}

\node[dot*=below:A] (A) at (1,1) {};
\node[dot*=right:B] (B) at (6,6) {};
\node[dot*=right:C] (C) at (4,2) {};

\draw[very thick,shorten=+3pt] (A) -- (B);

\node[doto=left:C(A)] (C'A) at (intersection of C--A|-C and A--B) {};
\node[doto=left:C(B)] (C'B) at (intersection of C--B-|C and A--B) {};

\path[-latex',very thick, dashed] (C) edge (C'A) edge (C'B);
\end{tikzpicture}
\end{document}


## Output

• Hm... just for the record: this worked, but can't mark more posts than one as a a solution (still a bit new to TeX.sx). So thank you. But your advanced reply actually is of some sort disadvantage to me as I do not follow your definitions of all those dot styles (-> if I were to use this code, I couldn't edit it properly to produce the right output :(). Thank you for cleaning up my code as well. Jun 24 '13 at 16:32
• @henry Well, that doesn’t change anything. Even with your code the coordinates (intersection of C--A|-C and A--B) and (intersection of C--B-|C and A--B) still result in the same coordinates on the line from A to B. What you do with these coordinates I leave to you. (This works only with straight lines, otherwise use the intersections library.) Jun 24 '13 at 16:47
• @henry If you’re willing to show what your intended output is, I’ll give some ideas on how to use it (better?). I find it very confusing that you draw a circle, then place an empty (and rectangular!) node at the some place (that is later referenced) and than manually (xshift/yshift) place another node to label that dot. The same can achieved with one node with an outer sep (or shorten paths as in my example) and a label. I think this is clearer then your code but you have by no means to use this code. :) The bigger the MWE, the more I’ll invest in what results in what. Jun 24 '13 at 16:47
• 1st:Why did the damn auto-completion of your name not work. 2nd:In my first comment I meant to say that I accepted Gonzalo's code as a solution as I find it quite easier to understand. 3rd:My code is that way just for the sake of display. 4th: As for shifting nodes as labes of other nodes, this is necessary for finetuning imho. I rather do it this way, otherwise I'd do it with the \foreach-command (provided by you guys). But thanks for offering help :) Jun 24 '13 at 17:00
• @henry 1st: Because you and me are the only users commenting here and I am the author of this answer, so I get notified anyway. 2nd, 3rd and 4th: No hard feelings, I’m sure somebody will find my answer helpful. I only hope that you do not make yourself more work than needed. Jun 24 '13 at 17:17

With PSTricks. It just focuses on the part in question. As the function is linear, no need to use intersection technique.

## Remarks:

• the left figure is created by specifying the points L and R first. Then C is constructed from L and R.

• the right figure is created by specifying the point C first.

\documentclass[crop,border=12pt]{standalone}
\usepackage{pst-plot,pst-eucl}
\psset{algebraic,saveNodeCoors}

\def\f{x+.5}
\def\g{y-.5}% inverse of \f

\begin{document}

% C is defined last
\begin{pspicture}[showgrid](-1,0)(4,4)
\pstGeonode[PointName={none,none,default},PointSymbol={none,none,default}]
(*.5 {\f}){L}
(*2 {\f}){R}
(R|L){C}
\psline[linestyle=dashed](L)(C)(R)
\psplot[linecolor=blue]{0}{3}{\f}
\end{pspicture}

\hspace{12pt}

% C is defined first
\begin{pspicture}[showgrid](-1,0)(4,4)
\pstGeonode(2,1){C}
\psline[linestyle=dashed]
(**{\g} N-C.y)
(C)
(*N-C.x {\f})
\psplot[linecolor=red]{0}{3}{\f}
\end{pspicture}

\end{document}


• Thank you for providing this code, it is useful for users of PSTricks. Jun 24 '13 at 16:38