25

How to draw a line passing through a point and parallel to another?

\draw (0,0) -- (1,0);
\node (A) at (1,1);

I need \draw (0,1) -- (1,1); parallel to first line.

Regis Santos
  • 14,463

1 Answers1

40

You can use the calc library to calculate the vector between the first two points. By using the + path operator, you can draw a line from a third point in the direction of this vector:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
  extended line/.style={shorten >=-#1,shorten <=-#1},
  extended line/.default=1cm]

\node [dot=A] at (2,1) {};
\node [dot=B] at (3,2) {};
\node [dot=P] at (2,2) {};

\draw [extended line=0.5cm] (A) -- (B);
\draw [extended line=0.5cm] (P) -- +($(B)-(A)$);

\end{tikzpicture}
\end{document}

parallel lines with TikZ

Jake
  • 232,450
  • I found the same output can be found using ++ instead of +. Is there any technical difference between both cases? – Diaa Apr 03 '17 at 03:42
  • 1
    @DiaaAbidou yes: "instead of two plus signs, you can also add a single one. This also specifies a point in a relative manner, but it does not “change” the current point used in subsequent relative commands. For example, (1,0) +(1,0) +(0,1) specifies the three coordinates (1,0), then (2,0), and (1,1)." On the other hand, "(1,0) ++(1,0) ++(0,1) specifies the three coordinates (1,0), then (2,0), and (2,1)." (tikz documenation page 119) – jakun Apr 09 '17 at 06:32
  • @Jake I am not familiar with ($(B)-(A)$); what is that for? thks. – mario Jun 10 '17 at 18:03
  • @mario ($(B)-(A$) is a calculated coordinate, $..$ is calculation mode in tikz, loaded by calc library. In it you can perform basic coordinate calculations such as the one presented and some more daring too. ($(B)-(A)$) is literaly (B) coordinate minus (A) coordinate, in geometry that gives the vector pointing from A to B, that's why the line P is drawn is parallel to (A)--(B). – Guilherme Zanotelli Nov 27 '18 at 16:52