5
$\begingroup$

This code gives me two sets of concentric circles with respective radii of 2n and 2n-1:

With[{n = 5}, Show[Graphics[
({{Circle[{0, 0}, 2*#1]}, {Circle[{0, 0}, 
     2*#1 - 1]}} & ) /@ Range[n]], Axes -> True]]

enter image description here

I would like alternating circles to be red and blue. I don't want to change anything else. But I can't figure out where to insert a PlotStyle instruction...

Any suggestions?

$\endgroup$

6 Answers 6

6
$\begingroup$
With[{n = 5}, Graphics[{{Red, Circle[{0, 0}, 2*#1]}, Blue, {Circle[{0, 0}, 2*#1 - 1]}} &/@
  Range[n], Axes -> True]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Appreciated. Feeling a bit of muppet. Can't accept for 8 mins, but will then do so :-) $\endgroup$ Oct 25, 2018 at 23:23
  • 1
    $\begingroup$ @RichardBurke-Ward, it happens to all of us:) $\endgroup$
    – kglr
    Oct 25, 2018 at 23:24
3
$\begingroup$

kglr's solution is what I would have done in old Mathematica. Nowadays, I would use Riffle[]:

With[{n = 5}, 
     Graphics[Riffle[Flatten[{Circle[{0, 0}, 2 #1 - 1], Circle[{0, 0}, 2 #1]} & /@ 
                             Range[n]], {Blue, Red}, {1, -2, 2}],
              Axes -> True]]

concentric circles with alternating colors

$\endgroup$
3
$\begingroup$
With[{n = 5},
 Graphics[
  Table[{If[OddQ[i], Blue, Red], Circle[{0, 0}, i]}, {i, n * 2}],
  Axes -> True]]

enter image description here

$\endgroup$
3
$\begingroup$

Using BlockMap:

With[{n = 5}, 
Graphics[
BlockMap[{If[OddQ[#[[1]]], Blue, Red], Circle[{0, 0}, #[[1]]]} &, 
Range[2 n], 1], Axes -> True]]

enter image description here

$\endgroup$
2
$\begingroup$

table driven

With[{n = 5},
    Graphics[Table[{<|True -> Blue, False -> Red|>[OddQ[i]], Circle[{0, 0}, i]}, {i, n * 2}], Axes -> True]
]
$\endgroup$
1
$\begingroup$

A few variations:

Clear["Global`*"];
n = 5;

c1 = {If[OddQ[#], Blue, Red], Circle[{0, 0}, #]} & /@ Range[2 n];

c2 = Array[{Red, Circle[{0, 0}, 2 #], Blue, 
    Circle[{0, 0}, 2 # - 1]} &, {n}];

c3 = MapIndexed[{If[OddQ[First@#2], Blue, Red], Circle[{0, 0}, #1]} &,
   Range[2 n]];

c4 = MapThread[{#1, Circle[{0, 0}, #2]} &
  , {Sequence @@@ ConstantArray[{Blue, Red}, n]
   , Range[2 n]
   }
  ];

c5 = FoldPairList[{{If[OddQ[#1], Blue, Red], 
     Circle[{0, 0}, #1]}, #2} &, Range[2 n]];

Graphics[#, Axes -> True] & /@ {c1, c2, c3, c4, c5}

(* desired output x5 *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.