21

This provides good details on How to draw a line passing through a point and perpendicular to another?. What I would like to do is to extend this somehow to add the usual geometry symbol to indicate that the two lines are indeed perpendicular. I could code a manual solution for each situation, but would like a macro, one that would draw it of an appropriate size, and allow me to choose the orientation (i.e., select one of the four quadrants) for this symbol.

Peter Grill
  • 223,288

2 Answers2

29

Here's one possible approach that uses a TikZ style. You insert the angle symbol by using the style

right angle symbol={<Point 1>}{<Point 2>}{<Point 3>}

in a draw command, where <Point 1> and <Point 2> are two points on a line (A and B in the image below), and <Point 3> is a point on the perpendicular line (Q or P in the image below). If you want the angle symbol on its own, just use it in a new draw command:

\draw [right angle symbol={<Point 1>}{<Point 2>}{<Point 3>}];

The quadrant can be selected by using right angle quadrant=<1-4>, the size by using right angle length=<length>. Both these options have to be called before right angle symbol.

right angles with symbols in TikZ

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

\tikzset{
    right angle quadrant/.code={
        \pgfmathsetmacro\quadranta{{1,1,-1,-1}[#1-1]}     % Arrays for selecting quadrant
        \pgfmathsetmacro\quadrantb{{1,-1,-1,1}[#1-1]}},
    right angle quadrant=1, % Make sure it is set, even if not called explicitly
    right angle length/.code={\def\rightanglelength{#1}},   % Length of symbol
    right angle length=2ex, % Make sure it is set...
    right angle symbol/.style n args={3}{
        insert path={
            let \p0 = ($(#1)!(#3)!(#2)$) in     % Intersection
                let \p1 = ($(\p0)!\quadranta*\rightanglelength!(#3)$), % Point on base line
                \p2 = ($(\p0)!\quadrantb*\rightanglelength!(#2)$) in % Point on perpendicular line
                let \p3 = ($(\p1)+(\p2)-(\p0)$) in  % Corner point of symbol
            (\p1) -- (\p3) -- (\p2)
        }
    }
}

\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 (0,0) {};
\node [dot=B] at (3,1) {};
\node [dot=P] at (0.9,-1.2) {};
\node [dot=Q] at (1.3,2.2) {};

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


\draw [red,right angle symbol={A}{B}{P}];

\draw [extended line,right angle quadrant=3,right angle symbol={A}{B}{Q}] ($(A)!(Q)!(B)$) -- (Q);
\end{tikzpicture}
\end{document}
Jake
  • 232,450
  • @Altermundus makes a good point about right angle symbol={A}{P}{B}. A better method syntax might be right angle symbol={A}{B}{P} to specify which angle to place the symbol on. – Peter Grill Jun 29 '11 at 22:03
  • @Peter: The original order reflected the syntax of the calc syntax that is used to calculate the projection of P on AB. I've changed it to {A}{B}{P} now. – Jake Jun 29 '11 at 23:02
  • I get an error latexing this exact file: `! Argument of \pgfmath@afterquick has an extra }. \par l.21 } ` The braces seem balanced though. – Liam Jan 14 '13 at 16:00
  • @Liam: It compiles fine for me. Are you sure you copied the code correctly? Also, are you using the current version of PGF (v. 2.1)? – Jake Jan 14 '13 at 16:04
  • @Jake Package: pgf 2008/01/15 v2.00 (rcs-revision 1.12) from \pgfversion in Debian stable. – Liam Jan 14 '13 at 16:22
  • @Liam: You should upgrade your PGF version, the step from 2.0 to 2.1 was a very substantial one, many essential functions weren't present in 2.0. – Jake Jan 14 '13 at 16:23
  • Can you explain in the answer in plain English how to interpret point1, point2 and point3? It took me ages to find out... – Calmarius Aug 16 '15 at 12:05
  • @Calmarius: Done. – Jake Aug 16 '15 at 12:36
12

Another possibility is to use my package tkz-euclide (now on ctan and texlive 2011) but you don't need to get all the objects that I defined. Only angles are necessary. I take the Jake's example to show you that you can mix tikz and tkz. \usetkzobj{angles} this macro loads all the macros for the angles, if you need other objects \usetkzobj{angles,polygons} the syntax is the same as \usetikzlibrary and if you want all the objects , you write \usetkzobj{all}

\documentclass{article}
\usepackage{tkz-euclide} % loads  TikZ and tkz-base
\usetkzobj{angles} % important you want to use angles
\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 (0,0) {};
\node [dot=B] at (3,1) {};
\node [dot=P] at (0.9,-1.2) {};

%\draw [extended line=0.5cm] (A) -- (B);
% In tkz-euclide I defined something like extend line 
% but I prefer my method because I add a percentage of the segment  at each sides
% with [add = % and %] left and right 
\tkzDrawLine[add=1 and .5](A,B) % with 1 you double the line BA from A
\draw [extended line] ($(A)!(P)!(B)$) coordinate (H) -- (P);
 % I named the projection H
  \tkzMarkRightAngle[fill=blue!20,size=.5](A,H,P) % size number in cm
  % and you need to give the points in an order counterclockwise
  \tkzMarkRightAngle[fill=red!20,size=.8](B,H,P)  
\end{tikzpicture}  

% Now an example with only tkz-euclide
\begin{tikzpicture}[scale=.5]
  \tkzDefPoint(0,0){A}
  \tkzDefPoint(4,2.5){B}
  \tkzDefPoint(4,5){C}
  \tkzDrawLine[add= 0.5 and 0.8,color=blue](A,B)
   \tkzDefPointBy[projection=onto B--A](C)  \tkzGetPoint{H}
  \tkzDrawLine[add = .5 and .2,color=red](C,H)
  \tkzLabelPoint(H){$h$} 
  \tkzMarkRightAngle[fill=blue!20,size=.5](C,H,B)
  \tkzDrawPoints(A,B,C)\tkzLabelPoints(A,B,C) % better now
\end{tikzpicture}  

\end{document}

enter image description here

Alain Matthes
  • 95,075
  • @Altermundus: tkz-euclide seems very useful, but Jake's answer does not require another package so going to accept that for now. Hope to start experimenting with tkz-euclide soon. – Peter Grill Jun 29 '11 at 21:11
  • @Peter No problem. We find solutions and you make your choice , it's normal. My only problem with Jake'solution it's the use of a quadrant and the syntaxe right angle symbol={A}{P}{B}. – Alain Matthes Jun 29 '11 at 21:24
  • @Altermundus: The ability to specify a quadrant was an explicit requirement by the OP. It is an optional argument in my solution, so I don't see the drawback. Also, could you be more specific regarding what you don't like about the syntax? Would you prefer if the points were comma separated, if their order was changed, or if the name was shorter? Either of that is easily possible. – Jake Jun 29 '11 at 22:00
  • @Jake Sorry for my english. These are only minor complaints about the syntax. right angle symbol={A}{P}{B} is not necessary if you use quadrant. Perhaps, it's preferable to name (H) the projection of P on the line AB. This can be useful for the OP. You can write now right angle=H. Now you can use a quadrant or write (A,H,P) or (P,H,B). – Alain Matthes Jun 30 '11 at 05:07
  • @Jake You have a problem if A,B and P are aligned ! – Alain Matthes Jun 30 '11 at 05:58
  • @Altermundus: The correct behaviour, I guess, would be to throw an error if the user tries to draw a right angle symbol in a situation where no right angle exists. However, I think it's fair enough to assume that this is something the user has to take care of themselves.
    Regarding your comment about the quadrants: There are four possible places for the right angle symbol, but only two can be specified by changing the order of the points, so something like a quadrant option (or maybe mirror) is still required.
    – Jake Jun 30 '11 at 06:05
  • @Jake If P is on AB the perpendicular line to AB exists, but I agree with you for the remark about quadrants. With my method I need to name others points. But If you draw a geometric figure more complex, I think is not a waste of name to name others points.. In the spirit of Tikz, your method is the better one ! – Alain Matthes Jun 30 '11 at 06:27