4

I want to draw some arcs inside a circle: they go from angle (120i+30)/2^j to (120i+90)/2^j for all i,j. I compute them with TikZ as follows:

\documentclass{minimal}
\usepackage{tikz}
%\usepackage{fp}\usetikzlibrary{fixedpointarithmetic}
\usetikzlibrary{calc,intersections}

\begin{document}
\def\myarc#1#2{\path[name path=a] ({#1}:1) -- ($({#1}:1)!10cm!270:(0,0)$);
  \path[name path=b] ({#2}:1) -- ($({#2}:1)!10cm!90:(0,0)$);
  \draw[name intersections={of=a and b,by=t}] (t) let \p1 = ($(t)-({#1}:1)$) in
  circle ({veclen(\x1,\y1)});
}

\begin{center}
  \begin{tikzpicture}[scale=3]
    \draw (0,0) circle (1cm);
    \clip (0,0) circle (1cm);
    \begin{scope}[very thick]\myarc{120}{240}\end{scope}
    \myarc{300}{60}
    \foreach\j/\k in {1/360,2/720,4/1440,8/2880,16/5760,32/11520} { %,64/23040} {
      \foreach\i in {150,330,...,\k} {\myarc{\i/\j}{(\i+60)/\j}};
    }
  \end{tikzpicture}
\end{center}
\end{document}

The result is fine up to j=16, but for the next one (j=32) some of the circles are not correctly placed; and for j=64 TikZ refuses to compile the picture, with a "dimension too large" error.

Does someone know how to fix this?

Thanks!

2 Answers 2

1

The dimension too large error appears because you need to use a huge \k in your approach. I agree with caverac in that you do not have to compute the intersections, but I disagree with the computation of the centers of the circles. In your code, these are at the intersections (which BTW could easier be obtained with tangent cs:). In principle you do not have to draw the full circles, but only arcs, but because of the way TikZ treats angles that overshoot 360 degrees I was unable to make this work. UPDATE with a big thanks to grok: changed the ordering of how to divide by 2^\k.

\documentclass[border=3pt]{standalone}
\usepackage{tikz}
\newcommand{\DrawArc}[3][]{ % from angle #2-#3 to #2+#3
  \draw[#1] ({#2}:{sec(#3)}) circle ({tan(#3)});
}

\begin{tikzpicture}[scale=3]
  \draw[very thin] (0,0) circle (1cm);
  \clip (0,0) circle (1cm);
  \DrawArc[thick]{180}{60}
  \foreach\k in {0,...,7} {
    \pgfmathsetmacro{\j}{2*2^\k}
    \foreach\i in {1,...,\j} {\DrawArc{180/2^\k*\i}{30/2^\k}};
  }
\end{tikzpicture}

\end{document}

enter image description here

5
  • Thanks! Indeed, your method of computing the centers (with 1/cos and tan) is much better than mine. However, the "overflow" problem persists when k=6. The solution, so it seems, is to compute (180/2^\k*\i-30/2^k) rather than (180*\i-30)/2^\k which causes an overflow.
    – grok
    Feb 24, 2018 at 16:47
  • Here is the "perfect" solution. Can you change it so I can select your answer? By the way, how to you display the result of TeX code in a post? Thanks again! \newcommand{\DrawArc}[3][]{ % from angle #2-#3 to #2+#3 \draw[#1] ({#2}:{sec(#3)}) circle ({tan(#3)}); } \begin{tikzpicture}[scale=4] \draw (0,0) circle (1cm); \clip (0,0) circle (1cm); \DrawArc[thick]{180}{60} \foreach\k in {0,...,6} { \pgfmathsetmacro{\j}{2*2^\k} \foreach\i in {1,...,\j} {\DrawArc{180/2^\k*\i}{30/2^\k}}; } \end{tikzpicture}
    – grok
    Feb 24, 2018 at 16:56
  • @grok I agree. You can even improve on that one.
    – user121799
    Feb 24, 2018 at 16:56
  • @grok What do you mean by "results". I usually just take a screenshot and put it into the answer using the insert picture icon on the top bar of the typing area.
    – user121799
    Feb 24, 2018 at 17:02
  • Ah, OK. I now see how to edit your post, I'll do it.
    – grok
    Feb 24, 2018 at 17:04
5

You can avoid drawing the arcs, if you just calculate the centers of the circles directly and then just clip them

enter image description here

And this is the code

\documentclass{article}

\usepackage{tikz}
\usepackage{expl3}

\ExplSyntaxOn
\cs_set_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff

\begin{document}

\begin{tikzpicture}[scale = 3]
  \draw (0,0) circle (1cm);
  \clip (0,0) circle (1cm);
  \foreach \i in {1,...,128} {
    \foreach \j in {1,...,32} {
      \pgfmathsetmacro{\theta}{\fpeval{(120 * \i + 60) / 2^\j}}
      \pgfmathsetmacro{\dtheta}{\fpeval{60 / 2^\j}}
      \pgfmathsetmacro{\r}{0.5 * sqrt(2 - 2 * cos(\dtheta))}
      \pgfmathsetmacro{\x}{cos(\theta)} 
      \pgfmathsetmacro{\y}{sin(\theta)}
      \draw[] (\x, \y) circle (\r);
    }
  }
\end{tikzpicture}
\end{document}

enter image description here

2
  • Thanks for the reply... but I do compute the centers of the arcs! The next answer does compute the centers more efficiently.
    – grok
    Feb 24, 2018 at 16:37
  • @grok Great! I'm glad you found the answer you're looking for
    – caverac
    Feb 24, 2018 at 17:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .