2

I want to do a loop until there is intersection points between two paths

edit

I oblic lines until they don't intersect the circle. When there is zero intersection points I have an error. Is there a way to deal with zero intersection points ?

\documentclass[tikz,margin=3pt]{standalone}
%\usepackage{}
\usetikzlibrary{calc,intersections}

\def\Dir{45}
\def\DirStep{1pt}
\def\Length{10}

\begin{document}

\begin{tikzpicture}

\coordinate (Depart) at (0,0) ;

\begin{scope}[shift=(Depart),rotate=\Dir]
\clip[name path=bob,draw] (0,0) circle (1) ;

\pgfmathtruncatemacro\i{0}

\loop
    \draw[name path=trait,shift={(0,\i*\DirStep)}] (-5,0)--(5,0) ;
    \path[name intersections={%
        of=trait and bob,
        name=\i,
        sort by=trait,
        total=\t}]
        \pgfextra{\xdef\InterNb{\t}} ;
    \pgfmathtruncatemacro\i{\i+1}
    \ifnum\i<10
    % I want :
    %\ifnum\\InterNb>0
\repeat

\end{scope}

\node   {\InterNb} ;
\end{tikzpicture}

\end{document}
2
1

I didn't understand what exactly happening but this should work;

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}

\def\Dir{45}
\def\DirStep{1}
\def\Length{10}

\begin{document}
\begin{tikzpicture}

\coordinate (Depart) at (0,0) ;

\begin{scope}[shift=(Depart),rotate=\Dir]
\clip[name path=bob,draw] (0,0) circle (1) ;

\foreach\i[count=\xi from 0] in {0,\DirStep,...,10}{
    \draw[name path=trait,shift={(0,\i pt)}] (-5,0)--(5,0) ;
    \path[name intersections={%
        of=trait and bob,
        name=\i,
        sort by=trait,
        total=\t}]
        \pgfextra{\xdef\InterNb{\t}};
        \ifnum\InterNb>0\relax
          \breakforeach
        \fi
}
\end{scope}
\node   {\InterNb} ;
\end{tikzpicture}

\end{document}

Sorry, I can't compile it right now I'll update the picture.

3
  • It compiles but it doesn't do what I want. See my edit.
    – Tarass
    Jun 10 '14 at 10:25
  • 1
    @Tarass It's probably the sort by key messing it up when there is no intersection. Can you try without?
    – percusse
    Jun 10 '14 at 11:03
  • It works without. I suppose I have to do without ... Thank you.
    – Tarass
    Jun 10 '14 at 13:56

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.