7

The problem I am modeling: Three points are randomly chosen on a circle. What is the probability that the triangle formed by these three points contains the center of the circle?

Conceptual understanding: Suppose we fix two of the three points, call them A and B. In order for the triangle to contain the center, the third point C must lie within the arc A'B', where A' and B' are the image of points A and B respectively under a rotation of 180 degrees.

What I want to happen: The randomly generated inscribed triangle to be filled green when it contains the center, and to fill red when it does not contain the center. I would also like to keep tally of the number of successes and failures to compute an experimental probability.

enter image description here

A few key things: I have access to the x and y coordinates of each point by using \pgfextractx and \pgfextracty. My method was to test whether the point C is between both the x-coordinates and y-coordinates of A and B by using \xintifboolexpr, however, this is flawed.

Minimal Working Example:

\documentclass{article}
\usepackage[left=2cm, right=2cm, top=2cm, bottom=1cm]{geometry}
\usepackage{amsmath,amsfonts,tikz,xintexpr,calc}

\newcommand\circletest{
\begin{tikzpicture}[scale=0.6]

    \newdimen{\tempxa}
    \newdimen{\tempya}
    \newdimen{\tempxb}
    \newdimen{\tempyb}
    \newdimen{\tempxc}
    \newdimen{\tempyc}

    \def\radius{2}
    \draw (0,0) coordinate (O);
    \draw (O) circle[radius=\radius];
    \draw (rnd*360:\radius) coordinate (A);
        \pgfextractx\tempxa{\pgfpointanchor{A}{center}}
        \pgfextracty\tempya{\pgfpointanchor{A}{center}}
    \draw (rnd*360:\radius) coordinate (B);
        \pgfextractx\tempxb{\pgfpointanchor{B}{center}}
        \pgfextracty\tempyb{\pgfpointanchor{B}{center}}
    \draw (rnd*360:\radius) coordinate (C);
        \pgfextractx\tempxc{\pgfpointanchor{C}{center}}
        \pgfextracty\tempyc{\pgfpointanchor{C}{center}}

    \xintifboolexpr { (((\tempxc > -\tempxa) && (\tempxc < -\tempxb)) || ((\tempxc > -\tempxb) && (\tempxc < -\tempxa))) && (((\tempyc > -\tempya) && (\tempyc < -\tempyb)) || ((\tempyc > -\tempyb) && (\tempyc < -\tempya)))} %%I know this is grotesque 
     {\filldraw[color=green!80!black!100, fill=green!15] (A) -- (B) -- (C) -- cycle;} %true
      {\filldraw[color=red!80!black!100, fill=red!15] (A) -- (B) -- (C) -- cycle;} %false

    \fill[black] (A) circle[radius=2pt];
    \fill[black] (B) circle[radius=2pt];
    \fill[black] (C) circle[radius=2pt];
    \fill[black] (O) circle[radius=2pt];

    \draw (A) node[below]{A};
    \draw (B) node[below]{B};
    \draw (C) node[below]{C};
\end{tikzpicture}}

\begin{document}

\foreach \x in {0,1,...,11}{
    \circletest
}


\end{document}

The issue I am having: Clearly my comparison operator \xintifboolexpr, along with my grotesque code following it is the problem. I am seeking a simpler method to tell if the point C is along the arc of the circle between (-\tempax,-\tempay) and (-\tempbx,-\tempby).

EDIT: A correct solution from Sandy G's suggestion.

\documentclass{article}
\usepackage[left=2cm, right=2cm, top=2cm, bottom=1cm]{geometry}
\usepackage{amsmath,amsfonts,tikz,xintexpr,calc}
\usepackage{xfp}

