7

I'm trying to draw the following in TikZ:

Simplex probabilities

Such that a=1/2, b=1/4 and c=1/4.These lines must be at right angles from the triangle sides.

Finally, the triangle has a height of 1.

Here's my MWE: triangle

\documentclass[tikz]{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\coordinate (A) at (0,0) ;
\coordinate (B) at ({sqrt(4/3}, 0) {};
\coordinate (C) at ({(sqrt(4/3)/2},1) {};

\node at (A) [below left] {1};

\node at (B) [below right]{2};

\node at (C) [above]{3};

\filldraw[opacity=.3, blue] (A)  -- (B) -- (C) -- cycle;

\end{tikzpicture}
\end{document}   
  • 1
    Can you compute the coordinates of the points? If so, you just need some \draw (a,b) -- (c,d) to connect the points. Please make an initial attempt and then edit the question as to where you got stuck. – Peter Grill Aug 27 '19 at 17:51
  • 1
    Please show us what you try so far and where you stuck. It seems that this is more geometry/math than TikZ problem ... – Zarko Aug 27 '19 at 17:51
  • I wanted to know if there was a way of skipping the calculations to find the middle point. I have already set the corners of a unit equilateral triangle as (0,1), (1,0) and (0.5, sqrt(0.75)) – Pablo Derbez Aug 27 '19 at 18:08
  • Do you know the sides lengths of the triangle? In short: what is given, in addition to a, b and c? – Bernard Aug 27 '19 at 18:21
  • The length of each side is 1 – Pablo Derbez Aug 27 '19 at 18:22
8

Tikz provides:

  • a barycentric coordinate system ("13.2.2 Barycentric Systems", p.136, pgfmanual, v3.1.4b).

    Example: (barycentric cs:A=1/2,B=1/4,C=1/4)

  • a projection modifier via the TikZ library calc ("13.5.5 The Syntax of Projection Modifiers", p.148, pgfmanual, v3.1.4b).

    Example to project P on AB: ($(A)!(P)!(B)$)

Here a solution using these features (and polar coordinates to define vertices):

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[node font=\scriptsize,inner sep=.5em]

  \path (-150:2/3) coordinate (A) node[below left]{A};
  \path ( -30:2/3) coordinate (B) node[below right]{B};
  \path (  90:2/3) coordinate (C) node[above]{C};

  \path[fill=blue!30,draw=blue] (A) -- (B) -- (C) -- cycle;

  \path (barycentric cs:A=1/2,B=1/4,C=1/4) coordinate (P) node[above]{P};
  \fill (P) circle (1pt);

  \draw[red] (P) -- ($(A)!(P)!(B)$);
  \draw[red] (P) -- ($(B)!(P)!(C)$);
  \draw[red] (P) -- ($(C)!(P)!(A)$);
\end{tikzpicture}
\end{document}

enter image description here

13

To me this looks like an XY question. What you really may be after (or what you were really asked to do) is to produce a so-called ternary diagram. Luckily there exists a library for this specifically: \usepgfplotslibrary{ternary}. It comes with pgfplots, which is based on TikZ. I added the braces for fun, but also think you'd be better off with just the diagram. Notice that there are already several posts on this site that discuss how you can customize these diagrams, just do a google search for site:tex.stackexchange.com ternary diagram to find them.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,decorations.pathreplacing}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.16}
\usepgfplotslibrary{ternary} 
\begin{document}
\begin{tikzpicture}
  \begin{ternaryaxis}
    \addplot3 coordinates {(0.25,0.5,0.25)} ;
    \path (0.25,0.5,0.25) coordinate (M)
     (1,0,0) coordinate (C) (0,1,0) coordinate (A) (0,0,1) coordinate (B);
  \end{ternaryaxis}
  \draw (M) -- ($(B)!(M)!(C)$); 
  \draw (M) -- ($(A)!(M)!(B)$);
  \draw (M) -- ($(C)!(M)!(A)$);
  \begin{scope}[thick,decoration={brace,raise=1pt}]
   \draw[decorate] (M) -- ($(B)!(M)!(C)$) node[midway,above=2pt,sloped]{$0.5$}; 
   \draw[decorate] (M) -- ($(A)!(M)!(B)$) node[midway,right=2pt]{$0.25$}; 
   \draw[decorate] ($(C)!(M)!(A)$) -- (M) node[midway,above=2pt,sloped]{$0.25$}; 
  \end{scope}   
\end{tikzpicture}
\end{document}

enter image description here

  • That looks really useful, I'll check it out. I've already found a solution which I posted below, but this looks more elegant. – Pablo Derbez Aug 28 '19 at 1:45
  • For context, what I'm doing is transcribing a professor's hand written slides to LaTeX because I they're difficult to study with. The diagram I posted on the questions comes directly from those slides. – Pablo Derbez Aug 28 '19 at 1:48
  • @PabloDerbez They could be early versions of such diagrams. Yet with the underlying grid that pgplots provides it is actually rather straightforward to infer the coordinates 25%, 25% and 50%, so there is not really a need to add all these braces, which may render the diagram unreadable if you add several points. – Schrödinger's cat Aug 28 '19 at 1:53
  • @Schrödinger'scat Can I use your code at here tex.stackexchange.com/questions/505037/… to input coordinates of the point M in my answer by formulas $M = (1 - \sqrt{r_1}) A + (\sqrt{r_1} (1 - r_2)) B + (r_2 \sqrt{r_1}) C$? – minhthien_2016 Aug 28 '19 at 4:50
7

The calculations is described in following drawing:

enter image description here

For a triangle whose the height is 1, b=c=0.25 and a=0.5.

\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{intersections,calc}
\begin{document}
\begin{tikzpicture}[scale=2]

\coordinate (A) at (0,0);
\coordinate (B) at ({sqrt(4/3)}, 0) {};
\coordinate (C) at ({(sqrt(4/3)/2},1) {};
\filldraw[opacity=.3, blue] (A)  -- (B) -- (C) -- cycle;
\node at (A) [below left] {1};
\node at (B) [below right]{2};
\node at (C) [above]{3};

%\draw (O)--++(0:1)coordinate(A)--++(120:1)coordinate(B)--cycle;
\draw ($(A)!0.375!(B)$)coordinate(X)--++(90:0.25)coordinate(P)--++(150:0.25)coordinate(Y);
\draw (P)--++(30:0.5)coordinate(Z);

\path[] let \p1 = ($ (X) - (P) $) in (X) -- (P) node[midway,below=-1mm,sloped]{\scalebox{0.25}{  \pgfmathparse{veclen(\x1,\y1)/28.4}\pgfmathresult cm}};
\path[] let \p1 = ($ (Y) - (P) $) in (Y) -- (P) node[above=-0.8mm,midway,sloped]{\scalebox{0.25}{ \pgfmathparse{veclen(\x1,\y1)/28.4}\pgfmathresult cm}};
\path[] let \p1 = ($ (Z) - (P) $) in (Z) -- (P) node[above=-0.8mm,midway,sloped]{\scalebox{0.25}{ \pgfmathparse{veclen(\x1,\y1)/28.4}\pgfmathresult cm}};

\end{tikzpicture}
\end{document}

enter image description here

  • Thank you! That's very helpful. I made a mistake when presenting the problem however. The height of the triangle is 1, not the length of the sides. How did you get 0.375 (the x coordinate for the point inside the triangle)? – Pablo Derbez Aug 27 '19 at 20:52
  • I have edited my answer according to your new conditions. – ferahfeza Aug 27 '19 at 20:56
  • The 0.375 is from 0.433x(lenght of triangle side)=0.433xsqrt(4/3). Because 0.25/tan(30)=0.433. – ferahfeza Aug 27 '19 at 21:55
6

Since there are already so many answers, one more cannot hurt. I am spelling out my comment, but instead of barycentric cs: an arguably even simpler possibility is to declare the x, y and z basis vectors to be the corners of the regular triangle. Then specifying the coordinate is as simple as

\path (0.25,0.5,0.25) coordinate(P);

Full example:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,shapes.geometric}
\begin{document}
\begin{tikzpicture}
 \node[draw,regular polygon,regular polygon sides=3,minimum width=4cm](ternary){};
 \begin{scope}[x={(ternary.corner 1)},y={(ternary.corner 2)},z={(ternary.corner 3)}]
  \path (0.25,0.5,0.25) node[circle,fill,inner sep=1.5pt,label=above:$P$](P){};
  \foreach \X [evaluate=\X as \NextX using {int(1+mod(\X,3))}]in {1,2,3}
   {\draw[blue] (P) -- ($(ternary.corner \X)!(P)!(ternary.corner \NextX)$);}
 \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

  • I understood. Thank you very much. – minhthien_2016 Aug 29 '19 at 1:12
5

(too long for a comment) So many people here for a question without MWE and incorrect data! I will delete if OP does not provide at least correct data.

enter image description here

\documentclass[tikz]{standalone}
\usetikzlibrary{calc,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[scale=4]
% suppose the altitude is 1
\pgfmathsetmacro{\a}{2*sqrt(3)/3}
\draw[teal]
(0,0) coordinate (1) node[below left]{1}--
(\a,0) coordinate (2) node[below right]{2}--
([turn]120:\a) coordinate (3) node[above]{3}--cycle;
\path
(.5*\a,0)   coordinate (M)
+(90:.5)    coordinate (I)
($(1)!(I)!(3)$) coordinate(N)
($(2)!(I)!(3)$) coordinate (P);
\draw[red] 
(I)--(M) node[midway,right=1pt,cyan]{$a$} 
(I)--(N) node[midway,below left,cyan]{$b$}
(I)--(P) node[midway,above left,cyan]{$c$};
\draw[decorate,decoration={brace,raise=1pt},cyan] (I)--(M);
\draw[decorate,decoration={brace,raise=1pt},cyan] (I)--(N);
\draw[decorate,decoration={brace,raise=1pt},cyan] (I)--(P);
\end{tikzpicture}
\end{document}
  • Sorry will upadte my question ASAP – Pablo Derbez Aug 27 '19 at 20:05
  • 1
    I have updated my question. – Pablo Derbez Aug 27 '19 at 20:18
5

This code provide a way which you only input three sides a, b, c and two numbers t and m, then the point M is choosen inside the triangle, see at https://math.stackexchange.com/questions/18686/uniform-random-point-in-triangle and then, draw the segments from M to the line AB, BC, and AC. To solve your problem, you choose a = b = c = 2/sqrt(3) and t = 1/4, m=1/2. You can use this code for every triangles.

\documentclass[border = 1mm]{standalone} 
\usepackage{tikz}
\usetikzlibrary{intersections,calc,backgrounds,fpu} 
\newcommand{\PgfmathsetmacroFPU}[2]{\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathsetmacro{#1}{#2}%
\pgfmathsmuggle#1\endgroup}
\begin{document}
\begin{tikzpicture}[line join = round, line cap = round,scale = 4]
    \pgfmathsetmacro{\a}{2*sqrt(3)/3} 
    \pgfmathsetmacro{\b}{2*sqrt(3)/3} 
    \pgfmathsetmacro{\c}{2*sqrt(3)/3}
    \pgfmathsetmacro{\t}{1/4}
    \pgfmathsetmacro{\m}{1/2}
     \coordinate (B) at (0,0);
    \coordinate (C) at (\c,0);
    \coordinate (A) at  ({(pow(\b,2) + pow(\c,2) - pow(\a,2))/(2*\c)},{sqrt((\a+\b-\c) *(\a-\b+\c) *(-\a+\b+\c)* (\a+\b+\c))/(2*\c)},0);
    \coordinate (M) at  ({-(((\c*\c* (-2 + \m) + (\a*\a - \b*\b) *\m) *sqrt(\t))/(2 *\c))},{(sqrt((\a + \b - \c)* (\a - \b + \c)* (-\a + \b + \c)* (\a + \b + \c))*\m*sqrt(\t))/(2*\c)});
    \coordinate (N) at ($(B)!(M)!(C)$);
    \coordinate (P) at ($(A)!(M)!(C)$);
       \coordinate (Q) at ($(A)!(M)!(B)$);
         \foreach \point/\position in {A/above,B/below,C/below,M/above,N/below,P/above,Q/left}
    {
        \fill (\point) circle (0.3pt);
        \node[\position=3pt] at (\point) {$\point$};
    }
    \draw[thick] (A) -- (B) -- (C) --cycle;
   \foreach \X in {N,P,Q} \draw[thick, cyan] (\X) -- (M);    
\end{tikzpicture}
   \end{document}

enter image description here

enter image description here

If you try

\documentclass[border = 1mm]{standalone} 
\usepackage{tikz}
\usetikzlibrary{intersections,calc,backgrounds,fpu} 
\newcommand{\PgfmathsetmacroFPU}[2]{\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathsetmacro{#1}{#2}%
\pgfmathsmuggle#1\endgroup}
\begin{document}
\begin{tikzpicture}[line join = round, line cap = round]
    \pgfmathsetmacro{\a}{3} 
    \pgfmathsetmacro{\b}{4} 
    \pgfmathsetmacro{\c}{5}
    \pgfmathsetmacro{\t}{4/9}
    \pgfmathsetmacro{\m}{1/2}
     \coordinate (B) at (0,0);
    \coordinate (C) at (\c,0);
    \coordinate (A) at  ({(pow(\b,2) + pow(\c,2) - pow(\a,2))/(2*\c)},{sqrt((\a+\b-\c) *(\a-\b+\c) *(-\a+\b+\c)* (\a+\b+\c))/(2*\c)},0);
    \coordinate (M) at  ({-(((\c*\c* (-2 + \m) + (\a*\a - \b*\b) *\m) *sqrt(\t))/(2 *\c))},{(sqrt((\a + \b - \c)* (\a - \b + \c)* (-\a + \b + \c)* (\a + \b + \c))*\m*sqrt(\t))/(2*\c)});
    \coordinate (N) at ($(B)!(M)!(C)$);
    \coordinate (P) at ($(A)!(M)!(C)$);
       \coordinate (Q) at ($(A)!(M)!(B)$);
         \foreach \point/\position in {A/above,B/below,C/below,M/above,N/below,P/above,Q/left}
    {
        \fill (\point) circle (0.3pt);
        \node[\position=3pt] at (\point) {$\point$};
    }
    \draw[thick] (A) -- (B) -- (C) --cycle;
   \foreach \X in {N,P,Q} \draw[thick, cyan] (\X) -- (M);    
\end{tikzpicture}
   \end{document}

enter image description here

I copied some code of Paul Gaborit.

\documentclass[border = 1mm]{standalone} 
\usepackage{tikz}
\usetikzlibrary{intersections,calc,backgrounds,fpu} 
\newcommand{\PgfmathsetmacroFPU}[2]{\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathsetmacro{#1}{#2}%
\pgfmathsmuggle#1\endgroup}
\begin{document}
\begin{tikzpicture}[line join = round, line cap = round]
    \pgfmathsetmacro{\a}{3} 
    \pgfmathsetmacro{\b}{4} 
    \pgfmathsetmacro{\c}{5}
        \coordinate (B) at (0,0);
    \coordinate (C) at (\c,0);
    \coordinate (A) at  ({(pow(\b,2) + pow(\c,2) - pow(\a,2))/(2*\c)},{sqrt((\a+\b-\c) *(\a-\b+\c) *(-\a+\b+\c)* (\a+\b+\c))/(2*\c)},0);
    \coordinate (P) at (barycentric cs:A=1/3,B=1/3,C=1/3);
    \fill (P) circle (1pt);
    \draw[red] (P) -- ($(A)!(P)!(B)$);
    \draw[red] (P) -- ($(B)!(P)!(C)$);
    \draw[red] (P) -- ($(C)!(P)!(A)$);

         \foreach \point/\position in {A/above,B/below,C/below,P/above}
    {
        \fill (\point) circle (0.3pt);
        \node[\position=3pt] at (\point) {$\point$};
    }
    \draw[thick] (A) -- (B) -- (C) --cycle;
  \end{tikzpicture}
   \end{document}
  • You can make it a projection from 3d by using an isometric projection, in which the projections of the three orthogonal 3d directions form a regular triangle. – Schrödinger's cat Aug 29 '19 at 2:13
  • Thanks. How about if the triangle non regular triangle? – minhthien_2016 Aug 29 '19 at 4:00
  • If you have three points, say, A, B and C, you can always say \begin{scope}[x={(A)},y={(B)},z={(c)}] \path (<x>,<y>,<z>) coordinate (P); \end{scope}, and P will be at x*(A)+y*(B)+z*(C). This is true regardless of whether A, B and C are 3d or 2d coordinates. – Schrödinger's cat Aug 29 '19 at 4:33
  • Many thank to you. – minhthien_2016 Aug 29 '19 at 4:42
  • I tried like this \begin{scope} [sqrt(\t)*(1-\m)={(B)},\m*sqrt(\t)={(C)}] \path (<y>,<z>) coordinate (M); \end{scope} Is it true? – minhthien_2016 Aug 29 '19 at 8:16
4

I wrote a macro that builds such a triangle. But its sides and height do not measure 1 unit. Probabilities are the arguments.

For example, we call it \proba{.5}{.25}{.25} or \proba{.2}{.3}{.5}

If that's all right with you, I'll explain the construction.

screenshot

\documentclass[tikz,border=5mm]{standalone} 
\usepackage{xcolor}
\usetikzlibrary{calc,angles,decorations.pathreplacing}
\definecolor{mygreen}{RGB}{63,186,143}

\newcommand{\proba}[3]{
\begin{tikzpicture}[auto=left,decoration={brace,amplitude=5pt,raise=5pt}]
\coordinate(I) at (0,0);
\coordinate(c) at (-90:#3*10);
\coordinate(b) at (150:#2*10);
\coordinate(a) at (30:#1*10);
\coordinate(c') at ($(c)!1!-90:(I)$);
\coordinate(b') at ($(b)!1!-90:(I)$);
\coordinate(a') at ($(a)!1!-90:(I)$);
\coordinate[label=left:1](1) at (intersection of c--c' and b--b');
\coordinate[label=right:2](2) at (intersection of a--a' and c--c');
\coordinate[label=above:3](3) at (intersection of a--a' and b--b');
\draw (1)--(2)--(3)--cycle;
\foreach \p in {a,b,c}{
    \draw[red,postaction={draw=mygreen,decorate,
        decoration={brace,amplitude=5pt,raise=5pt}}] (\p)--(I);
     \path($(\p)!5mm!90:(I)$)--($(I)!5mm!-90:(\p)$)node[midway,mygreen,font=\bf]{\p};   
    \pic [draw]{right angle = I--\p--\p'};
}
\end{tikzpicture}
}
\begin{document}

\proba{.5}{.25}{.25}

\proba{.2}{.3}{.5}

\end{document}
1

Thanks everybody. Here's what I ended up doing, inspired in part by the other answers. I also used tkz-euclid to draw the lines at right angles. I ended up ditching the exact measures.

enter image description here

\documentclass[tikz]{standalone}

\usepackage{tkz-euclide} 
\usetkzobj{all} 

\usetikzlibrary{calc,decorations.pathreplacing}

\begin{document}
\begin{tikzpicture}[scale=1.2]

\coordinate (A) at (0,0) ;
\coordinate (B) at ({sqrt(4/3}, 0) {};
\coordinate (C) at ({(sqrt(4/3)/2},1) {};

\filldraw[opacity=.3,blue] (A)  -- (B) -- (C) -- cycle;

\node at (A) [below left] {1};
\node at (B) [below right]{2};
\node at (C) [above]{3};

\coordinate (x) at ($(A) + (.4,.25)$){};

\tkzDefPointBy[projection=onto A--C](x) \tkzGetPoint{E}
\tkzDefPointBy[projection=onto A--B](x) \tkzGetPoint{F}
\tkzDefPointBy[projection=onto B--C](x) \tkzGetPoint{G}


\draw (x) -- (E);
\draw (x) -- (F);
\draw (x) -- (G);



\node at ($(x)!0.5!(G)$)[above left=0.5pt]{\footnotesize a};
\node at ($(x)!0.5!(E)$)[below left=0.5pt]{\footnotesize b};
\node at ($(x)!0.5!(F)$)[right=0.5pt]{\footnotesize c};

\draw[decorate,decoration={brace,raise=1pt}] (x)--(E);
\draw[decorate,decoration={brace,raise=1pt}] (x)--(F);
\draw[decorate,decoration={brace,raise=1pt}] (x)--(G);


\end{tikzpicture}
\end{document}

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.