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}
Tarass
  • 16,912

1 Answers1

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.

percusse
  • 157,807