\newcommand\circletest{
\begin{tikzpicture}[scale=0.6]

    \pgfmathsetmacro{\rndA}{rnd*360}
    \pgfmathsetmacro{\rndB}{rnd*360}
    \pgfmathsetmacro{\rndC}{rnd*360}

    %defining x and y coordinates of each point

    \def\radius{2}
    \def\xa{\fpeval{\radius*cosd(\rndA)}}
    \def\ya{\fpeval{\radius*sind(\rndA)}}
    \def\xb{\fpeval{\radius*cosd(\rndB)}}
    \def\yb{\fpeval{\radius*sind(\rndB)}}
    \def\xc{\fpeval{\radius*cosd(\rndC)}}
    \def\yc{\fpeval{\radius*sind(\rndC)}}

   %calculating side lengths of triangle
   \def\A{\fpeval{sqrt((\xb-\xc)^2 + (\yb-\yc)^2)}}
   \def\B{\fpeval{sqrt((\xa-\xc)^2 + (\ya-\yc)^2)}}
   \def\C{\fpeval{sqrt((\xa-\xb)^2 + (\ya-\yb)^2)}}

   %calculating angles of triangle
   \def\angleA{\fpeval{acosd((\B^2 + \C^2 -\A^2)/(2*\B*\C))}}
   \def\angleB{\fpeval{acosd((\C^2 + \A^2 -\B^2)/(2*\C*\A))}}
   \def\angleC{\fpeval{acosd((\A^2 + \B^2 -\C^2)/(2*\A*\B))}}

   %defining some coordinates
    \draw (0,0) coordinate (O);
    \draw (O) circle[radius=\radius];
    \draw (\xa,\ya) coordinate (A);
    \draw (\xb,\yb) coordinate (B);
    \draw (\xc,\yc) coordinate (C);

   %test if center is in circle
    \xintifboolexpr{((\angleA < 90) && (\angleB < 90)) && (\angleC < 90)}
     {\filldraw[color=green!80!black!100, fill=green!15] (A) -- (B) -- (C) -- cycle;} %true
     {\filldraw[color=red!80!black!100, fill=red!15] (A) -- (B) -- (C) -- cycle;} %false

   %Drawing points on top of line
    \draw[fill=black] (\xa,\ya) circle(1.5pt);
    \draw[fill=black] (\xb,\yb) circle(1.5pt);
    \draw[fill=black] (\xc,\yc) circle(1.5pt);
    \draw[fill=black] (O) circle(1.5pt);

\end{tikzpicture}}

\begin{document}

\foreach \x in {0,1,...,30}{
    \circletest
}

\end{document}

3
  • 1
    You could calculate the three angles. If any angle is obtuse (> 90°) then the center is outside the triangle. Otherwise the triangle contains the center.
    – Sandy G
    Mar 25, 2020 at 1:02
  • Great suggestion! I brought it to fruition in my edit :) Mar 25, 2020 at 2:30
  • Way cool observation. When one of the angles is 90 degrees the line connecting the two other angles runs right through the center. Apr 2, 2020 at 8:15

2 Answers 2

9

One can use the calc library and this prescription, which is very much like yours but perhaps a bit shorter. Using the calc library also allows us to avoid introducing new dimensions. Defining a pic has the advantage that you can use TikZ to arrange the drawings in any way you like.

\documentclass{article}
\usepackage[left=2cm, right=2cm, top=2cm, bottom=1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
What is the probability for the triangle containing the origin? Without loss of
generality we can take the angle of $A$ to be 0 (because one can rotate the
setup without changing the probability). Then the angle of $B$, $\beta$ can be
chosen to be between $0$ and $\pi$ (because one can reflect the setup at the
$x$--axis without changing the probability). Then the angle of $C$, 
 $\gamma$, needs to satisfy
\[ \pi<\gamma<\pi+\beta \] 
for the center to be inside the triangle, see Figure~\ref{fig:derivation}.
As $\beta$ scans the domain $[0,\pi]$, the probability for a triangle with
corners at random positions of the circle enclosing the center of the circle is
$1/4$. 
\begin{figure}[ht]
\centering
\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill},
    declare function={rr=2.5;}]
 \begin{scope}
  \draw (0,0) circle[radius=rr] (0,0) -- (rr,0) node[dot,label=right:$A$]{};
  \pgfmathsetmacro{\rndB}{rnd*90}
  \draw (1,0) arc[start angle=0,end angle=\rndB,radius=1] 
    node[midway,anchor=180+\rndB/2,circle]{$\beta$}
  (0,0) -- (\rndB:rr) node[dot,label={[anchor=\rndB+180]:$B$}]{};
  \draw[dashed] (180+\rndB:rr) -- (0,0) -- (180:rr);
  \draw[blue,thick] (180:rr) arc[start angle=180,end angle=180+\rndB,radius=rr]
  node[midway,anchor=\rndB/2,circle,align=right]{allowed\\ positions\\ for $C$};
 \end{scope}
 %
 \begin{scope}[xshift=2.8*rr*1cm]
  \draw (0,0) circle[radius=rr] (0,0) -- (rr,0) node[dot,label=right:$A$]{};
  \pgfmathsetmacro{\rndB}{90+rnd*90}
  \draw  (1,0) arc[start angle=0,end angle=\rndB,radius=1] 
    node[midway,anchor=180+\rndB/2,circle]{$\beta$}
    (0,0) -- (\rndB:rr) node[dot,label={[anchor=\rndB+180]:$B$}]{};
  \draw[dashed] (180+\rndB:rr) -- (0,0) -- (180:rr);
  \draw[blue,thick] (180:rr) arc[start angle=180,end angle=180+\rndB,radius=rr]
  node[midway,anchor=\rndB/2,circle,align=right]{allowed\\ positions\\ for $C$};
 \end{scope}
