3

I want to draw equal circles inside a circle using Tikz-pgf or other way as in the below image (please forget the numbers inside the small circles):

enter image description here

To draw a circle, we can use:

\begin{tikzpicture}
\draw[color=red] (0,0) circle [radius=1];
\end{tikzpicture}

enter image description here

But how to create an equal circle inside this unit circle, as in the first picture ?

One way is to find the center of each small circle and its radius. But that is very technical.

Is their any drawing tool that can easily do it? For example, there are drawing tools in Wolfram, but they need a subscription.

Remark: To give some idea about the first image above, it is a number system called the p-adic number system, and the image shows the distribution of 3-adic numbers. It is an interesting number system in mathematics.

learner
  • 663
  • 5
    R=r(1+2/sqrt(3)) should help you find centres and radius. – SebGlav Sep 04 '23 at 12:55
  • 2
    @SebGlav, yes that is from circle packing in a circle, but how would type squre inputs like (1+2/sqrt(3)) in Tikz ? – learner Sep 04 '23 at 13:00
  • 2
    Please give comments if you are downvoting. – learner Sep 04 '23 at 13:01
  • 2
    Admittedly it is more of a geometry question than a LaTeX queston, but at least it is interesting. – John Kormylo Sep 04 '23 at 13:20
  • 1
    @learner, I suggest spending some more time on the tikz manual, e.g. about \pic https://tikz.dev/tikz-pics and delegate less work here ... that's the wrong approach. – MS-SPO Sep 04 '23 at 14:10
  • @MS-SPO, thank you for suggestion. Just tell me if I want to place the number inside the small circles, do I have to use \node and coordiantes ? – learner Sep 04 '23 at 14:15
  • 2
    @learner, you'll need a node for displaying text. You can place it in absolute coordinates, with predefined \coordinate, or relative using positioning. But it's all written in the manuel, i.e. the ... tutorials there. // You may alos look there, how the syntax for polar coordinates look like. Will make your task easier. – MS-SPO Sep 04 '23 at 14:35
  • @MS-SPO, thanks, so I can use polar coordinates to display text in the above image. Since it is circular image, polar coordinates will be helpful. – learner Sep 04 '23 at 15:54

2 Answers2

11

Here you go!

