# Drawing of triangle and coordinate system using TikZ

I am using TikZ for the first time, and I am experiencing some difficulty getting what exactly I want it to display. I am trying to draw the following picture:

Here is the code (I know it is not that good, but note it's my first time using TikZ):

\documentclass[paper=a4, fontsize=11pt]{scrartcl} % A4 paper and 11pt font size

\usepackage[T1]{fontenc}
\usepackage[english]{babel} % English language/hyphenation
\usepackage{amsmath,amsfonts,amsthm} % Math packages
\usepackage{graphicx}
\usepackage{mdframed}
\usepackage[ampersand]{easylist}
\usepackage{enumitem}
\usepackage{tikz}

\begin{document}
\begin{center}\begin{tikzpicture}[auto]
\draw[thick, ->] (-6,0) -- (10,0) node[anchor = north west] {x};
\draw[thick, ->] (0,-3) -- (0,11) node[anchor = south east] {y};
\node (P) at (-4,-2) {$P(-4;-2)$};
\node (Q) at (8,3) {$Q(8;3)$};
\node (R) at (4,10) {$R(4;10)$};
\node (N) at (2,7) {N};
\draw (P) to (Q);
\draw (R) to (Q);
\draw (P) to (R);
\draw (N) to (Q);
\end{tikzpicture}\end{center}
\end{document}


and it is currently giving me the following:

How can I

• get the point names to not cut through the lines,
• draw the line connecting QN to be indicated as a perpendicular line?

I'll just show one out of many possibilities (and yet another alternative for 90-degrees angle mark - for other angles have a look at the angles library in the manual for v3.00-)

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[scale=5]% Scale it rather using too big dimensions in centimeters
\coordinate[label=below:{$P(-4,-2)$}](p) at (-4mm,-2mm);
\coordinate[label=right:{$Q(8,3)$}](q) at (8mm,3mm);
\coordinate[label=above:{$R(4,10)$}] (r) at (4mm,10mm);

\draw (p) -- (q) -- (r) -- cycle; %Look at the tip of the triangle with cycle or (p)
% Here is some black magic; start from q and draw to a point
% which is at the place along the line from p to r but at the
% place where q is projected on that line.
\draw (q) -- ($(p)!(q)!(r)$) coordinate (s);
\draw ($(s)!0.5mm!(q)$) coordinate (t) -- ($(t)!0.5mm!90:(q)$) --($(s)!0.5mm!(r)$);
\end{tikzpicture}
\end{document}


While you are developing your TikZ-fu, keep an eye on the package from our own Alain Matthes, called tkz-euclide. It makes these kind of drawings really really easy and structured. The only downside is that the manual is French to me (literally). But it is pretty self-explanatory nevertheless.

• Thank you! :). Can you please show me how to get the "perpendicular block" at N from line QN as well? :) – user860374 Feb 15 '15 at 14:12
• +1; I have just seen that page 146 of the TikZ manual v3.0 has the exact same solution. :) – Pier Paolo Feb 15 '15 at 14:22
• This code is very easy to follow :). Thank you! :). It worked wonderfully! How can I label the "projected" line's point as N? – user860374 Feb 15 '15 at 14:32
• @PierPaolo Apparently, I subconsciously memorized the TikZ manual. – percusse Feb 15 '15 at 14:52
• @Dillon \draw (q) -- ($(p)!(q)!(r)$)node[draw,pos=1,sloped,anchor=south west]{}node[pos=1.05]{$N$};. – user11232 Feb 15 '15 at 15:12
\documentclass[11pt,a4paper]{article}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}
\begin{tikzpicture}
\tkzInit[xmin=-5,xmax=9,ymin=-3,ymax=11]
\tkzAxeXY
%\tkzGrid
\tkzDefPoint[label=below:{$P(-4,-2)$}](-4,-2){P}
\tkzDefPoint[label=right:{$Q(8,3)$}](8,3){Q}
\tkzDefPoint[label=above:{$R(4,10)$}](4,10){R}
\tkzDrawSegments(P,Q Q,R R,P)
\tkzDefPointBy[projection=onto P--R](Q)
\tkzGetPoint{N}
\tkzLabelPoints[above left](N)
\tkzDrawPoints[color=red](P,Q,R,N)
\tkzDrawSegment(Q,N)
\tkzMarkRightAngle[fill=lightgray](Q,N,R)
\tkzLabelAngle[pos=1.3](Q,P,R){$\beta$}
\tkzMarkAngle[arc=l,size=1cm](Q,P,R)
\end{tikzpicture}
\end{document}


• Thank you! :) . How can I label the point of intersection of the projected line onto PR as N? – user860374 Feb 15 '15 at 14:35
• @Dillon Please see the edit. – user11232 Feb 15 '15 at 14:44

And for comparison, with Metapost.

Note that unlike the warning at the top of the OP diagram, this one is drawn to scale...

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);
u := 5mm;

% axes
path xx, yy;
xx = (5 left -- 9 right) scaled u;
yy = (3 down -- 11 up) scaled u;
drawarrow xx withcolor .5 white;
drawarrow yy withcolor .5 white;
label.rt (btex $x$ etex, point 1 of xx);
label.top(btex $y$ etex, point 1 of yy);

