Drawing concentric circles with alternating colors by means of foreach in TikZ












6















I would like to draw concentric circles like these:



enter image description here



by means of TikZ and foreach command. The way I achieved it is very elementary (and burdensome):



   documentclass[standalone]
usepackage{tikz}
begin{document}
begin{tikzpicture}[scale=0.2]
coordinate (O) at (0,0);
fill[red!70] (O) circle (20);
fill[white] (O) circle (19);
fill[yellow!70] (O) circle (18);
fill[white] (O) circle (17);
fill[red!70] (O) circle (16);
fill[white] (O) circle (15);
fill[yellow!70] (O) circle (14);
fill[white] (O) circle (13);
fill[red!70] (O) circle (12);
fill[white] (O) circle (11);
fill[yellow!70] (O) circle (10);
fill[white] (O) circle (9);
fill[red!70] (O) circle (8);
fill[white] (O) circle (7);
fill[yellow!70] (O) circle (6);
fill[white] (O) circle (5);
fill[red!70] (O) circle (4);
fill[white] (O) circle (3);
fill[yellow!70] (O) circle (2);
fill[white] (O) circle (1);
end{tikzpicture}
end{document}


To no avail did I try applying foreach, but this is definitely due to my lack of fluency with TikZ (I always ended up with one color, or at most two nested circle with different color). Could you be so kind and help me with these? A suggestion how to do this would be even better than the full answer.










