4

I have a typical geometric construction that I want to draw using TikZ. The following code gives a line segment PQ. How do I get TikZ to draw a point on the line perpendicular to PQ and through P that is 2\sqrt{2} units below the line segment PQ? I think that I have to have \usetikzlibrary{calc} in the preamble to do this. (I want to label this point R and draw right triangle QPR.) If I want to use \tkzMarkRightAngle(Q,P,R); to indicate that the triangle is a right triangle, do I have to have \usepackage{tkz-euclide} in the preamble?

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,intersections,quotes}


\begin{document}

\begin{tikzpicture}

\draw[yellow, line width=0.1pt] (-1.75,-1.75) grid[xstep=0.5, ystep=0.5]  (2.75,1.75);
\draw[draw=gray!30,latex-latex] (0,1.75) +(0,0.25cm) node[above right] {$y$} -- (0,-1.75) -- +(0,-0.25cm);
\draw[draw=gray!30,latex-latex] (-1.75,0) +(-0.25cm,0) -- (2.75,0) -- +(0.25cm,0) node[below right] {$x$};

\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]left:$P$}] (P) at (-1,-1) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt, label={[fill=white]right:$Q$}] (Q) at (2,1) {};

\draw[green!20!white] (P) -- (Q);

\end{tikzpicture}
\end{document}
  • Check page 145 of the manual (calc ltikzlibrary). – John Kormylo Apr 20 '15 at 16:01
  • @John Kormylo I made the grid more expansive and included the code \coordinate (R) at ($(P)!2\sqrt{5}!90:(Q)$); and got an error message. It seems to me that TikZ could not interpret \sqrt{5} as part of a measurement. I changed the command to \coordinate (R) at ($(P)!1!90:(Q)$); and nothing was plotted. I think that I need to have cm as the units. Isn't cm the default unit assigned to a number? I changed the command to \coordinate (R) at ($(P)!1cm!90:(Q)$); and nothing was plotted. – user74973 Apr 21 '15 at 0:56
  • @John Kormylo I would like to have a dot put at R. Why isn't \node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]above left:$R$}] (R) at R {}; putting a dot at R? – user74973 Apr 21 '15 at 0:57
  • @John Kormylo By the way, how do I know whether -90 or 90 will make the coordinate R below line segment PQ? – user74973 Apr 21 '15 at 0:58
2

Check the code for explanatory comments:

\documentclass{amsart}
\usepackage{tikz}

%% you need the following 2 lines to use \tkzMarkRightAngle
%\usepackage{tkz-euclide}
%\usetkzobj{all}

\usetikzlibrary{shapes,positioning,intersections,quotes,calc}


\begin{document}

\begin{tikzpicture}

\draw[yellow, line width=0.1pt] (-1.75,-5) grid[xstep=0.5, ystep=0.5]  (2.75,1.75);
\draw[draw=gray!30,latex-latex] (0,1.75) +(0,0.25cm) node[above right] {$y$} -- (0,-1.75) -- +(0,-0.25cm);
\draw[draw=gray!30,latex-latex] (-1.75,0) +(-0.25cm,0) -- (2.75,0) -- +(0.25cm,0) node[below right] {$x$};

\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]left:$P$}] (P) at (-1,-1) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt, label={[fill=white]right:$Q$}] (Q) at (2,1) {};

\draw[green!20!white] (P) -- (Q);

%% the perpendicular
\pgfmathparse{2*sqrt(5)}
\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]left:$R$}] (R) at ($ (P)!\pgfmathresult cm! -90:(Q) $) {};
\draw[green!20!white] (P) -- (R) -- (Q);


%% right angle with tkz-euclide
%\coordinate (p) at (P);
%\tkzMarkRightAngle[color=green!20!white](Q,p,R)
%\fill (p) circle (2.1pt);  %% to make the dot above right angle again.

\coordinate (a) at ($ (P)!5mm! -45:(Q) $);
\draw[green!20!white] (a) -- ($(P)!(a)!(Q)$);
\draw[green!20!white] (a) -- ($(P)!(a)!(R)$);

\end{tikzpicture}
\end{document}

enter image description here

  • I understand that the command \coordinate (a) at ($ (P)!5mm! -45:(Q) $); is instructing TikZ to put a coordinate on the graph and to call it "a." With -45, isn't the command instructing TikZ to put the point at a distance of 5mm from P and at an angle of 45 degrees in the clockwise direction from line segment PQ? – user74973 Apr 21 '15 at 18:21
  • @user74973 Yes. – user11232 Apr 21 '15 at 23:20
2

To answer your questions, tikz runs almost everything through pgfmathparse which looks more like C code: 2*sqrt(5) which BTW is over 4cm at the default scale. ($(P)!1!90:(Q)$) means start at (P) and go 1 times the distance to (Q) 90 degrees (counter-clockwise) from the direction to (Q).

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,intersections,quotes,calc}

\begin{document}

\begin{tikzpicture}

\draw[yellow, line width=0.1pt] (-1.75,-1.75) grid[xstep=0.5, ystep=0.5]  (2.75,1.75);
\draw[draw=gray!30,latex-latex] (0,1.75) +(0,0.25cm) node[above right] {$y$} -- (0,-1.75) -- +(0,-0.25cm);
\draw[draw=gray!30,latex-latex] (-1.75,0) +(-0.25cm,0) -- (2.75,0) -- +(0.25cm,0) node[below right] {$x$};