\end{tikzpicture}
\label{fig:derivation}
\end{figure}

\begin{figure}[ht]
\centering
\begin{tikzpicture}[pics/circletest/.style={code={
        \tikzset{circletest/.cd,#1}%
        \def\pv##1{\pgfkeysvalueof{/tikz/circletest/##1}}%
        \draw (0,0) coordinate (O) circle[radius=\pv{r}];
        \pgfmathsetmacro{\rndA}{rnd*360}
        \pgfmathsetmacro{\rndB}{rnd*360}
        \pgfmathsetmacro{\rndC}{rnd*360}
        \path (\rndA:\pv{r}) coordinate[label={[anchor=\rndA+180]:$A$}] (A)
         (\rndB:\pv{r}) coordinate[label={[anchor=\rndB+180]:$B$}] (B) 
         (\rndC:\pv{r}) coordinate[label={[anchor=\rndC+180]:$C$}] (C);
        \draw let \p1=(A),\p2=(B),\p3=(C),\p0=(O),
         \n1={(\x0-\x2)*(\y1-\y2)-(\x1-\x2)*(\y0-\y2)},
         \n2={(\x0-\x3)*(\y2-\y3)-(\x2-\x3)*(\y0-\y3)},
         \n3={(\x0-\x1)*(\y3-\y1)-(\x3-\x1)*(\y0-\y1)}
         in \pgfextra{\pgfmathtruncatemacro\itest{%
            ((\n1 < 0) || (\n2 < 0) || (\n3 < 0)) &&
            ((\n1 > 0) || (\n2 > 0) || (\n3 > 0))}}
         \ifnum\itest=0
          [color=green!80!black!100, fill=green!15] (A) -- (B) -- (C) -- cycle
         \else
          [color=red!80!black!100, fill=red!15]  (A) -- (B) -- (C) -- cycle
         \fi;
        \fill (O) circle[radius=1pt] node[below]{$O$}; 
    }},circletest/.cd,r/.initial=1]
 \path foreach \X in {1,...,5}
 {  foreach \Y in {1,...,5} {(3*\X,3*\Y) pic{circletest}}}; 
\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

An alternative proposal based on intersections. Construct a ray that leaves the circle from its center. If the number of intersections with the triangle is even, the center is outside of the triangle, otherwise it is inside.

\documentclass{article}
\usepackage[left=2cm, right=2cm, top=2cm, bottom=1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[pics/circletest/.style={code={
        \tikzset{circletest/.cd,#1}%
        \def\pv##1{\pgfkeysvalueof{/tikz/circletest/##1}}%
        \draw (0,0) coordinate (O) circle[radius=\pv{r}];
        \pgfmathsetmacro{\rndA}{rnd*360}
        \pgfmathsetmacro{\rndB}{rnd*360}
        \pgfmathsetmacro{\rndC}{rnd*360}
        \path (\rndA:\pv{r}) coordinate[label={[anchor=\rndA+180]:$A$}] (A)
         (\rndB:\pv{r}) coordinate[label={[anchor=\rndB+180]:$B$}] (B) 
         (\rndC:\pv{r}) coordinate[label={[anchor=\rndC+180]:$C$}] (C);
        \path[name path=triangle] (A) -- (B) -- (C) -- cycle;
        \path[name path=ray,overlay] (O) -- ({180+(\rndA+\rndB+\rndC)/3}:1.5*\pv{r});
        \draw[name intersections={of=triangle and ray,total=\t}]
         \ifodd\t
          [color=green!80!black!100, fill=green!15] (A) -- (B) -- (C) -- cycle
         \else
          [color=red!80!black!100, fill=red!15]  (A) -- (B) -- (C) -- cycle
         \fi;
    }},circletest/.cd,r/.initial=1]
 \path foreach \X in {1,...,5}
 {  foreach \Y in {1,...,5} {(3*\X,3*\Y) pic{circletest}}}; 
\end{tikzpicture}
\end{document}

enter image description here

This approach is limited by the accuracy of intersections, and can fail if the triangle is to thin, i.e. essentially a line.

P.S. These distributions are consistent with the actual probability.

enter image description here

7
  • You could calculate the three angles. If any angle is obtuse (> 90°) then the center is outside the triangle. Otherwise the triangle contains the center.
    – Sandy G
    Mar 25, 2020 at 1:04
  • @SandyG Thanks! I agree. The point of setting the above code in the way it is done is that the test works for any point. That is, given a triangle ABC and a point P, the test will decide whether or not P is inside the triangle, regardless of whether it is the center of the circumcircle. But I agree that one could devise simpler tests here, such as the one you describe, or the one that is implicit in the analytic discussion at the end.
    – user194703
    Mar 25, 2020 at 1:09
  • 1
    True. That way we can decide if a triangle chosen at random in the plane contains a random point in the plane! ;-) Just kidding. Nice solution!
    – Sandy G
    Mar 25, 2020 at 1:15
  • This is beautiful yet daunting solution. I have much to learn! Thanks for taking the time to do this. Mar 25, 2020 at 2:31
  • 1
    @LoganWeinert This is explained in section 18.3 Defining New Pic Types of pgfmanual v3.1.5. I like /.style={code={...}} better than /.pic because you can then also use /.style args={#1 and #2}{code={...}}, say. And I tried to explain why I like using pgf keys in this answer. It is basically because I always can add features without losing backward compatibility, and also because I personally can then remember the usage more easily.
    – user194703
    Mar 25, 2020 at 19:38
7

To satisfy my curiosity about the experimental probability, I did this in metapost. It seems to take about 100,000 triangles to consistently get the theoretical probability (i.e. 1/4) to 3 decimal places. If you comment the drawing commands to just print the result, then 1,000,000 runs only takes a few seconds. A portion of the out put for 20,000 inscribed triangles in 1mm circles :

enter image description here Run with lualatex:

\documentclass{article}
\usepackage{luamplib}
\usepackage{geometry}
\mplibnumbersystem{double}
\mplibtextextlabel{enable}
\mplibcodeinherit{enable}
\begin{document}
\begin{mplibcode}
    vardef triarray(expr r,n)=
        save x,tmp,width;
        width:=\mpdim{\linewidth} div r;
        count:=0;
        tot:=n;
        for j=0 upto n:
            % for the grid
            drawoptions(withpen pencircle scaled .1bp shifted ((r+.1)*(j mod width),-(r+.1)*(j div width)));
            for i=1 upto 3: x[i]:=uniformdeviate(8); endfor;
            % sort vals, probably didn't need to, but made things tidier.
            if x1>x2: 
                tmp:=x1; x1:=x2; x2:=tmp; 
            fi;
            if x2>x3:
                tmp:=x2; x2:=x3; x3:=tmp;
                if x1>x2:
                    tmp:=x1; x1:=x2; x2:=tmp; 
                fi;
            fi;
            % end sort
            % points on a circle in mp are mapped to the interval [0,8] with 0->0 and 8->360
            % reflected points rather than rotating arc
            if ((x1+4) mod 8>x2) and ((x1+4) mod 8<x3) and ((x3+4) mod 8>x1) and ((x3+4) mod 8<x2):
                fill fullcircle scaled r withcolor .2[white,green];
                count:=count+1;
            else:
                fill fullcircle scaled r withcolor .2[white,red];
            fi;
            % uncomment below for the triangles
            draw for i=1 upto 3: point x[i] of (fullcircle scaled r)-- endfor cycle; 
        endfor;
    enddef;
    beginfig(0);

    triarray(1mm,20000);

    endfig;
\end{mplibcode}
\begin{mplibcode}
beginfig(1);
        picture p; string s;
        s="$\frac{"&decimal(count)&"}{"&decimal(tot)&"}="&decimal(count/tot)&"$";
        p= s infont defaultfont scaled defaultscale;
        draw p;
endfig;
\end{mplibcode}
\end{document} 
3
  • This is really neat! Well done. Mar 31, 2020 at 2:33
  • Neat! The relative error goes like 1/sqrt(N), so even this is consistent with theory. ;-)
    – user194703
    Mar 31, 2020 at 2:44
  • @schrodinger'scat. Thanks for pointing out the error, it seemed weird it was taking so long, but that explains it :)
    – Scott H.
    Mar 31, 2020 at 3:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .