如何在不计算角度的情况下将圆弧与直线相交

如何在不计算角度的情况下将圆弧与直线相交

我想画一个与光学相关的图形,需要画几条具有不同开角的光束,并进行标记。

我目前想到的(归结为一个最小的例子)如下:

\documentclass{article}

\usepackage{tikz}
    \usetikzlibrary{intersections}
\usepackage[active,tightpage]{preview}

\begin{document}
\centering
\begin{preview}
\begin{tikzpicture}
    \coordinate (origin) at (0,0);
    \coordinate (screen-top) at (5,2);
    \coordinate (screen-bottom) at (5,-2);
    \draw [dashed,name path=beam] (origin) -- (screen-top) -- (screen-bottom) -- cycle;
    \draw [green,name path=circle] (0,0) circle (1);
    \path [name intersections={of=beam and circle,name=arc}];
    \draw [bend left] (arc-1) to node [midway,right] { \(\alpha\)} (arc-2) ;
\end{tikzpicture}
\end{preview}
\end{document}

现在我对这段代码有点疑问。我似乎无法自动找出交叉点(屏幕圆和光束)之间路径的正确弯曲角度,因此弯曲并不“美观”,如下图所示。

[bend left=19]我知道我可以使用而不是手动设置弯曲角度[bend left],但对所有图纸手动执行此操作似乎不符合 TikZ 的方式。

问题

有人能自动计算路径的弯曲角度,以便自动匹配通过该点的圆吗?自动意味着不同的光束角度和“屏幕”尺寸和位置(可以在代码中设置...)

答案1

这里有两个解决方案。第一个是裁剪一个圆。第二个是实际计算所需的角度,然后绘制圆弧。

代码如下

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}

\begin{document}

%option 1
\begin{tikzpicture}

\coordinate (origin) at (0,0);
\coordinate (screen-top) at (5,2);
\coordinate (screen-bottom) at (5,-2);

\draw [dashed,name path=beam] (origin) -- (screen-top) -- (screen-bottom) -- cycle;
\draw [green,name path=circle] (0,0) circle (1);

\clip (origin) -- (screen-top) -- (screen-bottom) -- cycle;
\draw (origin) circle[radius=1];
\node[right] at (1,0) {$\alpha$};

\end{tikzpicture}

%option 2
\begin{tikzpicture}
\pgfmathsetmacro{\radius}{1}

\coordinate (origin) at (0,0);
\coordinate (screen-top) at (5,2);
\coordinate (screen-bottom) at (5,-2);

\draw [dashed,name path=beam] (origin) -- (screen-top) -- (screen-bottom) -- cycle;
\draw [green,name path=circle] (0,0) circle (\radius);

\draw let
        \p{a} = ($(screen-top) - (origin)$),
        \p{b} = ($(screen-bottom) - (origin)$),
        \n{inner product} = {(\x{a}/1cm)*(\x{b}/1cm) + (\y{a}/1cm)*(\y{b}/1cm)},
        \n{la} = {veclen(\x{a}/1cm,\y{a}/1cm)},
        \n{lb} = {veclen(\x{b}/1cm,\y{b}/1cm)},
        \n{cosine} = {\n{inner product}/(\n{la}*\n{lb})},
        \n{half-angle} = {0.5*acos(\n{cosine})}
        in 
        (arc-2) arc[start angle=-\n{half-angle},end angle=\n{half-angle},radius = \radius] node[right] at ($(origin) + (\radius,0)$){$\alpha$};



\end{tikzpicture}


\end{document} 

Habi 的评论:您可以用以下行替换定义弧的代码,其中弧从 arc-1 开始:

(arc-1) arc[start angle=\n{half-angle},end angle=-\n{half-angle},radius = \radius] node[right] at ($(origin) + (\radius,0)$){$\alpha$};

答案2

使用tkz-euclide,您只需要一行。

\documentclass{scrartcl}
\usepackage{tkz-euclide} 
\usetkzobj{all} 
\begin{document}

\begin{tikzpicture}
  \tkzDefPoint(1,1){O}  \tkzDefPoint(3,-1){A}  \tkzDefPoint(4,2){B}
  \tkzDrawArc[R with nodes, color=blue](O,2cm)(A,B)
  \tkzDrawArc[R with nodes, color=red](O,2cm)(B,A)
  \tkzDrawLines[add = 0 and .5](O,A O,B)
  \tkzDrawPoints(O,A,B)
  \tkzLabelPoints[below](O,A,B)
\end{tikzpicture} 

\end{document}

在此处输入图片描述

答案3

然而,这并不是对你的问题的直接回答,因为你知道射线的起源在哪里以及它们通向哪些点,所以这里是另一种方法:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.markings}
\tikzset{arcnode/.style={
            decoration={
                        markings, raise = 2mm,  
                        mark=at position 0.5 with { 
                                    \node[inner sep=0] {#1};
                        }
            },
            postaction={decorate}
      }
}

\newcommand*\marktheangle[5]{
            \draw[thick,arcnode={#5}] let \p2=($(#2)-(#1)$),%
                        \p3=($(#3)-(#1)$),%
                        \n2 = {atan2(\x2,\y2)},%
                        \n3 = {atan2(\x3,\y3)}%
                        in ($(\n2:#4)+(#1)$) arc (\n2:\n3:#4);
            }

\begin{document}
\begin{tikzpicture}
\draw[style=help lines] (-0.5,-0.5) grid[step=1cm] (4.5,2.5);
\node[draw,red,circle,minimum width=2cm] (O1) at (1,1) {}; 
\node[draw,red,circle,minimum width=1cm] (O2) at (3.5,1.5) {}; 
% Define 4 points "out there"
\node (a) at (1.5,-0.8) {$a$};
\node (b) at (-5mm,-5mm)  {$b$};
\coordinate (c) at (4.5,1);
\coordinate (d) at (3,2.5);

\draw[loosely dashed,blue] (O1.base) -- (a) (O1.base) -- (b); % Draw the lines
\draw[loosely dashed,green] (O2.base) -- (c) (O2.base) -- (d); %Draw the lines

% the code for the arc labeling
\marktheangle{O1}{a}{b}{1cm}{$\alpha$}
\marktheangle{O2}{d}{c}{5mm}{$\beta$} % Mind the order for label positioning.
\end{tikzpicture}
\end{document}

在此处输入图片描述

现在,在回答这个问题时Cjorssen 提到,在新版本中,可以直接在弧上放置节点。因此,如果您希望使用 CVS 版本,整个标记代码可能很快就会过时。可以进一步将这些代码嵌入到一个tikzset具有更好密钥处理的漂亮定义中。

相关内容