绘制以半圆结尾的箭头

绘制以半圆结尾的箭头

我正在尝试用 tikz 绘制一个相当简单的场景。 在此处输入图片描述

我遇到的问题是如何定义半圆上的端点。我尝试实现一个用于交叉点检测的算法,伪代码可以在以下位置找到:环线交叉口。但是它不能正常工作。此外,如果我使用该\ifthenelse子句,它就不会编译。

关于如何实现这个功能有什么建议吗?

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{graphics, tkz-berge, tkz-graph}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>

\tikzset{isometricXYZ/.style={x={(-0.866cm,-0.5cm)}, y={(0.866cm,-0.5cm)}, z={(0cm,1cm)}}}

%% document-wide tikz options and styles
\begin{document}
\begin{tikzpicture} [scale=4, line join=round,
        opacity=.75, fill opacity=.35, text opacity=1.0,%
        >=latex,
        inner sep=0pt,%
        outer sep=2pt,%
    ]
% First argument is a ray angle, second argument is an offset along x-axis.
    \newcommand{\ray}[2]{
      \def\r{1} % sphere radius
      \def\l{2} % line length
      \def\xc{#2} % offset

      % Sphere center
      \def\Cx{0}
      \def\Cy{0}

      % Ray start
      \def\Ex{(\xc + (\l*cos(#1)))}
      \def\Ey{(\l*sin(#1))}

      % Ray end
      \def\Lx{\xc}
      \def\Ly{0}

      % Vector from ray start to end
      \def\dx{(\Lx -\Ex)}
      \def\dy{(\Ly -\Ey)}

      % Vector from ray start sphere center
      \def\fx{(\Ex - \Cx)}
      \def\fy{(\Ey - \Cy)}

      % solve eq
      \def\a{(\dx * \dx + \dy * \dy)}
      \def\b{(2 * \fx * \dx + \fy * \dy)}
      \def\c{(\fx * \fx + \fy * \fy - \r * \r)}
      \def\discriminant{(\b*\b - 4*\a*\c)}
      \ifthenelse{{\discriminant} < 0}
      {
          \def\endc{(\xc, 0)}
      }
      {
          \def\sqdiscriminant{sqrt(\discriminant)}
          \def\t{(-\b +\sqdiscriminant)/(2*\a)}
          \def\endc{({\Ex + \t*\dx}, {\Ey + \t*\dy})}
      }

      \def\startc{({\Ex}, {\Ey})}
      \draw [->] \startc -- \endc;
    }

    \draw[fill=gray, fill opacity=0.2] (1, 0) arc (0:180:1);

    \draw [dotted] (0, 0) -- ({cos(30)}, {sin(30)}) node[above right] {$\alpha_1$};
    \draw [dotted] (0, 0) -- ({cos(150)}, {sin(150)}) node[above left] {$\alpha_2$};
    \draw [dotted] (0, 0) -- (0, 1.1);

    \node[below right] (halfpi) at (1, 0) {$\frac{\pi}{2}$};
    \node[below left] (minushalfpi) at (-1, 0) {$-\frac{\pi}{2}$};

    \foreach \x in {2}
    {
      \ray{55}{\x}
    };

\end{tikzpicture}
\end{document}

我测试了以下两种解决方案,但在这两种情况下,我都错误地选择了某些交叉点。我猜那是因为我在所有情况下都选择了第一个交叉点,但不幸的是,这并不总是正确的。有没有办法总是选择最近,不是第一个,路口? 在此处输入图片描述

pps :) 没关系。我通过反转路径方向修复了这个问题。

答案1

您可以简单地使用该intersections库来避免手动计算射线和圆的交点。它甚至适用于中等复杂的路径,例如组合的半圆和直线路径。

为了得到交叉点最近对于“光源”,您可以从光源处开始射线,然后使用选项sort by=ray。这样,(interection-1)总是最接近光源的那个。

是的,TikZ 使用 进行所有计算和排序TeX。巫术!

许多交叉路口

\documentclass[tikz,margin=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture} [scale=4, line join=round, >=latex]
    % Name this path for intersections
    \draw[fill=gray, fill opacity=0.2, name path=outline]
        (-2,0) -- (-1,0) arc (180:0:1)-- (2,0) ;

    \draw [dotted] (0, 0) -- ({cos(30)}, {sin(30)}) node[above right] {$\alpha_1$};
    \draw [dotted] (0, 0) -- ({cos(150)}, {sin(150)}) node[above left] {$\alpha_2$};
    \draw [dotted] (0, 0) -- (0, 1.1);
    \node[below right] (halfpi) at (1, 0) {$\frac{\pi}{2}$};
    \node[below left] (minushalfpi) at (-1, 0) {$-\frac{\pi}{2}$};

    % Properties of the "light source"
    \def\angle{45}
    \def\width{2.8cm}

     % Draw the light source:
    \draw[very thick] (\angle:2.5cm) coordinate(light)
        ++(\angle+90:0.5*\width) -- +(\angle-90:\width);
    % Draw a number of rays:
    \foreach \x in {-8,...,8} {
        % Place a path from light source through circle and name it:
        \path[name path=ray,overlay] (light) ++(\angle+90:\x*\width/17)
            coordinate(start) -- +(180+\angle:5cm);
        % Draw the arrow from light source to first intersection:
        \draw[->, name intersections={of=outline and ray, sort by=ray}]
            (start) -- (intersection-1);
    }
\end{tikzpicture}
\end{document}

答案2

如果你想有选择地绘制交点,那么你也可以实现是否找到交点。但交点计算是一件很微妙的事情,你不能总是依赖它的精度。

\documentclass[tikz]{standalone}
\usetikzlibrary{intersections,calc}
\begin{document}
\begin{tikzpicture}[>=latex]
\begin{scope}[fill opacity=.35]
\fill[blue] (4cm,0)--++(40:1cm) arc (40:180:1cm);
\fill[gray] (4cm,0)--++(40:1cm) arc (40:0:1cm);
\end{scope}
\draw[ultra thick] (1cm,2.5cm) coordinate (ls) -- ++(30:4cm) coordinate (le); 
\draw[ultra thick,name path=a] (2cm,-2cm)--++(7cm,0);
\draw[ultra thick,name path=b] (5cm,0) arc (0:180:1cm);
\foreach \x[count=\xi] in {0.1,0.15,...,0.9}{
\path[name path global=ray\xi,overlay] ($(ls)!\x!(le)$) --++(-60:15cm);
\draw[->,
    name intersections={of=a and ray\xi,name=intaray\xi},
    name intersections={of=b and ray\xi,name=intbray\xi}] 
\pgfextra{% Test for the shape name existence
\csname pgfutil@ifundefined\endcsname{pgf@sh@ns@intbray\xi-1}{\def\temp{a}}{\def\temp{b}}%
}
($(ls)!\x!(le)$) -- (int\temp ray\xi-1);
} 
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容