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.
-
Related Question: Mark 90 degree angle in tikz in german convention.– Peter GrillNov 1, 2012 at 18:06
-
Follow-up Question: Right Angle Symbol Alignment with TikZ/Calc.– Peter GrillMay 31, 2013 at 0:26
-
Simple answer for 3d generalized: indicate a right angle in 3d– strpeterFeb 11, 2015 at 14:17
2 Answers
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
.
\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}
-
@Altermundus makes a good point about
right angle symbol={A}{P}{B}
. A better method syntax might beright angle symbol={A}{B}{P}
to specify which angle to place the symbol on. Jun 29, 2011 at 22:03 -
@Peter: The original order reflected the syntax of the
calc
syntax that is used to calculate the projection ofP
onAB
. I've changed it to{A}{B}{P}
now.– JakeJun 29, 2011 at 23:02 -
I get an error latexing this exact file:
! Argument of \pgfmath@afterquick has an extra }. <inserted text> \par l.21 }
The braces seem balanced though.– LiamJan 14, 2013 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)?– JakeJan 14, 2013 at 16:04
-
@Jake
Package: pgf 2008/01/15 v2.00 (rcs-revision 1.12)
from \pgfversion in Debian stable.– LiamJan 14, 2013 at 16:22
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}
-
@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. Jun 29, 2011 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}
. Jun 29, 2011 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.– JakeJun 29, 2011 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 nowright angle=H
. Now you can use a quadrant or write (A,H,P) or (P,H,B). Jun 30, 2011 at 5:07 -