I am trying to draw a fairly simple scene with tikz. enter image description here

The issue i have is with defining the end points on half circle. I tried to implement an algorithm for intersection detection, pseudocode can be found at Circle-Line intersection. However it does not work as it should. In addition, it does not compile if i use the \ifthenelse clause.

Any suggestions on how to get this to work?

\usepackage{graphics, tkz-berge, tkz-graph}

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

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

      % Sphere center

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

      % Ray end

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


I tested both solutions below, but in both cases i get some intersections picked up incorrectly. I guess that is because i take the first intersection in all cases, which is unfortunately not always the right one. Is there a way to always choose the closest, not the first, intersection? enter image description here

p.p.s :) nvm. I fixed it by reversing the path direction.

  • When trying to compile your example as given, I get ! Missing number, treated as zero. on line 79.
    – Fritz
    Sep 24, 2014 at 14:13
  • The shorten option for the draw command is probably the best way to do it, now the difficult part is to find the intersection points between the arrows and the circle to compute the right length to remove.
    – Thomas
    Sep 24, 2014 at 14:14
  • 1
    @Thomas: No, the intersections library is the easiest way to calculate intersections. ;-)
    – Fritz
    Sep 24, 2014 at 14:28
  • What about starting the arrows from the circle using polar coordinates?
    – Luigi
    Sep 24, 2014 at 14:32
  • @Luigi: That makes it difficult to space the rays evenly. You'd still have to calculate the polar angle by hand.
    – Fritz
    Sep 24, 2014 at 15:13

2 Answers 2


You can simply use the intersections library to avoid calculating the intersections of the rays and the circle manually. It even works for moderately complex paths like the combined half-circle-and-line path.

In order to get the intersection closest to the "light source", you can start the ray at the light source and then use the option sort by=ray. This way, (interection-1) is always the one closest to the source.

And yes, TikZ does all the calculation and sorting using TeX. Witchcraft!

Many intersections


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

     % 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);
  • @BlazBratanic: I've updated it with the "correct" solution for obtaining the intersection which is closest to the source.
    – Fritz
    Sep 25, 2014 at 12:01
  • @Fritz Thanks! Both for the fixed solution and for the clarification of the notification rules. Sep 25, 2014 at 17:48

If you want to selectively draw the intersection points then you can also implement whether an intersection is found or not. But intersection computation is a delicate thing and you can't always rely on its precision.

\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);
\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);
    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);

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.