share|improve this question





























    6















    I would like to draw concentric circles like these:



    enter image description here



    by means of TikZ and foreach command. The way I achieved it is very elementary (and burdensome):



       documentclass[standalone]
    usepackage{tikz}
    begin{document}
    begin{tikzpicture}[scale=0.2]
    coordinate (O) at (0,0);
    fill[red!70] (O) circle (20);
    fill[white] (O) circle (19);
    fill[yellow!70] (O) circle (18);
    fill[white] (O) circle (17);
    fill[red!70] (O) circle (16);
    fill[white] (O) circle (15);
    fill[yellow!70] (O) circle (14);
    fill[white] (O) circle (13);
    fill[red!70] (O) circle (12);
    fill[white] (O) circle (11);
    fill[yellow!70] (O) circle (10);
    fill[white] (O) circle (9);
    fill[red!70] (O) circle (8);
    fill[white] (O) circle (7);
    fill[yellow!70] (O) circle (6);
    fill[white] (O) circle (5);
    fill[red!70] (O) circle (4);
    fill[white] (O) circle (3);
    fill[yellow!70] (O) circle (2);
    fill[white] (O) circle (1);
    end{tikzpicture}
    end{document}


    To no avail did I try applying foreach, but this is definitely due to my lack of fluency with TikZ (I always ended up with one color, or at most two nested circle with different color). Could you be so kind and help me with these? A suggestion how to do this would be even better than the full answer.










    share|improve this question



























      6












      6








      6


      1






      I would like to draw concentric circles like these:



      enter image description here



      by means of TikZ and foreach command. The way I achieved it is very elementary (and burdensome):



         documentclass[standalone]
      usepackage{tikz}
      begin{document}
      begin{tikzpicture}[scale=0.2]
      coordinate (O) at (0,0);
      fill[red!70] (O) circle (20);
      fill[white] (O) circle (19);
      fill[yellow!70] (O) circle (18);
      fill[white] (O) circle (17);
      fill[red!70] (O) circle (16);
      fill[white] (O) circle (15);
      fill[yellow!70] (O) circle (14);
      fill[white] (O) circle (13);
      fill[red!70] (O) circle (12);
      fill[white] (O) circle (11);
      fill[yellow!70] (O) circle (10);
      fill[white] (O) circle (9);
      fill[red!70] (O) circle (8);
      fill[white] (O) circle (7);
      fill[yellow!70] (O) circle (6);
      fill[white] (O) circle (5);
      fill[red!70] (O) circle (4);
      fill[white] (O) circle (3);
      fill[yellow!70] (O) circle (2);
      fill[white] (O) circle (1);
      end{tikzpicture}
      end{document}


      To no avail did I try applying foreach, but this is definitely due to my lack of fluency with TikZ (I always ended up with one color, or at most two nested circle with different color). Could you be so kind and help me with these? A suggestion how to do this would be even better than the full answer.










      share|improve this question
















      I would like to draw concentric circles like these:



      enter image description here



      by means of TikZ and foreach command. The way I achieved it is very elementary (and burdensome):



         documentclass[standalone]
      usepackage{tikz}
      begin{document}
      begin{tikzpicture}[scale=0.2]
      coordinate (O) at (0,0);
      fill[red!70] (O) circle (20);
      fill[white] (O) circle (19);
      fill[yellow!70] (O) circle (18);
      fill[white] (O) circle (17);
      fill[red!70] (O) circle (16);
      fill[white] (O) circle (15);
      fill[yellow!70] (O) circle (14);
      fill[white] (O) circle (13);
      fill[red!70] (O) circle (12);
      fill[white] (O) circle (11);
      fill[yellow!70] (O) circle (10);
      fill[white] (O) circle (9);
      fill[red!70] (O) circle (8);
      fill[white] (O) circle (7);
      fill[yellow!70] (O) circle (6);
      fill[white] (O) circle (5);
      fill[red!70] (O) circle (4);
      fill[white] (O) circle (3);
      fill[yellow!70] (O) circle (2);
      fill[white] (O) circle (1);
      end{tikzpicture}
      end{document}


      To no avail did I try applying foreach, but this is definitely due to my lack of fluency with TikZ (I always ended up with one color, or at most two nested circle with different color). Could you be so kind and help me with these? A suggestion how to do this would be even better than the full answer.







      tikz-pgf foreach loops






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 23 at 14:21







      Mad Hatter

















      asked Jan 23 at 12:02









      Mad HatterMad Hatter

      659520




      659520






















          7 Answers
          7






          active

          oldest

          votes


















          11














          You mean something like this:



          documentclass{standalone}
          usepackage{tikz}

          begin{document}
          begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
          foreach x in {1,3,5,7,9,11,13,15,17,19}
          node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) {};
          foreach x in {2,4,6,8,10,12,14,16,18, 20}
          node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) {};
          end{tikzpicture}
          end{document}


          which would give you:



          enter image description here



          I just made the yellow a bit lighter because, it was bleeding yellow too much :D






          share|improve this answer



















          • 1





            @MadHatter Also, this happens to be the first time I am using for loop in TeX. So, thanks to you ;)

            – Raaja
            Jan 23 at 12:36






          • 1





            +1, but iirc TikZ has an ifodd test which could make this a bit shorter…

            – TeXnician
            Jan 23 at 12:38











          • @TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?

            – Raaja
            Jan 23 at 12:39













          • @Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).

            – TeXnician
            Jan 23 at 12:44






          • 1





            @Raaja You're welcome :)

            – Mad Hatter
            Jan 23 at 14:22



















          9














          A slight generalization of Raaja's and TeXncian's answers in that I allow arbitrary color cycle lists, which is illustrated by an additional list of length 3.



          documentclass[tikz,border=3.14mm]{standalone}
          begin{document}
          begin{tikzpicture}
          deflstColors{{"red!70","yellow!50"}}
          foreach X [evaluate=X as Col using {lstColors[int(mod(X,2))]}] in {1,...,20}
          {draw[line width = 8pt,Col] (0,0) circle (X);}
          end{tikzpicture}
          begin{tikzpicture}
          deflstColors{{"red!70","yellow!50","blue!70"}}
          foreach X [evaluate=X as Col using {lstColors[int(mod(X,3))]}] in {1,...,20}
          {draw[line width = 8pt,Col] (0,0) circle (X);}
          end{tikzpicture}
          end{document}


          enter image description here






          share|improve this answer



















          • 1





            Neat. I'd wondered how to do that... I'm certainly missing something basic, but for X=1 (the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors in lstColors, but just wondered...

            – sgmoye
            Jan 23 at 15:10













          • @sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)

            – marmot
            Jan 23 at 22:04











          • I saw that, and to counteract it I changed {lstColors[int(mod(X,2))]} to {lstColors[int(mod(X-1,2))]} in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)

            – sgmoye
            Jan 23 at 22:58



















          7














          A PSTricks solution.



          documentclass[pstricks]{standalone}
          psset{runit=4pt,unit=psrunit}
          begin{document}
          pspicture[linewidth=.5](-11,-11)(11,11)
          foreach i in {1,2,...,10}{%
          ifoddidefc{yellow}elsedefc{red}fi
          pscircle[linecolor=c]{i}}
          endpspicture
          end{document}


          enter image description here



          Note: There is an unnecessary white spot after converting to PNG.



          Animated version



          documentclass[pstricks]{standalone}
          psset{runit=4pt,unit=psrunit}
          begin{document}
          foreach j in {1,...,10}{%
          pspicture[linewidth=.5](-11,-11)(11,11)
          foreach i in {1,...,j}{%
          ifoddidefc{yellow}elsedefc{red}fi
          pscircle[linecolor=c]{i}}%
          endpspicture}
          end{document}


          enter image description here






          share|improve this answer





















          • 1





            You did not animate this.

            – AlexG
            Jan 23 at 13:13



















          6














          This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width to make it thicker).



          concentric circles



          documentclass{standalone}
          usepackage{tikz}

          begin{document}
          begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
          foreach x in {1,...,20}{
          pgfmathparse{isodd(x)}ifnumpgfmathresult=1defcurrcol{red!70}elsedefcurrcol{yellow!50}fi
          draw[line width=8pt,currcol] (0,0) circle (x cm);}
          end{tikzpicture}
          end{document}





          share|improve this answer


























          • Next time, I will know how to apply this ifodd ;) [IMO, it is useful, if the loop counts grows].

            – Raaja
            Jan 23 at 12:46






          • 1





            @Raaja Yes, it is :)

            – TeXnician
            Jan 23 at 12:46











          • Thank you very much. P.S. I think you mean thinner not thicker lines :).

            – Mad Hatter
            Jan 23 at 14:18











          • @MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)

            – TeXnician
            Jan 23 at 14:31











          • @TeXnician Sorry, I misread your comment.

            – Mad Hatter
            Jan 23 at 15:01



















          5














          A SkiaSharp solution only for comparison purposes.



          using SkiaSharp; // needs skiasharp nuget
          using System.Diagnostics;


          class ConcentricCircle
          {
          static readonly SKPaint yellowStroke = new SKPaint
          {
          Style = SKPaintStyle.Stroke,
          Color = SKColors.Yellow,
          IsAntialias = true
          };


          static readonly SKPaint redStroke = new SKPaint
          {
          Style = SKPaintStyle.Stroke,
          Color = SKColors.Red,
          IsAntialias = true
          };

          static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
          static readonly float width = 6 * scale; // 6 cm
          static readonly float height = 6 * scale; // 6 cm

          static float PtToCm(float pt) => pt / scale;



          public static void Generate(string filename)
          {
          yellowStroke.StrokeWidth = PtToCm(4); // 4pt
          redStroke.StrokeWidth = PtToCm(4); // 4pt

          using (var stream = new SKFileWStream($"{filename}.pdf"))
          using (var document = SKDocument.CreatePdf(stream))
          using (var canvas = document.BeginPage(width, height))
          {

          // translate first and then scale, don't reverse!
          canvas.Translate(width / 2, height / 2);
          canvas.Scale(scale);

          // draw a red circle
          for (int i = 0; i < 5; i++)
          {
          canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
          canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
          }

          document.EndPage();
          }
          }

          private static void Main()
          {
          string filename = nameof(ConcentricCircle);
          Generate(filename);

          // convert to PNG with ImageMagick
          using (Process p = new Process())
          {
          p.StartInfo.FileName = "magick";
          p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove {filename}.pdf {filename}.png";
          p.Start();
          }
          }
          }


          enter image description here






          share|improve this answer





















          • 2





            Please don't radiate too much :) (+1)

            – Raaja
            Jan 23 at 12:46






          • 1





            I am a SkiaSharp evangelist.

            – The Inventor of God
            Jan 23 at 12:47






          • 2





            Nice alternative indeed.

            – Raaja
            Jan 23 at 12:49






          • 1





            I really don't know the answer for that :/

            – Raaja
            Jan 23 at 14:32



















          4














          Just for fun. Another solution which draws a couple of red and yellow circles in each iteration:



          documentclass[tikz,border=3mm]{standalone}

          usetikzlibrary{positioning}

          begin{document}
          begin{tikzpicture}[
          mycircle/.style={circle, draw, fill=none, line width=8pt},
          twocircle/.style={
          mycircle,
          minimum size=#1cm,
          color=yellow!70,
          append after command={%
          pgfextra
          node[mycircle, minimum size=thenumexpr#1-1relax cm, color=red!70] at (tikzlastnode.center) {};
          endpgfextra}},
          ]

          foreach i in {2,4,...,12}
          node[twocircle=i] {};
          end{tikzpicture}
          end{document}


          enter image description here






          share|improve this answer































            2














            A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):



            documentclass{standalone}
            usepackage{tikz}

            begin{document}
            begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
            foreach x in {1,2,...,19}{
            pgfmathsetmacromycolor{isodd x?"red!70":"yellow!50"}%
            node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) {};
            }%
            end{tikzpicture}
            end{document}





            share|improve this answer


























            • Why do you refer to the post about xifthen? Neither I do use it nor do you. And you do not even need the numexpr because x already is a number (the number you want to check).

              – TeXnician
              Jan 23 at 14:05











            • @TeXnician Had you checked, you would have seen that xifthen is in the title of the question from which I used some code, and which is substituted automatically for the URL. numexpr is superfluous. Will remove.

              – sgmoye
              Jan 23 at 14:39











            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "85"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f471465%2fdrawing-concentric-circles-with-alternating-colors-by-means-of-foreach-in-tikz%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            7 Answers
            7






            active

            oldest

            votes








            7 Answers
            7






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            11














            You mean something like this:



            documentclass{standalone}
            usepackage{tikz}

            begin{document}
            begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
            foreach x in {1,3,5,7,9,11,13,15,17,19}
            node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) {};
            foreach x in {2,4,6,8,10,12,14,16,18, 20}
            node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) {};
            end{tikzpicture}
            end{document}


            which would give you:



            enter image description here



            I just made the yellow a bit lighter because, it was bleeding yellow too much :D






            share|improve this answer



















            • 1





              @MadHatter Also, this happens to be the first time I am using for loop in TeX. So, thanks to you ;)

              – Raaja
              Jan 23 at 12:36






            • 1





              +1, but iirc TikZ has an ifodd test which could make this a bit shorter…

              – TeXnician
              Jan 23 at 12:38











            • @TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?

              – Raaja
              Jan 23 at 12:39













            • @Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).

              – TeXnician
              Jan 23 at 12:44






            • 1





              @Raaja You're welcome :)

              – Mad Hatter
              Jan 23 at 14:22
















            11














            You mean something like this:



            documentclass{standalone}
            usepackage{tikz}

            begin{document}
            begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
            foreach x in {1,3,5,7,9,11,13,15,17,19}
            node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) {};
            foreach x in {2,4,6,8,10,12,14,16,18, 20}
            node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) {};
            end{tikzpicture}
            end{document}


            which would give you:



            enter image description here



            I just made the yellow a bit lighter because, it was bleeding yellow too much :D






            share|improve this answer



















            • 1





              @MadHatter Also, this happens to be the first time I am using for loop in TeX. So, thanks to you ;)

              – Raaja
              Jan 23 at 12:36






            • 1





              +1, but iirc TikZ has an ifodd test which could make this a bit shorter…

              – TeXnician
              Jan 23 at 12:38











            • @TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?

              – Raaja
              Jan 23 at 12:39













            • @Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).

              – TeXnician
              Jan 23 at 12:44






            • 1





              @Raaja You're welcome :)

              – Mad Hatter
              Jan 23 at 14:22














            11












            11








            11







            You mean something like this:



            documentclass{standalone}
            usepackage{tikz}

            begin{document}
            begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
            foreach x in {1,3,5,7,9,11,13,15,17,19}
            node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) {};
            foreach x in {2,4,6,8,10,12,14,16,18, 20}
            node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) {};
            end{tikzpicture}
            end{document}


            which would give you:



            enter image description here



            I just made the yellow a bit lighter because, it was bleeding yellow too much :D






            share|improve this answer













            You mean something like this:



            documentclass{standalone}
            usepackage{tikz}

            begin{document}
            begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
            foreach x in {1,3,5,7,9,11,13,15,17,19}
            node [mystyle, minimum size = x cm, color =red!70] (2) at (0, 0) {};
            foreach x in {2,4,6,8,10,12,14,16,18, 20}
            node [mystyle, minimum size = x cm, color =yellow!50] (2) at (0, 0) {};
            end{tikzpicture}
            end{document}


            which would give you:



            enter image description here



            I just made the yellow a bit lighter because, it was bleeding yellow too much :D







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 23 at 12:14









            RaajaRaaja

            4,73921338




            4,73921338








            • 1





              @MadHatter Also, this happens to be the first time I am using for loop in TeX. So, thanks to you ;)

              – Raaja
              Jan 23 at 12:36






            • 1





              +1, but iirc TikZ has an ifodd test which could make this a bit shorter…

              – TeXnician
              Jan 23 at 12:38











            • @TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?

              – Raaja
              Jan 23 at 12:39













            • @Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).

              – TeXnician
              Jan 23 at 12:44






            • 1





              @Raaja You're welcome :)

              – Mad Hatter
              Jan 23 at 14:22














            • 1





              @MadHatter Also, this happens to be the first time I am using for loop in TeX. So, thanks to you ;)

              – Raaja
              Jan 23 at 12:36






            • 1





              +1, but iirc TikZ has an ifodd test which could make this a bit shorter…

              – TeXnician
              Jan 23 at 12:38











            • @TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?

              – Raaja
              Jan 23 at 12:39













            • @Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).

              – TeXnician
              Jan 23 at 12:44






            • 1





              @Raaja You're welcome :)

              – Mad Hatter
              Jan 23 at 14:22








            1




            1





            @MadHatter Also, this happens to be the first time I am using for loop in TeX. So, thanks to you ;)

            – Raaja
            Jan 23 at 12:36





            @MadHatter Also, this happens to be the first time I am using for loop in TeX. So, thanks to you ;)

            – Raaja
            Jan 23 at 12:36




            1




            1





            +1, but iirc TikZ has an ifodd test which could make this a bit shorter…

            – TeXnician
            Jan 23 at 12:38





            +1, but iirc TikZ has an ifodd test which could make this a bit shorter…

            – TeXnician
            Jan 23 at 12:38













            @TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?

            – Raaja
            Jan 23 at 12:39







            @TeXnician Could you enlighten me a bit in detail? I am not aware of those :D and what does iirc means?

            – Raaja
            Jan 23 at 12:39















            @Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).

            – TeXnician
            Jan 23 at 12:44





            @Raaja iirc is "if I recall correctly" and I have shown the test in my answer below (it's only to save the second loop, no real advantage).

            – TeXnician
            Jan 23 at 12:44




            1




            1





            @Raaja You're welcome :)

            – Mad Hatter
            Jan 23 at 14:22





            @Raaja You're welcome :)

            – Mad Hatter
            Jan 23 at 14:22











            9














            A slight generalization of Raaja's and TeXncian's answers in that I allow arbitrary color cycle lists, which is illustrated by an additional list of length 3.



            documentclass[tikz,border=3.14mm]{standalone}
            begin{document}
            begin{tikzpicture}
            deflstColors{{"red!70","yellow!50"}}
            foreach X [evaluate=X as Col using {lstColors[int(mod(X,2))]}] in {1,...,20}
            {draw[line width = 8pt,Col] (0,0) circle (X);}
            end{tikzpicture}
            begin{tikzpicture}
            deflstColors{{"red!70","yellow!50","blue!70"}}
            foreach X [evaluate=X as Col using {lstColors[int(mod(X,3))]}] in {1,...,20}
            {draw[line width = 8pt,Col] (0,0) circle (X);}
            end{tikzpicture}
            end{document}


            enter image description here






            share|improve this answer



















            • 1





              Neat. I'd wondered how to do that... I'm certainly missing something basic, but for X=1 (the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors in lstColors, but just wondered...

              – sgmoye
              Jan 23 at 15:10













            • @sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)

              – marmot
              Jan 23 at 22:04











            • I saw that, and to counteract it I changed {lstColors[int(mod(X,2))]} to {lstColors[int(mod(X-1,2))]} in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)

              – sgmoye
              Jan 23 at 22:58
















            9














            A slight generalization of Raaja's and TeXncian's answers in that I allow arbitrary color cycle lists, which is illustrated by an additional list of length 3.



            documentclass[tikz,border=3.14mm]{standalone}
            begin{document}
            begin{tikzpicture}
            deflstColors{{"red!70","yellow!50"}}
            foreach X [evaluate=X as Col using {lstColors[int(mod(X,2))]}] in {1,...,20}
            {draw[line width = 8pt,Col] (0,0) circle (X);}
            end{tikzpicture}
            begin{tikzpicture}
            deflstColors{{"red!70","yellow!50","blue!70"}}
            foreach X [evaluate=X as Col using {lstColors[int(mod(X,3))]}] in {1,...,20}
            {draw[line width = 8pt,Col] (0,0) circle (X);}
            end{tikzpicture}
            end{document}


            enter image description here






            share|improve this answer



















            • 1





              Neat. I'd wondered how to do that... I'm certainly missing something basic, but for X=1 (the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors in lstColors, but just wondered...

              – sgmoye
              Jan 23 at 15:10













            • @sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)

              – marmot
              Jan 23 at 22:04











            • I saw that, and to counteract it I changed {lstColors[int(mod(X,2))]} to {lstColors[int(mod(X-1,2))]} in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)

              – sgmoye
              Jan 23 at 22:58














            9












            9








            9







            A slight generalization of Raaja's and TeXncian's answers in that I allow arbitrary color cycle lists, which is illustrated by an additional list of length 3.



            documentclass[tikz,border=3.14mm]{standalone}
            begin{document}
            begin{tikzpicture}
            deflstColors{{"red!70","yellow!50"}}
            foreach X [evaluate=X as Col using {lstColors[int(mod(X,2))]}] in {1,...,20}
            {draw[line width = 8pt,Col] (0,0) circle (X);}
            end{tikzpicture}
            begin{tikzpicture}
            deflstColors{{"red!70","yellow!50","blue!70"}}
            foreach X [evaluate=X as Col using {lstColors[int(mod(X,3))]}] in {1,...,20}
            {draw[line width = 8pt,Col] (0,0) circle (X);}
            end{tikzpicture}
            end{document}


            enter image description here






            share|improve this answer













            A slight generalization of Raaja's and TeXncian's answers in that I allow arbitrary color cycle lists, which is illustrated by an additional list of length 3.



            documentclass[tikz,border=3.14mm]{standalone}
            begin{document}
            begin{tikzpicture}
            deflstColors{{"red!70","yellow!50"}}
            foreach X [evaluate=X as Col using {lstColors[int(mod(X,2))]}] in {1,...,20}
            {draw[line width = 8pt,Col] (0,0) circle (X);}
            end{tikzpicture}
            begin{tikzpicture}
            deflstColors{{"red!70","yellow!50","blue!70"}}
            foreach X [evaluate=X as Col using {lstColors[int(mod(X,3))]}] in {1,...,20}
            {draw[line width = 8pt,Col] (0,0) circle (X);}
            end{tikzpicture}
            end{document}


            enter image description here







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 23 at 14:00









            marmotmarmot

            108k5132248




            108k5132248








            • 1





              Neat. I'd wondered how to do that... I'm certainly missing something basic, but for X=1 (the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors in lstColors, but just wondered...

              – sgmoye
              Jan 23 at 15:10













            • @sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)

              – marmot
              Jan 23 at 22:04











            • I saw that, and to counteract it I changed {lstColors[int(mod(X,2))]} to {lstColors[int(mod(X-1,2))]} in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)

              – sgmoye
              Jan 23 at 22:58














            • 1





              Neat. I'd wondered how to do that... I'm certainly missing something basic, but for X=1 (the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors in lstColors, but just wondered...

              – sgmoye
              Jan 23 at 15:10













            • @sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)

              – marmot
              Jan 23 at 22:04











            • I saw that, and to counteract it I changed {lstColors[int(mod(X,2))]} to {lstColors[int(mod(X-1,2))]} in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)

              – sgmoye
              Jan 23 at 22:58








            1




            1





            Neat. I'd wondered how to do that... I'm certainly missing something basic, but for X=1 (the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors in lstColors, but just wondered...

            – sgmoye
            Jan 23 at 15:10







            Neat. I'd wondered how to do that... I'm certainly missing something basic, but for X=1 (the innermost circle) shouldn't that be red? The fix is simple, just reverse the order of the colors in lstColors, but just wondered...

            – sgmoye
            Jan 23 at 15:10















            @sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)

            – marmot
            Jan 23 at 22:04





            @sgmoye Good catch. The index of the first element of the list is 0, and the one of the second is 1. That's where the flip comes from. (I'll keep it to make the screenshots here more diverse. ;-)

            – marmot
            Jan 23 at 22:04













            I saw that, and to counteract it I changed {lstColors[int(mod(X,2))]} to {lstColors[int(mod(X-1,2))]} in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)

            – sgmoye
            Jan 23 at 22:58





            I saw that, and to counteract it I changed {lstColors[int(mod(X,2))]} to {lstColors[int(mod(X-1,2))]} in my copy -- that seemed to work for both 2 and 3 colors. Still, nice work! (Been a long day or I'd have posted earlier...)

            – sgmoye
            Jan 23 at 22:58











            7














            A PSTricks solution.



            documentclass[pstricks]{standalone}
            psset{runit=4pt,unit=psrunit}
            begin{document}
            pspicture[linewidth=.5](-11,-11)(11,11)
            foreach i in {1,2,...,10}{%
            ifoddidefc{yellow}elsedefc{red}fi
            pscircle[linecolor=c]{i}}
            endpspicture
            end{document}


            enter image description here



            Note: There is an unnecessary white spot after converting to PNG.



            Animated version



            documentclass[pstricks]{standalone}
            psset{runit=4pt,unit=psrunit}
            begin{document}
            foreach j in {1,...,10}{%
            pspicture[linewidth=.5](-11,-11)(11,11)
            foreach i in {1,...,j}{%
            ifoddidefc{yellow}elsedefc{red}fi
            pscircle[linecolor=c]{i}}%
            endpspicture}
            end{document}


            enter image description here






            share|improve this answer





















            • 1





              You did not animate this.

              – AlexG
              Jan 23 at 13:13
















            7














            A PSTricks solution.



            documentclass[pstricks]{standalone}
            psset{runit=4pt,unit=psrunit}
            begin{document}
            pspicture[linewidth=.5](-11,-11)(11,11)
            foreach i in {1,2,...,10}{%
            ifoddidefc{yellow}elsedefc{red}fi
            pscircle[linecolor=c]{i}}
            endpspicture
            end{document}


            enter image description here



            Note: There is an unnecessary white spot after converting to PNG.



            Animated version



            documentclass[pstricks]{standalone}
            psset{runit=4pt,unit=psrunit}
            begin{document}
            foreach j in {1,...,10}{%
            pspicture[linewidth=.5](-11,-11)(11,11)
            foreach i in {1,...,j}{%
            ifoddidefc{yellow}elsedefc{red}fi
            pscircle[linecolor=c]{i}}%
            endpspicture}
            end{document}


            enter image description here






            share|improve this answer





















            • 1





              You did not animate this.

              – AlexG
              Jan 23 at 13:13














            7












            7








            7







            A PSTricks solution.



            documentclass[pstricks]{standalone}
            psset{runit=4pt,unit=psrunit}
            begin{document}
            pspicture[linewidth=.5](-11,-11)(11,11)
            foreach i in {1,2,...,10}{%
            ifoddidefc{yellow}elsedefc{red}fi
            pscircle[linecolor=c]{i}}
            endpspicture
            end{document}


            enter image description here



            Note: There is an unnecessary white spot after converting to PNG.



            Animated version



            documentclass[pstricks]{standalone}
            psset{runit=4pt,unit=psrunit}
            begin{document}
            foreach j in {1,...,10}{%
            pspicture[linewidth=.5](-11,-11)(11,11)
            foreach i in {1,...,j}{%
            ifoddidefc{yellow}elsedefc{red}fi
            pscircle[linecolor=c]{i}}%
            endpspicture}
            end{document}


            enter image description here






            share|improve this answer















            A PSTricks solution.



            documentclass[pstricks]{standalone}
            psset{runit=4pt,unit=psrunit}
            begin{document}
            pspicture[linewidth=.5](-11,-11)(11,11)
            foreach i in {1,2,...,10}{%
            ifoddidefc{yellow}elsedefc{red}fi
            pscircle[linecolor=c]{i}}
            endpspicture
            end{document}


            enter image description here



            Note: There is an unnecessary white spot after converting to PNG.



            Animated version



            documentclass[pstricks]{standalone}
            psset{runit=4pt,unit=psrunit}
            begin{document}
            foreach j in {1,...,10}{%
            pspicture[linewidth=.5](-11,-11)(11,11)
            foreach i in {1,...,j}{%
            ifoddidefc{yellow}elsedefc{red}fi
            pscircle[linecolor=c]{i}}%
            endpspicture}
            end{document}


            enter image description here







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 23 at 13:19

























            answered Jan 23 at 13:07









            The Inventor of GodThe Inventor of God

            4,58611041




            4,58611041








            • 1





              You did not animate this.

              – AlexG
              Jan 23 at 13:13














            • 1





              You did not animate this.

              – AlexG
              Jan 23 at 13:13








            1




            1





            You did not animate this.

            – AlexG
            Jan 23 at 13:13





            You did not animate this.

            – AlexG
            Jan 23 at 13:13











            6














            This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width to make it thicker).



            concentric circles



            documentclass{standalone}
            usepackage{tikz}

            begin{document}
            begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
            foreach x in {1,...,20}{
            pgfmathparse{isodd(x)}ifnumpgfmathresult=1defcurrcol{red!70}elsedefcurrcol{yellow!50}fi
            draw[line width=8pt,currcol] (0,0) circle (x cm);}
            end{tikzpicture}
            end{document}





            share|improve this answer


























            • Next time, I will know how to apply this ifodd ;) [IMO, it is useful, if the loop counts grows].

              – Raaja
              Jan 23 at 12:46






            • 1





              @Raaja Yes, it is :)

              – TeXnician
              Jan 23 at 12:46











            • Thank you very much. P.S. I think you mean thinner not thicker lines :).

              – Mad Hatter
              Jan 23 at 14:18











            • @MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)

              – TeXnician
              Jan 23 at 14:31











            • @TeXnician Sorry, I misread your comment.

              – Mad Hatter
              Jan 23 at 15:01
















            6














            This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width to make it thicker).



            concentric circles



            documentclass{standalone}
            usepackage{tikz}

            begin{document}
            begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
            foreach x in {1,...,20}{
            pgfmathparse{isodd(x)}ifnumpgfmathresult=1defcurrcol{red!70}elsedefcurrcol{yellow!50}fi
            draw[line width=8pt,currcol] (0,0) circle (x cm);}
            end{tikzpicture}
            end{document}





            share|improve this answer


























            • Next time, I will know how to apply this ifodd ;) [IMO, it is useful, if the loop counts grows].

              – Raaja
              Jan 23 at 12:46






            • 1





              @Raaja Yes, it is :)

              – TeXnician
              Jan 23 at 12:46











            • Thank you very much. P.S. I think you mean thinner not thicker lines :).

              – Mad Hatter
              Jan 23 at 14:18











            • @MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)

              – TeXnician
              Jan 23 at 14:31











            • @TeXnician Sorry, I misread your comment.

              – Mad Hatter
              Jan 23 at 15:01














            6












            6








            6







            This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width to make it thicker).



            concentric circles



            documentclass{standalone}
            usepackage{tikz}

            begin{document}
            begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
            foreach x in {1,...,20}{
            pgfmathparse{isodd(x)}ifnumpgfmathresult=1defcurrcol{red!70}elsedefcurrcol{yellow!50}fi
            draw[line width=8pt,currcol] (0,0) circle (x cm);}
            end{tikzpicture}
            end{document}





            share|improve this answer















            This is Raaja's solution but without the second loop (test if the count variable is odd) and with a simple draw instead of nodes (change line width to make it thicker).



            concentric circles



            documentclass{standalone}
            usepackage{tikz}

            begin{document}
            begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
            foreach x in {1,...,20}{
            pgfmathparse{isodd(x)}ifnumpgfmathresult=1defcurrcol{red!70}elsedefcurrcol{yellow!50}fi
            draw[line width=8pt,currcol] (0,0) circle (x cm);}
            end{tikzpicture}
            end{document}






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 23 at 12:50

























            answered Jan 23 at 12:43









            TeXnicianTeXnician

            25.6k63390




            25.6k63390













            • Next time, I will know how to apply this ifodd ;) [IMO, it is useful, if the loop counts grows].

              – Raaja
              Jan 23 at 12:46






            • 1





              @Raaja Yes, it is :)

              – TeXnician
              Jan 23 at 12:46











            • Thank you very much. P.S. I think you mean thinner not thicker lines :).

              – Mad Hatter
              Jan 23 at 14:18











            • @MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)

              – TeXnician
              Jan 23 at 14:31











            • @TeXnician Sorry, I misread your comment.

              – Mad Hatter
              Jan 23 at 15:01



















            • Next time, I will know how to apply this ifodd ;) [IMO, it is useful, if the loop counts grows].

              – Raaja
              Jan 23 at 12:46






            • 1





              @Raaja Yes, it is :)

              – TeXnician
              Jan 23 at 12:46











            • Thank you very much. P.S. I think you mean thinner not thicker lines :).

              – Mad Hatter
              Jan 23 at 14:18











            • @MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)

              – TeXnician
              Jan 23 at 14:31











            • @TeXnician Sorry, I misread your comment.

              – Mad Hatter
              Jan 23 at 15:01

















            Next time, I will know how to apply this ifodd ;) [IMO, it is useful, if the loop counts grows].

            – Raaja
            Jan 23 at 12:46





            Next time, I will know how to apply this ifodd ;) [IMO, it is useful, if the loop counts grows].

            – Raaja
            Jan 23 at 12:46




            1




            1





            @Raaja Yes, it is :)

            – TeXnician
            Jan 23 at 12:46





            @Raaja Yes, it is :)

            – TeXnician
            Jan 23 at 12:46













            Thank you very much. P.S. I think you mean thinner not thicker lines :).

            – Mad Hatter
            Jan 23 at 14:18





            Thank you very much. P.S. I think you mean thinner not thicker lines :).

            – Mad Hatter
            Jan 23 at 14:18













            @MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)

            – TeXnician
            Jan 23 at 14:31





            @MadHatter You can make it thicker by choosing a larger dimension than 8pt and thinner by choosing something smaller. So it's up to you to decide which way ;)

            – TeXnician
            Jan 23 at 14:31













            @TeXnician Sorry, I misread your comment.

            – Mad Hatter
            Jan 23 at 15:01





            @TeXnician Sorry, I misread your comment.

            – Mad Hatter
            Jan 23 at 15:01











            5














            A SkiaSharp solution only for comparison purposes.



            using SkiaSharp; // needs skiasharp nuget
            using System.Diagnostics;


            class ConcentricCircle
            {
            static readonly SKPaint yellowStroke = new SKPaint
            {
            Style = SKPaintStyle.Stroke,
            Color = SKColors.Yellow,
            IsAntialias = true
            };


            static readonly SKPaint redStroke = new SKPaint
            {
            Style = SKPaintStyle.Stroke,
            Color = SKColors.Red,
            IsAntialias = true
            };

            static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
            static readonly float width = 6 * scale; // 6 cm
            static readonly float height = 6 * scale; // 6 cm

            static float PtToCm(float pt) => pt / scale;



            public static void Generate(string filename)
            {
            yellowStroke.StrokeWidth = PtToCm(4); // 4pt
            redStroke.StrokeWidth = PtToCm(4); // 4pt

            using (var stream = new SKFileWStream($"{filename}.pdf"))
            using (var document = SKDocument.CreatePdf(stream))
            using (var canvas = document.BeginPage(width, height))
            {

            // translate first and then scale, don't reverse!
            canvas.Translate(width / 2, height / 2);
            canvas.Scale(scale);

            // draw a red circle
            for (int i = 0; i < 5; i++)
            {
            canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
            canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
            }

            document.EndPage();
            }
            }

            private static void Main()
            {
            string filename = nameof(ConcentricCircle);
            Generate(filename);

            // convert to PNG with ImageMagick
            using (Process p = new Process())
            {
            p.StartInfo.FileName = "magick";
            p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove {filename}.pdf {filename}.png";
            p.Start();
            }
            }
            }


            enter image description here






            share|improve this answer





















            • 2





              Please don't radiate too much :) (+1)

              – Raaja
              Jan 23 at 12:46






            • 1





              I am a SkiaSharp evangelist.

              – The Inventor of God
              Jan 23 at 12:47






            • 2





              Nice alternative indeed.

              – Raaja
              Jan 23 at 12:49






            • 1





              I really don't know the answer for that :/

              – Raaja
              Jan 23 at 14:32
















            5














            A SkiaSharp solution only for comparison purposes.



            using SkiaSharp; // needs skiasharp nuget
            using System.Diagnostics;


            class ConcentricCircle
            {
            static readonly SKPaint yellowStroke = new SKPaint
            {
            Style = SKPaintStyle.Stroke,
            Color = SKColors.Yellow,
            IsAntialias = true
            };


            static readonly SKPaint redStroke = new SKPaint
            {
            Style = SKPaintStyle.Stroke,
            Color = SKColors.Red,
            IsAntialias = true
            };

            static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
            static readonly float width = 6 * scale; // 6 cm
            static readonly float height = 6 * scale; // 6 cm

            static float PtToCm(float pt) => pt / scale;



            public static void Generate(string filename)
            {
            yellowStroke.StrokeWidth = PtToCm(4); // 4pt
            redStroke.StrokeWidth = PtToCm(4); // 4pt

            using (var stream = new SKFileWStream($"{filename}.pdf"))
            using (var document = SKDocument.CreatePdf(stream))
            using (var canvas = document.BeginPage(width, height))
            {

            // translate first and then scale, don't reverse!
            canvas.Translate(width / 2, height / 2);
            canvas.Scale(scale);

            // draw a red circle
            for (int i = 0; i < 5; i++)
            {
            canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
            canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
            }

            document.EndPage();
            }
            }

            private static void Main()
            {
            string filename = nameof(ConcentricCircle);
            Generate(filename);

            // convert to PNG with ImageMagick
            using (Process p = new Process())
            {
            p.StartInfo.FileName = "magick";
            p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove {filename}.pdf {filename}.png";
            p.Start();
            }
            }
            }


            enter image description here






            share|improve this answer





















            • 2





              Please don't radiate too much :) (+1)

              – Raaja
              Jan 23 at 12:46






            • 1





              I am a SkiaSharp evangelist.

              – The Inventor of God
              Jan 23 at 12:47






            • 2





              Nice alternative indeed.

              – Raaja
              Jan 23 at 12:49






            • 1





              I really don't know the answer for that :/

              – Raaja
              Jan 23 at 14:32














            5












            5








            5







            A SkiaSharp solution only for comparison purposes.



            using SkiaSharp; // needs skiasharp nuget
            using System.Diagnostics;


            class ConcentricCircle
            {
            static readonly SKPaint yellowStroke = new SKPaint
            {
            Style = SKPaintStyle.Stroke,
            Color = SKColors.Yellow,
            IsAntialias = true
            };


            static readonly SKPaint redStroke = new SKPaint
            {
            Style = SKPaintStyle.Stroke,
            Color = SKColors.Red,
            IsAntialias = true
            };

            static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
            static readonly float width = 6 * scale; // 6 cm
            static readonly float height = 6 * scale; // 6 cm

            static float PtToCm(float pt) => pt / scale;



            public static void Generate(string filename)
            {
            yellowStroke.StrokeWidth = PtToCm(4); // 4pt
            redStroke.StrokeWidth = PtToCm(4); // 4pt

            using (var stream = new SKFileWStream($"{filename}.pdf"))
            using (var document = SKDocument.CreatePdf(stream))
            using (var canvas = document.BeginPage(width, height))
            {

            // translate first and then scale, don't reverse!
            canvas.Translate(width / 2, height / 2);
            canvas.Scale(scale);

            // draw a red circle
            for (int i = 0; i < 5; i++)
            {
            canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
            canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
            }

            document.EndPage();
            }
            }

            private static void Main()
            {
            string filename = nameof(ConcentricCircle);
            Generate(filename);

            // convert to PNG with ImageMagick
            using (Process p = new Process())
            {
            p.StartInfo.FileName = "magick";
            p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove {filename}.pdf {filename}.png";
            p.Start();
            }
            }
            }


            enter image description here






            share|improve this answer















            A SkiaSharp solution only for comparison purposes.



            using SkiaSharp; // needs skiasharp nuget
            using System.Diagnostics;


            class ConcentricCircle
            {
            static readonly SKPaint yellowStroke = new SKPaint
            {
            Style = SKPaintStyle.Stroke,
            Color = SKColors.Yellow,
            IsAntialias = true
            };


            static readonly SKPaint redStroke = new SKPaint
            {
            Style = SKPaintStyle.Stroke,
            Color = SKColors.Red,
            IsAntialias = true
            };

            static readonly float scale = SKDocument.DefaultRasterDpi / 2.54f; // dots per cm
            static readonly float width = 6 * scale; // 6 cm
            static readonly float height = 6 * scale; // 6 cm

            static float PtToCm(float pt) => pt / scale;



            public static void Generate(string filename)
            {
            yellowStroke.StrokeWidth = PtToCm(4); // 4pt
            redStroke.StrokeWidth = PtToCm(4); // 4pt

            using (var stream = new SKFileWStream($"{filename}.pdf"))
            using (var document = SKDocument.CreatePdf(stream))
            using (var canvas = document.BeginPage(width, height))
            {

            // translate first and then scale, don't reverse!
            canvas.Translate(width / 2, height / 2);
            canvas.Scale(scale);

            // draw a red circle
            for (int i = 0; i < 5; i++)
            {
            canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 1), yellowStroke);
            canvas.DrawCircle(0, 0, PtToCm(8) * (2 * i + 2), redStroke);
            }

            document.EndPage();
            }
            }

            private static void Main()
            {
            string filename = nameof(ConcentricCircle);
            Generate(filename);

            // convert to PNG with ImageMagick
            using (Process p = new Process())
            {
            p.StartInfo.FileName = "magick";
            p.StartInfo.Arguments = $"convert -compose copy -bordercolor red -border 2x2 -density 200 -alpha remove {filename}.pdf {filename}.png";
            p.Start();
            }
            }
            }


            enter image description here







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 23 at 14:39

























            answered Jan 23 at 12:43









            The Inventor of GodThe Inventor of God

            4,58611041




            4,58611041








            • 2





              Please don't radiate too much :) (+1)

              – Raaja
              Jan 23 at 12:46






            • 1





              I am a SkiaSharp evangelist.

              – The Inventor of God
              Jan 23 at 12:47






            • 2





              Nice alternative indeed.

              – Raaja
              Jan 23 at 12:49






            • 1





              I really don't know the answer for that :/

              – Raaja
              Jan 23 at 14:32














            • 2





              Please don't radiate too much :) (+1)

              – Raaja
              Jan 23 at 12:46






            • 1





              I am a SkiaSharp evangelist.

              – The Inventor of God
              Jan 23 at 12:47






            • 2





              Nice alternative indeed.

              – Raaja
              Jan 23 at 12:49






            • 1





              I really don't know the answer for that :/

              – Raaja
              Jan 23 at 14:32








            2




            2





            Please don't radiate too much :) (+1)

            – Raaja
            Jan 23 at 12:46





            Please don't radiate too much :) (+1)

            – Raaja
            Jan 23 at 12:46




            1




            1





            I am a SkiaSharp evangelist.

            – The Inventor of God
            Jan 23 at 12:47





            I am a SkiaSharp evangelist.

            – The Inventor of God
            Jan 23 at 12:47




            2




            2





            Nice alternative indeed.

            – Raaja
            Jan 23 at 12:49





            Nice alternative indeed.

            – Raaja
            Jan 23 at 12:49




            1




            1





            I really don't know the answer for that :/

            – Raaja
            Jan 23 at 14:32





            I really don't know the answer for that :/

            – Raaja
            Jan 23 at 14:32











            4














            Just for fun. Another solution which draws a couple of red and yellow circles in each iteration:



            documentclass[tikz,border=3mm]{standalone}

            usetikzlibrary{positioning}

            begin{document}
            begin{tikzpicture}[
            mycircle/.style={circle, draw, fill=none, line width=8pt},
            twocircle/.style={
            mycircle,
            minimum size=#1cm,
            color=yellow!70,
            append after command={%
            pgfextra
            node[mycircle, minimum size=thenumexpr#1-1relax cm, color=red!70] at (tikzlastnode.center) {};
            endpgfextra}},
            ]

            foreach i in {2,4,...,12}
            node[twocircle=i] {};
            end{tikzpicture}
            end{document}


            enter image description here






            share|improve this answer




























              4














              Just for fun. Another solution which draws a couple of red and yellow circles in each iteration:



              documentclass[tikz,border=3mm]{standalone}

              usetikzlibrary{positioning}

              begin{document}
              begin{tikzpicture}[
              mycircle/.style={circle, draw, fill=none, line width=8pt},
              twocircle/.style={
              mycircle,
              minimum size=#1cm,
              color=yellow!70,
              append after command={%
              pgfextra
              node[mycircle, minimum size=thenumexpr#1-1relax cm, color=red!70] at (tikzlastnode.center) {};
              endpgfextra}},
              ]

              foreach i in {2,4,...,12}
              node[twocircle=i] {};
              end{tikzpicture}
              end{document}


              enter image description here






              share|improve this answer


























                4












                4








                4







                Just for fun. Another solution which draws a couple of red and yellow circles in each iteration:



                documentclass[tikz,border=3mm]{standalone}

                usetikzlibrary{positioning}

                begin{document}
                begin{tikzpicture}[
                mycircle/.style={circle, draw, fill=none, line width=8pt},
                twocircle/.style={
                mycircle,
                minimum size=#1cm,
                color=yellow!70,
                append after command={%
                pgfextra
                node[mycircle, minimum size=thenumexpr#1-1relax cm, color=red!70] at (tikzlastnode.center) {};
                endpgfextra}},
                ]

                foreach i in {2,4,...,12}
                node[twocircle=i] {};
                end{tikzpicture}
                end{document}


                enter image description here






                share|improve this answer













                Just for fun. Another solution which draws a couple of red and yellow circles in each iteration:



                documentclass[tikz,border=3mm]{standalone}

                usetikzlibrary{positioning}

                begin{document}
                begin{tikzpicture}[
                mycircle/.style={circle, draw, fill=none, line width=8pt},
                twocircle/.style={
                mycircle,
                minimum size=#1cm,
                color=yellow!70,
                append after command={%
                pgfextra
                node[mycircle, minimum size=thenumexpr#1-1relax cm, color=red!70] at (tikzlastnode.center) {};
                endpgfextra}},
                ]

                foreach i in {2,4,...,12}
                node[twocircle=i] {};
                end{tikzpicture}
                end{document}


                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 24 at 10:25









                IgnasiIgnasi

                94.6k4175317




                94.6k4175317























                    2














                    A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):



                    documentclass{standalone}
                    usepackage{tikz}

                    begin{document}
                    begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
                    foreach x in {1,2,...,19}{
                    pgfmathsetmacromycolor{isodd x?"red!70":"yellow!50"}%
                    node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) {};
                    }%
                    end{tikzpicture}
                    end{document}





                    share|improve this answer


























                    • Why do you refer to the post about xifthen? Neither I do use it nor do you. And you do not even need the numexpr because x already is a number (the number you want to check).

                      – TeXnician
                      Jan 23 at 14:05











                    • @TeXnician Had you checked, you would have seen that xifthen is in the title of the question from which I used some code, and which is substituted automatically for the URL. numexpr is superfluous. Will remove.

                      – sgmoye
                      Jan 23 at 14:39
















                    2














                    A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):



                    documentclass{standalone}
                    usepackage{tikz}

                    begin{document}
                    begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
                    foreach x in {1,2,...,19}{
                    pgfmathsetmacromycolor{isodd x?"red!70":"yellow!50"}%
                    node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) {};
                    }%
                    end{tikzpicture}
                    end{document}





                    share|improve this answer


























                    • Why do you refer to the post about xifthen? Neither I do use it nor do you. And you do not even need the numexpr because x already is a number (the number you want to check).

                      – TeXnician
                      Jan 23 at 14:05











                    • @TeXnician Had you checked, you would have seen that xifthen is in the title of the question from which I used some code, and which is substituted automatically for the URL. numexpr is superfluous. Will remove.

                      – sgmoye
                      Jan 23 at 14:39














                    2












                    2








                    2







                    A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):



                    documentclass{standalone}
                    usepackage{tikz}

                    begin{document}
                    begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
                    foreach x in {1,2,...,19}{
                    pgfmathsetmacromycolor{isodd x?"red!70":"yellow!50"}%
                    node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) {};
                    }%
                    end{tikzpicture}
                    end{document}





                    share|improve this answer















                    A slightly different approach to the solution proposed @TeXnician (suggested by this: What is wrong with the use of `isodd` of `xifthen`?):



                    documentclass{standalone}
                    usepackage{tikz}

                    begin{document}
                    begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 8pt}]
                    foreach x in {1,2,...,19}{
                    pgfmathsetmacromycolor{isodd x?"red!70":"yellow!50"}%
                    node [mystyle, minimum size = x cm, mycolor] (2) at (0, 0) {};
                    }%
                    end{tikzpicture}
                    end{document}






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 23 at 14:42

























                    answered Jan 23 at 13:02









                    sgmoyesgmoye

                    3,89811327




                    3,89811327













                    • Why do you refer to the post about xifthen? Neither I do use it nor do you. And you do not even need the numexpr because x already is a number (the number you want to check).

                      – TeXnician
                      Jan 23 at 14:05











                    • @TeXnician Had you checked, you would have seen that xifthen is in the title of the question from which I used some code, and which is substituted automatically for the URL. numexpr is superfluous. Will remove.

                      – sgmoye
                      Jan 23 at 14:39



















                    • Why do you refer to the post about xifthen? Neither I do use it nor do you. And you do not even need the numexpr because x already is a number (the number you want to check).

                      – TeXnician
                      Jan 23 at 14:05











                    • @TeXnician Had you checked, you would have seen that xifthen is in the title of the question from which I used some code, and which is substituted automatically for the URL. numexpr is superfluous. Will remove.

                      – sgmoye
                      Jan 23 at 14:39

















                    Why do you refer to the post about xifthen? Neither I do use it nor do you. And you do not even need the numexpr because x already is a number (the number you want to check).

                    – TeXnician
                    Jan 23 at 14:05





                    Why do you refer to the post about xifthen? Neither I do use it nor do you. And you do not even need the numexpr because x already is a number (the number you want to check).

                    – TeXnician
                    Jan 23 at 14:05













                    @TeXnician Had you checked, you would have seen that xifthen is in the title of the question from which I used some code, and which is substituted automatically for the URL. numexpr is superfluous. Will remove.

                    – sgmoye
                    Jan 23 at 14:39





                    @TeXnician Had you checked, you would have seen that xifthen is in the title of the question from which I used some code, and which is substituted automatically for the URL. numexpr is superfluous. Will remove.

                    – sgmoye
                    Jan 23 at 14:39


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f471465%2fdrawing-concentric-circles-with-alternating-colors-by-means-of-foreach-in-tikz%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                    Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                    A Topological Invariant for $pi_3(U(n))$