three inner circles

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
    \newcommand{\RA}{2}
    \pgfmathsetmacro{\RB}{\RA*(1+2/sqrt(3)}
    \begin{tikzpicture}
        \draw (0,0) circle[radius=\RB cm];
        \foreach \ang in {90,210,330}
            \draw (\ang:\RB-\RA) circle[radius=\RA cm];
    \end{tikzpicture}   
\end{document}

Of course, if you want to draw the outer circle first and define the inner circles from it, you may change a bit your code:

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
    \newcommand{\RA}{2}
    \pgfmathsetmacro{\RB}{\RA/(1+2/sqrt(3)}
    \begin{tikzpicture}
        \draw (0,0) circle[radius=\RA cm];
        \foreach \ang in {90,210,330}
            \draw (\ang:\RA-\RB) circle[radius=\RB cm];
    \end{tikzpicture}   
\end{document}

BONUS: SECOND ITERATION

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
    \newcommand{\RA}{2}
    \pgfmathsetmacro{\RB}{\RA/(1+2/sqrt(3)}
    \begin{tikzpicture}
        \draw (0,0) circle[radius=\RA cm];
        \foreach \ang [count = \i from 1] in {90,210,330}
            \draw (\ang:\RA-\RB) coordinate (center-\i) circle[radius=\RB cm];
        \pgfmathsetmacro{\RC}{\RB/(1+2/sqrt(3)}
        \foreach \i in {1,2,3}
            \foreach \ang in {90,210,330}
                \draw ($(center-\i)+(\ang:\RB-\RC)$) circle[radius=\RC cm];
    \end{tikzpicture}   
\end{document}

two iterations

It was made very roughly if you just need two iterations. If you need more, it will be needed to make the code more legible and useful.

SebGlav
  • 19,186
  • 1
    Great! Now can we use the same code to draw 3 equal circles in each of the 3 small circes you have created ? That is I want divide each 3 small circles into same way – learner Sep 04 '23 at 13:10
  • 3
    Feel free to do it recursively. – SebGlav Sep 04 '23 at 13:12
  • 2
    \RA is the radius of the first circle, here 2 cm. Then \pgfmathsetmacro{\RB}{\RA/(1+2/sqrt(3)} computes the radius of the inner circles, which is named \RB. – SebGlav Sep 04 '23 at 13:25
  • I understand that 2 in the command \newcommand{\RA}{2} control the size of circles. But can you please add the 2nd iteration ? I can not do it – learner Sep 04 '23 at 13:27
  • Here it is. Beware that this code is not optimal if you want more iterations. – SebGlav Sep 04 '23 at 13:34
  • Unfortunately the 2nd iteration is showing errors. The error message says You need to say \usetikzlibrary{calc} for calculation..... – learner Sep 04 '23 at 13:39
  • Sorry, it worked nicely. I forgot to add \usetikzlibrary{calc} in the preamble – learner Sep 04 '23 at 13:44
  • My purpose is almost done. How to fill each circles of same size with same color ? For example, the 1st big circle with blue color, next 3 small circles with red colors, next 9 small circles with green colors etc – learner Sep 04 '23 at 13:46
  • 4
    Just replace \draw by \draw[fill=purple] or even \filldraw[purple]. And please avoid asking multiple questions in the comments in the future. – SebGlav Sep 04 '23 at 14:16
  • Thanks a lot. Where did you get manual so that I can learn more ? – learner Sep 04 '23 at 14:16
  • 3
    https://tikz.dev/ to begin with. You can find multiple sources out there on the Internet. – SebGlav Sep 04 '23 at 14:18
  • Thank you very much. – learner Sep 04 '23 at 14:20
  • Can you look at the answer by myself, below. I have tried to 3rd iteration using your code. It is almost coming as we need but still needs to improve. I believe the last governing equation needs to be improved appropriately – learner Sep 08 '23 at 02:15
11

The mathematical problem was solved by SebGlav already in the comments and they also helped you a big deal with their answer.

I want to provide a recursive solution using circular nodes and labels (i.e. more nodes).

It uses the ext.pgfkeys-plus library of my tikz-ext package for the /utils/TeX/ifnum={<test>}{<true>}{<false>} key to end the recursion when level zero has been reached. (And if you can't use that the definition of it is in the code below.)

The idea behind this is to anchor the labels (i.e. the circles inside another circle) at the angles 90, 210 and 330 and at their parent node. That way, we only need to evalute the radii and PGF/TikZ figures out the trigonometry of it.


The second picture moves the recursion check inside cic/label so we can use level 0 to do something totally different. Furthermore another argument is introduced (#3 in cic/@ which replaced cic which is now only a “starter”) which gets carried down and appears as #2 in cic/node'.

It just contains a list of directions in the form of u (90°), l (210°) and r (330°).

Since this is just a ternary number, we could also replace this with some calculations using the level #1 and the digits 0, 1 and 2 to evaluate the values on-the-fly but here I've opted to hardcode these.

Code

\documentclass[tikz]{standalone}
%\usepgfkeyslibrary{ext.pgfkeys-plus}
\makeatletter
\pgfkeys{/utils/TeX/ifnum/.code n args={3}{%
    \ifnum#1\relax\expandafter\pgfutil@firstoftwo\else\expandafter\pgfutil@secondoftwo\fi
    {\pgfkeysalso{#2}}{\pgfkeysalso{#3}}}}
\makeatother
\begin{document}
\tikz[
  cic/node/.style={
    shape=circle, inner sep=+0pt, outer sep=+0pt, minimum size={#1}},
  cic/.style n args={2}{
    cic/node={#2}, cic/level #1/.try,
    /utils/TeX/ifnum={#1=0}{}{
      /utils/exec=%
        \pgfmathsetlengthmacro\rad{#2/(1+2/sqrt(3))}%
        \pgfmathtruncatemacro\level{#1-1},
      cic/label/.expanded={\level}{\rad}{ 90},
      cic/label/.expanded={\level}{\rad}{210},
      cic/label/.expanded={\level}{\rad}{330}}},
  cic/label/.style n args={3}{
    label={[anchor=#3, label position=#3, cic={#1}{#2}]}},
  cic/level setter/.style args={#1=#2}{cic/level #1/.style={fill=#2}},
  cic/level setter/.list={3=purple, 2=red!50, 1=green!50!black, 0=blue!50},
] \node[cic={3}{2cm}]{};

\tikz[ cic/node/.style={ shape=circle, inner sep=+0pt, outer sep=+0pt, minimum size={#1}}, cic/node'/.style 2 args={% this node contents is just a debug fall back cic/node={#1}, path only, node contents={#2}, cic/node #2/.try}, cic/.style 2 args={cic/@={#1}{#2}{cic-}}, cic/@/.style n args={3}{ cic/node={#2}, cic/level #1/.try, /utils/exec=\pgfmathsetlengthmacro\rad{#2/(1+2/sqrt(3))}, cic/label/.expanded={\inteval{#1-1}}{\rad}{ 90}{#3u}, cic/label/.expanded={\inteval{#1-1}}{\rad}{210}{#3l}, cic/label/.expanded={\inteval{#1-1}}{\rad}{330}{#3r}}, cic/label/.style n args={4}{ /utils/TeX/ifnum={#1=0}{ label={[anchor=#3, label position=#3, cic/node'={#2}{#4}]}, }{ label={[anchor=#3, label position=#3, cic/@={#1}{#2}{#4}]}}}, cic/level setter/.style args={#1=#2}{cic/level #1/.style={fill=#2}}, cic/level setter/.list={2=purple, 1=red!50}, cic/node setter/.style args={#1=#2}{ cic/node cic-#1/.style={node contents={#2}}}, cic/node setter/.list={uu=0, ru=1, lu=2, ur=3, rr=4, lr=5, ul=6, rl=7, ll=8} ] \footnotesize\node[cic={2}{2cm}]{}; \end{document}

Output

enter image description here

enter image description here

Qrrbrbirlbel
  • 119,821
  • 2
    For a more general approach, see my answer on something that is related to fractals and Sierpinski carpets. (But you will need to do the setup which is a bit verbose.) – Qrrbrbirlbel Sep 04 '23 at 17:12
  • 1
    Well thanks, can you please suggest how to \node command for text display using polar coordinates (r, theta) ? Because I want to display the numbers in the 1st image of my question – learner Sep 04 '23 at 17:16
  • 1
    I mean what is the syntax of a node for polar coordinates. I didn't find exactly in the manual – learner Sep 04 '23 at 17:17
  • 1
    @learner I don't know what you mean. Adding the numbers is not so complicated (see my update). In this case, you just need to relate the values to the positions. Or figure out a formula depending on the position in diagram (see my remarks about ternary numbers). – Qrrbrbirlbel Sep 04 '23 at 17:30
  • 1
    @Qrrbrbirlbel Wonderful solution, as usual. You definitely master TikZ and your approach is way deeper than mine (I didn't even have the time to look deeper myself, but I wouldn't find anything like you did). Bravo! – SebGlav Sep 04 '23 at 20:49
  • 1
    @Qrrbrbirlbel, by the way, there is an error during compile undefined control sequence \usepgfkeyslibrary{ext.pgfkeys-plus}. I think my TexStudio editor is old. Needs to update – learner Sep 05 '23 at 02:30
  • 1
    @Qrrbrbirlbel, even I run on Overleaf cloud but still shwoing error undefined control sequence. I think \usepgfkeyslibrary{ext.pgfkeys-plus} is the problem creator – learner Sep 05 '23 at 09:25
  • 1
    @learner Has nothing to do with your editor. PGFKeys libraries are a pretty new thing in PGF/TikZ. You need to update your TeX distribution. Or try \usetikzlibrary{ext.misc}. – Qrrbrbirlbel Sep 05 '23 at 11:33
  • 1
    @Qrrbrbirlbel, I don't know but it is not running, sometimes it says undefined control sequence ] \footnotesize\node[cic={2}{2cm}]{}, Also the error says i don't know the key "/utils/TeX/ifnum", perhaps you mispelled it". Such errors are coming. I know your code is beautiful but my editor is very old one. The previus answer's code is ok but I am unable to add the numbers smoothly as you did in your code. Can you help me with the numbers. – learner Sep 05 '23 at 12:23
  • @learner I've added the definition of /utils/TeX/ifnum in the code so that it is usable without an uptodate TikZ/tikz-ext. – Qrrbrbirlbel Sep 06 '23 at 09:09
  • @learner, sigh, look for "The implicit form for canvas polar coordinates" in https://tikz.dev/tikz-coordinates#sec-13.2.1, right below said text ... – MS-SPO Sep 06 '23 at 09:13
  • 1
    @MS-SPO, thanks. I have managed to plot the displayed numbers in the 1st image. I want to animate the images, I will be back with a separate question. – learner Sep 06 '23 at 11:23