使用随机数指定范围

使用随机数指定范围

我正在使用下面的代码来说明双角定理,尽管它有时有效,但我喜欢一种定义随机数的方法,因此它总是采用如屏幕截图所示的形式。

\documentclass[border=3.141592=12mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning,
                quotes}
            
\usetikzlibrary {angles,quotes}
\usetikzlibrary{calc,positioning}
            
\pgfmathsetseed{\pdfuniformdeviate 10000000} 

\begin{document}
\begin{tikzpicture}[
trig format=rad,
dot/.style = {circle, fill, inner sep=1pt, outer sep=0pt},
ang/.style = {draw=red, <->,
              angle radius = 3mm,
              angle eccentricity=1.2,
          }
                        ]
\pgfmathsetmacro{\r}{2}
\pgfmathsetmacro{\R}{\r+0.3}
% circle
\draw (0,0) coordinate (O) circle[radius=\r];
 % triangles' corners coordinates and labels
\foreach \c/\l in {rand/A, rand/B, rand/C}  % define random coefficients 
                                            % for calculations of triangle's 
                                            % corners coordinates on circle
                                            % and define corners names
{
\pgfmathsetmacro{\C}{2*pi*\c}               % calculate triangle coordinates
\node (\l) [dot] at (\C:\r) {};             % draw dots at triangle corners
% \draw[-Stealth, gray, very thin]          % draw arrows from circle center 
                                            % to triangle's corners, 
                                            % if not needed, just delete this line 
        (0,0)   -- (\l);
  \path (\l) -- (\C:\R) node {\l};          % define corners labels coordinates,
                                            % they are in direction of vector 
                                            % from circle origin to dot node
}
    % triangle
\draw[cyan]   
        (B) -- (O);           % draw trangle
\draw[cyan]        
        (C) -- (O);
\draw[cyan]
        (C) -- (A);
\draw[cyan]
        (A) -- (B);        
    
 \draw (B) -- (O) -- (C)
 pic [draw=green!50!black, fill=green!5, angle radius=4mm,
 "$\theta$"] {angle = B--O--C};  

 \draw (B) -- (A) -- (C)
 pic [draw=green!50!black, fill=green!5, angle radius=4mm,
 "$\theta$"] {angle = B--A--C};       
    
     \end{tikzpicture}
  \end{document}

所需效果的屏幕截图

答案1

像这样:

在此处输入图片描述

我(目前)还不知道为什么角度 $\theta:1$ 的弧是凹的而不是凸的。无论如何,现在三角形角的可能角度被限制在由 限制的选定角度范围内0.3*\rand

编辑: 同时,@hpekristiansen 建议,可以通过使用度数而不是弧度作为角度单位来避免绘制角度的问题。在代码的第一个版本中使用它们时,必须进行以下更改:

  • 删除代码行trig format=rad,(现在已注释
  • 在循环中替换\foreach \c/\l in {4/A, 1/B, -4/C}\foreach \c/\l in {45/A, 180/B, 315/C}
  • 在计算三角形角坐标时替换\pgfmathsetmacro{\C}{4/\c + 0.3*rand}\pgfmathsetmacro{\C}{\c + 15*rand}
\documentclass[border=3.141592mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{angles, arrows.meta,
                backgrounds,
                positioning,
                quotes}

\pgfmathsetseed{\pdfuniformdeviate 10000000}

\begin{document}
\begin{tikzpicture}[
% trig format=rad,
dot/.style = {circle, fill, inner sep=1pt, outer sep=0pt},
ang/.style = {draw=green!50!black, fill=green!10,
              angle radius = 4mm,
              angle eccentricity=0.9,
              font=\footnotesize,
              anchor=west
            }
                        ]
\pgfmathsetmacro{\r}{2}
\pgfmathsetmacro{\R}{\r+0.3}
% circle
\draw (0,0) coordinate (O) circle[radius=\r];
 % triangles' corners coordinates and labels
\foreach \c/\l in {45/A, 180/B, 315/C}{
\pgfmathsetmacro{\C}{\c + 15*rand}               
\node (\l) [dot] at (\C:\r) {};             
% 
\path (\l) -- (\C:\R) node {\l};          
}
% triangle
\draw[cyan, semithick] (A) -- (B) -- (C) -- (O) -- (A);
% angles labels
\scoped[on background layer]
{
\pic [ang, "$\theta_1$"]            {angle = C--B--A};
\pic [ang, "$\theta_2=2\theta_1$"]  {angle = C--O--A};
}
\end{tikzpicture}
\end{document}

代码稍微短一些,删除了所有不需要的代码。

答案2

这是我对这个问题的看法。请注意,我将 O 移到了弧 BC 中点的同一侧。我也不允许两个点的距离小于 10^\circ,并确保 B 和 C 位于 A 的相对侧。

\documentclass[border=12mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning,
                quotes,calc}
            
\usetikzlibrary {angles,quotes}
\usetikzlibrary{calc,positioning}
            
\pgfmathsetseed{\pdfuniformdeviate 10000000} 

\begin{document}
\begin{tikzpicture}[
%trig format=rad,
dot/.style = {circle, fill, inner sep=1pt, outer sep=0pt},
ang/.style = {draw=red, <->,
              angle radius = 3mm,
              angle eccentricity=1.2,
          }
                        ]
\pgfmathsetlengthmacro{\r}{2cm}
\pgfmathsetlengthmacro{\R}{\r+0.3cm}
% circle
\draw (0,0) coordinate (O) circle[radius=\r];
 % triangles' corners coordinates and labels
\pgfmathsetmacro{\a}{random(-180,180)}%
\pgfmathsetmacro{\b}{random(\a+10,\a+180)}% \b ? \a
\pgfmathsetmacro{\c}{random(\a-10,\b-350)}% \c < \a
\coordinate(A) at (\a: \r);
\coordinate(B) at (\b: \r);
\coordinate(C) at (\c: \r);
\coordinate(D) at ({0.5*(\b+\c)}: \r);
\node at (\a: \R) {A};
\node at (\b: \R) {B};
\node at (\c: \R) {C};
\node at ($(O)!0.3cm!(D)$) {O};
\draw[orange]   
        (B) -- (O) -- (C);           % draw trangle
\draw[cyan]
        (B) -- (A) -- (C);
\draw pic [draw=green!50!black, fill=green!5, angle radius=4mm,opacity=0.5,
 "$\phi$"] {angle = B--O--C};
\draw pic [draw=green!50!black, fill=green!5, angle radius=4mm,opacity=0.5,
 "$\theta$"] {angle = B--A--C};  

\draw[green] (B) arc[start angle=\b, end angle={\c+360}, radius=\r];
\end{tikzpicture}
\end{document}

相关内容