\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]left:$P$}] (P) at (-1,-1) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt, label={[fill=white]right:$Q$}] (Q) at (2,1) {};

\draw[green!20!white] (P) -- (Q);

\coordinate (R) at ($(P)!2cm*sqrt(5)!-90:(Q)$);
\node[outer sep=0pt,circle, fill,inner sep=1.5pt, label={[fill=white]right:$R$}] at(R) {};

\end{tikzpicture}
\end{document}

calc usage

  • You said that "($(P)!1!90:(Q)$) means start at (P) and go 1 times the distance to (Q) 90 degrees (counter-clockwise) from the direction to (Q)." This code will give me a right triangle. Right? It won't be the one intended - it will draw the height from the base PQ at Q. – user74973 Apr 21 '15 at 16:35
1

Actually, you are not forced to use \usepackage{tkz-euclide}. It only gives you an easier \tkzMarkRightAngle(Q,P,R) construct than pure tikz, but this can also be achieved relatively easily using only tikz as @HarishKumar did it. Also, using styles for axes and points makes the code cleaner.

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}\small

\begin{tikzpicture}[outer sep=0pt,p/.style={circle, fill,inner sep=1.5pt},ax/.style={draw=gray!50,latex-latex}]
\draw[yellow,line width=0.1pt] (-2.75cm,-1.75cm) grid[xstep=0.5, ystep=0.5]  (2.75cm,1.75cm);
\draw[ax](0,1.75cm) +(0,0.25cm) node[above] {$y$} -- (0,-1.75cm) -- +(0,-0.25cm);
\draw[ax](-2.75cm,0) +(-0.25cm,0) -- (2.75cm,0) -- +(0.25cm,0) node[right] {$x$};
\coordinate[p,label={[fill=white]below:$P$}] (P) at (-1cm,-1cm);
\coordinate[p,label={[fill=white]right:$Q$}] (Q) at (2cm,1cm);
\coordinate[p,label={[fill=white]left :$R$}] (R) at ($(P)!{2cm*sqrt(2)}!90:(Q)$);
\draw (P)--(R)--(Q)--(P)--cycle;
\coordinate (a) at ($(P)!4mm!45:(Q)$);
\draw ($(P)!(a)!(Q)$) -- (a) -- ($(P)!(a)!(R)$);
\end{tikzpicture}

\end{document}

enter image description here

  • I looked on page 216 of the manual at http://texdoc.net/texmf-dist/doc/generic/pgf/pgfmanual.pdf and it only says that specifying coordinates follows at. Now, you tell me that you can locate a point with $(P)!{2cm*sqrt(2)}!90:(Q)$. That will be helpful in coding. Thanks. – user74973 Apr 21 '15 at 16:22
  • I see how you got a length of 2\sqrt{2} - you scaled 2cm by \sqrt{2}. Isn't the default unit centimeters? Why can't you get the same picture with $(P)!{2*sqrt(2)}!90:(Q)$? – user74973 Apr 21 '15 at 16:28
0

For those who are interested, a way (among others) of doing it with MetaPost.

The point R such that PR is perpendicular to PQ and located 2\sqrt{2} units away from P below PQ, is given by the following instruction (u being the unit length, here cm):

 R = P + 2u*sqrt2*unitvector(Q-P) rotated -90;

The anglebetween macro of MetaPost's Metafun format allows to draw right angle marks quite easily between the two intersecting segments P--Q and P--R (the third argument being the label, here an empty string), if the anglemethod parameter is set to 2. With the default value of this parameter, 1, the mark would be a circular arc. The desired length of the mark should be given as argument to another parameter, anglelength (which defaults to 20 pt).

  anglemethod := 2; anglelength := 2mm;
  draw anglebetween(P--Q, P--R, "");

See the Metafun manual, p. 279, for a detailed presentation.

\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
  \mplibsetformat{metafun}
  \mplibtextextlabel{enable}
\begin{document}
  \begin{mplibcode}
    % Axes parameters
    u := cm; % Unit length
    xmin := -1.75u; xstep := .5u; xmax := 2.75u; 
    ymin := -5u; ystep := xstep; ymax := 1.75u;
    % Triangle summits
    pair P, Q, R; P = u*(-1, -1); Q = u*(2, 1); 
    R = P + 2u*sqrt2*unitvector(Q-P) rotated -90;
    beginfig(1);
      % Grid
      drawoptions(withcolor yellow);
      for i = ceiling(xmin/xstep) upto floor(xmax/xstep):
        draw (i*xstep, ymin) -- (i*xstep, ymax);
      endfor 
      for j = ceiling(ymin/ystep) upto floor(ymax/ystep):
        draw (xmin, j*ystep) -- (xmax, j*ystep);
      endfor
      % Axes
      drawoptions(withcolor .8white);
      drawarrow (xmin, 0) -- (xmax, 0); 
      drawarrow (0, ymin) -- (0, ymax); 
      % Triangle
      drawoptions(withcolor green);
      path triangle; triangle = P--Q--R--cycle; draw triangle;
      % Right-angle mark of length 2 mm (and no label)
      anglemethod := 2; anglelength := 2mm;
      draw anglebetween(P--Q, P--R, "");
      % Labels
      drawoptions();
      label.bot("$x$", (xmax, 0)); label.lft("$y$", (0, ymax));
      dotlabel.lft("$P$", P); dotlabel.rt("$Q$", Q); dotlabel.bot("$R$", R);
    endfig;
  \end{mplibcode}
\end{document}

To be typeset with LuaLaTeX. Output:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.