# Could you tell me how to Count intersection points in tikz?

in this picture: there are 6 intersection points. I need 6 auto change when number of intersection points change. Can you help me?

my code:

\documentclass[tikz,border=5mm]{standalone}

\usepackage[utf8]{vietnam}

\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}[smooth]

\draw[gray!30](-3,-3) grid (3,3);

\draw[->](-3,0)--(3,0) node[below]{$x$};

\draw[->](0,-3)--(0,3) node[right]{$y$};

\draw[blue,name path=hamso] plot[domain=-2.1:2.1] (\x,{(\x)^3-3*(\x)})
node[right, red] {$y=x^3-3x$};

\draw[red,name path=tron] (0,0) circle(2);

\fill[violet,name intersections={of=hamso and tron,name=A,total=\t}]

\foreach \i in {1,...,\t} {(A-\i) circle (2pt) node[above]{\i}};
\path (current bounding box.south) node[below]{There are: \fbox{6} intersection points};
\end{tikzpicture}

\end{document}
• It's already available. You're doing total=\t, so just write \fbox{\t}. – Henri Menke Feb 21 at 5:07
• @HenriMenke \fbox{\t} do not work. May be total=\t is locally deffined only. – Black Mild Feb 21 at 5:28

I believe Henri wanted to tell you that you only make \t global (or smuggle it out of the path). One way of accomplishing this is to add \pgfextra{\xdef\myt{\t}} to the path, which stores \t in the global macro \myt. (Advanced possibilities of smuggling can be found e.g. here, but I think here you almost necessarily need to globalize the macro.) EDIT: Made this handle the case of 0 intersections, too.

\documentclass[tikz,border=5mm]{standalone}

\usepackage[utf8]{vietnam}

\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}[smooth]

\draw[gray!30](-3,-3) grid (3,3);

\draw[->](-3,0)--(3,0) node[below]{$x$};

\draw[->](0,-3)--(0,3) node[right]{$y$};

\draw[blue,name path=hamso] plot[domain=-2.1:2.1] (\x,{(\x)^3-3*(\x)})
node[right, red] {$y=x^3-3x$};

\draw[red,name path=tron] (0,0) circle(2);

\fill[violet,name intersections={of=hamso and tron,name=A,total=\t}]
\ifnum\t>0
foreach \i in {1,...,\t} {(A-\i) circle (2pt) node[above]{\i}}
\fi
\pgfextra{\xdef\myt{\t}};
\path (current bounding box.south) node[below]{There are: \fbox{\myt} intersection points};
\end{tikzpicture}

\end{document}

If you change the cubic curve to

\draw[blue,name path=hamso] plot[domain=-2.1:2.1] (\x,{7+(\x)^3-3*(\x)})
node[right, red] {$y=7+x^3-3x$};

you'll get

• page 145 of the manual states -- /tikz/intersection/total=(macro) This key means that the total number of intersections found will be stored in macro --- could you please tell how to print the output of this macro – js bibra Feb 21 at 6:19
• @jsbibra Yes, this is what the macro says, but the macro is a local macro, i.e. it is only "known" inside the path. You can print it out by adding \pgfextra{\typeout{I found \t\space intersections}}; to the path. But this literally prints it it out. – Schrödinger's cat Feb 21 at 6:27
• thank you so much. @Schrödinger'scat You can tell me when they do not have intersection, how to handle errors? – Jack Nguyễn Feb 21 at 6:36
• @JackNguyễn Good point! Added. – Schrödinger's cat Feb 21 at 6:42