建模隨機產生的內接三角形

建模隨機產生的內接三角形

我正在建模的問題:在圓上隨機選出三個點。這三個點所構成的三角形包含圓心的機率是多少?

概念理解:假設我們固定三個點中的兩個,稱它們為 A 和 B。的A 點和B 點。

我想要發生什麼:隨機產生的內切三角形,包含中心時填滿綠色,不包含中心時填滿紅色。我還想記錄成功和失敗的次數來計算實驗機率。

在此輸入影像描述

幾個關鍵的事情\pgfextractx:我可以使用和存取每個點的 x 和 y 座標\pgfextracty。我的方法是使用 來測試點 C 是否位於 A 和 B 的 x 座標和 y 座標之間\xintifboolexpr,但是,這是有缺陷的。

最小工作範例:

\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}

我遇到的問題:顯然我的比較運算子\xintifboolexpr以及後面的怪異程式碼就是問題所在。我正在尋找一種更簡單的方法來判斷點 C 是否沿著(-\tempax,-\tempay)和之間的圓弧(-\tempbx,-\tempby)

編輯:Sandy G 建議的正確解決方案。

\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}

答案1

一個可以使用calc圖書館和這個處方,這與您的非常相似,但可能更短一些。使用該calc庫還可以讓我們避免引入新的維度。定義一個pic好處是可以使用 TikZ 以您喜歡的任何方式排列繪圖。

\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}

在此輸入影像描述

基於交叉點的替代提案。構造一條從圓心離開圓的射線。若與三角形的交點個數為偶數,則中心在三角形外,否則在三角形內。

\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}

在此輸入影像描述

這種方法受到相交精度的限制,如果三角形太細(即本質上是一條線),則可能會失敗。

PS 這些分佈與實際機率一致。

在此輸入影像描述

答案2

為了滿足我對實驗機率的好奇,我在metapost.似乎需要大約 100,000 個三角形才能一致地將理論機率(即 1/4)精確到小數點後 3 位。如果您註釋繪圖命令以僅列印結果,則 1,000,000 次運行只需幾秒鐘。 1 毫米圓內 20,000 個內切三角形的輸出的一部分:

在此輸入影像描述 運行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} 

相關內容