% define the points
pair M, N, P, Q, R;
P = (-4, -2) scaled u;
Q = ( 8,  3) scaled u;
R = ( 4, 10) scaled u;
M = yy intersectionpoint (P--R);
N = whatever[P,R]; (N-Q) dotprod (R-P) = 0;
%
% mark the right angle
draw unitsquare scaled 5 rotated angle (Q-N) shifted N withcolor .5 white;
% draw the lines
draw P--Q--R--cycle; draw Q--N;
label.ulft(btex $M$          etex, M);
label.ulft(btex $N$          etex, N);
label.top (btex $R\,(4;10)$  etex, R);
label.rt  (btex $Q\,(8;3)$   etex, Q);
label.lft (btex $P\,(-4;-2)$ etex, P);
% label the angle along the bisector
label(btex $\beta$ etex, P + 20 unitvector(Q+R-2P));
endfig;
end.


A little thought about the geometry here, shows us that since M is on the y-axis it is half way between P and R in the x-direction, so it must be the midpoint of P--R. It therefore has co-ordinates (0,4); and the distance from M to Q is therefore sqrt(8^2+1^2)=sqrt(65), but this is the same as the distance from R to Q, which is sqrt(4^2+7^2)=sqrt(65); hence QNR and QNM are congruent triangles, and therefore N is the midpoint of R--M with coordinates (2,7). You can use Metapost equation system to confirm this; if you add

M = (0,4) scaled u; N = (2,7) scaled u;


after the implicit definitions already given, then MP gives no error.

• how do you know if it's to scale when we do not know the units of the original? ;-) – Paul Gessler Feb 15 '15 at 19:25

A PSTricks solution using the pst-eucl package:

\documentclass{article}

\psset{unit = 0.76, PointSymbol = none}

\begin{document}

\begin{pspicture}(-5.3,-3.2)(10,11)
\pnodes(-4,-2){P}(8,3){Q}(4,10){R}(0,0){O}(0,10){Y}
\psaxes{->}(0,0)(-5.3,-3.2)(9.5,10.5)[$x$,0][$y$,90]
\pspolygon(P)(Q)(R)
\uput[270](P){$P(-4,-2)$}
\uput[350](Q){$Q(8,3)$}
\uput[90](R){$R(4,10)$}
\pstInterLL{P}{R}{O}{Y}{M}
\pstProjection{P}{R}{Q}[N]
\pstLineAB{Q}{N}
\pstLineAB{P}{N}
\pstRightAngle{P}{N}{Q}
\pstMarkAngle{Q}{P}{R}{$\beta$}
\end{pspicture}

\end{document}


With the unfairly unkwnown mfpic package. It is a wide set of (La)TeX macros providing a very convenient interface to either METAFONT or MetaPost, in this case the latter.

Since most are not familiar with this package, I've put a fair number of comments in the following code, hence its relative length.

For those who know a bit about MetaPost, mfpic can also embed raw MetaPost instructions which are sometimes more convenient. As I did here to locate the points M and N (which is done by MetaPost in its trademark implicit way), and to draw the right angle mark upon N (thanks to MetaPost transformers).

\documentclass{scrartcl}
% MetaPost instead of Metafont as drawing program and labels manager.
% Bounding box based on actual picture dimensions, not on the axes dimensions.
\usepackage[metapost, mplabels, truebbox]{mfpic}
% LaTeX preamble given to MetaPost for its labels management
% (corresponds to the verbatimtex ... etex flags of MetaPost)
\mfpverbtex{%&latex
\documentclass{scrartcl}
\begin{document}}
\setlength{\mfpicunit}{.5cm}
\opengraphsfile{\jobname}
\begin{document}
\begin{mfpic}[1]{-5}{9.5}{-3}{11}
% Points definitions. For MetaPost they are local pairs.
\setmfpair{P}{(-4, -2)}
\setmfpair{Q}{(8, 3)}
\setmfpair{R}{(4, 10)}
% Point M computed by MetaPost as intersection of y-axis and straight line (PQ)
\setmfpair{M}{(P -- R) intersectionpoint (origin -- (0, \ymax))}
% Point N computed by MetaPost as intersection of line (PQ)
% and the straight line perpendicular to (PQ) going through Q
\mfsrc{save N; pair N; N = whatever[P, R] = whatever[Q, Q + (R-P) rotated 90];}
% Mark angle beta with the convenient \arc macro of mfpic
\store{mark_angle}\arc[a]{P, 0.8, angle(Q-P), angle(R-P)}
% Mark right angle on N with help of MetaPost transformers
\setmfvariable{path}{mark_right_angle}
{((1, 0) -- (1, 1) -- (0, 1)) zscaled 0.3unitvector(Q-N) shifted N}
% Actual drawings
\polygon{P, Q, R}
\lines{Q, N}
\doaxes{xy}
\mfobj{mark_angle}
\mfobj{mark_right_angle}
% Labels
\tlpointsep{2bp} % Offset
\tlabels{[tc]{(\xmax, 0)}{$x$} [cr]{(0, \ymax)}{$y$}
[cr]{P}{$P(-4, -2)$} [cl]{Q}{$Q(8, 3)$} [bc]{R}{$R(4, 10)$}
[br]{M}{$M$} [cr]{N}{$N$} [bl]{point 0.4 of mark_angle}{$\beta$}}
\end{mfpic}
\closegraphsfile
\end{document}


The .tex file is to be typeset by (pdf)LaTeX first, then the resulting .mp file by MetaPost, and finally the LaTeX file by (pdf)LaTeX again, to produce the figure below.