在 \foreach 中评估结果

在 \foreach 中评估结果

我正在尝试制作全反射动画,所以我需要评估 \foreach 循环中的第二个角度,我试过了\pgfmathparse{asin(1.33*sin(deg(\th))},但出现了错误!missing number, treated as zero

    \documentclass[border=10pt,tikz]{standalone}
    \usepackage{xcolor}
    \definecolor{lava}{rgb}{0.81, 0.06, 0.13}
    \definecolor{myblue}{rgb}{0.0, 0.30, 0.60}
    \usepackage{tikz}
    \usepackage{tikz}
    \usetikzlibrary{arrows,shapes,positioning}
    \usetikzlibrary{decorations.markings}
    \tikzstyle arrowstyle=[scale=1]
    \tikzstyle directed=[postaction={decorate,decoration={markings,
        mark=at position .65 with {\arrow[arrowstyle]{stealth}}}}]
    \tikzstyle reverse directed=[postaction={decorate,decoration={markings,
        mark=at position .65 with {\arrowreversed[arrowstyle]{stealth};}}}]
        
        \begin{document}
        
            \foreach \th in {180,185,190,...,265}
        {\begin{tikzpicture}[scale=0.75]
        % define coordinates
        \coordinate (O) at (0,0) ;
        \coordinate (A) at (0,4) ;
        \coordinate (B) at (0,-4) ;
        \def\n1{1.33}
        \def\n2{1}
        \def\r{4cm}
    
        
        % media
        \fill[blue!10!,opacity=.3] (-4,0) rectangle (4,4);
        \fill[myblue,opacity=.3] (-4,0) rectangle (4,-4);
        \node[right] at (2,2) {Fast $n_2$};
        \node[left] at (-2,-2) {Slow $n_1$};
    
        % axis
        \draw[dash pattern=on5pt off3pt] (A) -- (B) ;
    
        % rays
        \draw[red,ultra thick,directed] (\th:\r) -- (O);
        \pgfmathparse{asin(1.33*sin(deg(\th))}
        \draw[blue,directed,ultra thick,,reverse directed] (O) -- (\pgfmathresult:\r);
   
    
    \end{tikzpicture}}
    
        \end{document}

我正在尝试在下面提供的代码中添加角度,并在 时删除灰色射线\pgfmathresult<49,但是对于第一个,我收到了错误“缺少数字”,对于第二个,命令行不应该\fi \draw[gray,directed,ultra thick] (O) -- ({-(90-\th)}:\r);意味着除非 ,否则不会绘制该线\pgfmathresult>49

    \begin{document}

\foreach \th in {0,4,...,48,48.8,54,...,90} {%    
        \begin{tikzpicture}[scale=0.75,ang/.style = {draw, Straight Barb-Straight Barb, anchor=center}]
            % define coordinates

            \def\n1{1.33}
            \def\n2{1}
            \def\r{4cm}
            \coordinate (O) at (0,0) ;
            \coordinate (A) at (0,4) ;
            \coordinate (B) at (0,-4) ;
            \coordinate (C) at ({90-asin(1.33*sin(\th)) }:\r);
           \coordinate (D) at  ({270-\th}:\r);
           \coordinate (E) at  (90:\r);
           \coordinate (F) at  (270:\r);


            % media
            \fill[blue!10!,opacity=.3] (-4,0) rectangle (4,4);
            \fill[myblue,opacity=.3] (-4,0) rectangle (4,-4);
            \node[right] at (2,2) {Fast $n_2$};
            \node[left] at (-2,-2) {Slow $n_1$};

            % axis
            \draw[dash pattern=on5pt off3pt] (A) -- (B) ;

            % rays


            \draw[lava,ultra thick,directed] ({270-\th}:\r) -- (O);
            \pgfmathparse{int(\th)}
            \ifnum\pgfmathresult<49
                \draw[myblue,directed,ultra thick] (O) -- ({90-asin(1.33*sin(\th)) }:\r);
            \fi
            \draw[gray,directed,ultra thick] (O) -- ({-(90-\th)}:\r);
                \pic [draw,Straight Barb-Straight Barb, anchor=center, angle radius = 4mm, angle eccentricity=1.5, "$ \theta_i$"] {angle = F--O--D};
    \pic [draw,Straight Barb-Straight Barb, anchor=center,angle radius = 4mm, angle eccentricity=1.5,  "$ \theta$"] {angle = E--O--C};
        \end{tikzpicture}
    }

\end{document}

答案1

出现此错误的原因是,当角度大于 90 时,不存在 acrsine。限制折射后,会发生全内反射。因此,您无法再尝试计算折射光线的角度。

\documentclass[border=10pt,tikz]{standalone}
\usepackage{xcolor}
\definecolor{lava}{rgb}{0.81, 0.06, 0.13}
\definecolor{myblue}{rgb}{0.0, 0.30, 0.60}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}
\usetikzlibrary{decorations.markings}
\tikzstyle arrowstyle=[scale=1]
\tikzstyle directed=[postaction={decorate,decoration={markings,
    mark=at position .65 with {\arrow[arrowstyle]{stealth}}}}]
\tikzstyle reverse directed=[postaction={decorate,decoration={markings,
    mark=at position .65 with {\arrowreversed[arrowstyle]{stealth};}}}]

    
\begin{document}

\foreach \th in {0,4,...,48,48.8,54,...,90} {%    
        \begin{tikzpicture}[scale=0.75]
            % define coordinates
            \coordinate (O) at (0,0) ;
            \coordinate (A) at (0,4) ;
            \coordinate (B) at (0,-4) ;
            \def\n1{1.33}
            \def\n2{1}
            \def\r{4cm}


            % media
            \fill[blue!10!,opacity=.3] (-4,0) rectangle (4,4);
            \fill[myblue,opacity=.3] (-4,0) rectangle (4,-4);
            \node[right] at (2,2) {Fast $n_2$};
            \node[left] at (-2,-2) {Slow $n_1$};

            % axis
            \draw[dash pattern=on5pt off3pt] (A) -- (B) ;

            % rays


            \draw[red,ultra thick,directed] ({270-\th}:\r) -- (O);
            \pgfmathparse{int(\th)}
            \ifnum\pgfmathresult<49
                \draw[blue,directed,ultra thick] (O) -- ({90-asin(1.33*sin(\th)) }:\r);
            \fi
            \draw[gray,directed,ultra thick] (O) -- ({-(90-\th)}:\r);
        \end{tikzpicture}
    }

\end{document}

在此处输入图片描述

相关内容