2
$\begingroup$

I'm just starting out in Mathematica and still learning the ropes.

I'm making a "necklace" which is a circle with squares around it. The following works:

necklace[n_] :=
 Block[    {minCenter, maxCenter, circ, center, squares, out}, (
   circ := Circle[{0, 0}, 10];
   center :=
    Table[    {10*Cos[k*2*Pi/n] , 10*Sin[k*2*Pi/n]  }  , {k, 1, n} ];
   minCenter := center - 0.5;
   maxCenter := center + 0.5;
   squares :=
    Table  [     
     Rectangle[ minCenter[[i]], maxCenter[[i]]    ] , {i, 1, n}    ];
   out := Graphics[{circ, {Red, squares}}];
   print["hi"];
   Return[out]
   )]
necklace[10]

The problem is that I want each square to have a different color, right now they're all red. But when I edit the out:= Graphics at the bottom, it will change every square. For testing I was using Randomcolor[] but 1 random color is assigned to the whole necklace instead of for each one. Do I need a table of colors? Multiple outs? What's the best way?

$\endgroup$
  • 1
    $\begingroup$ Welcome! Take a look at Thread or Map or Riffle. $\endgroup$ – Yves Klett Jan 16 '15 at 10:50
5
$\begingroup$

Here's another way to make a random necklace that uses much less code:

necklace[n_] := 
 Graphics[{Circle[{0, 0}, 10], {RandomColor[], Rectangle[# - 1/2]}
 & /@ (10 {Cos@#, Sin@#} & /@ (2 π Range[n]/n))}]

necklace[40]

enter image description here

Here's a rainbow-necklace generator:

necklace2[n_] := 
 Graphics[{Circle[{0, 0}, 10], MapIndexed[{Hue[First@#2/n], 
 Rectangle[# - 1/2]} &, 10 {Cos@#, Sin@#} & /@ (2 π Range[n]/n)]}]

necklace2[40]

enter image description here

$\endgroup$
  • $\begingroup$ This is excellent thanks! Is there a cheatsheet on where I can find the meaning of those symbols? $\endgroup$ – dukevin Jan 16 '15 at 22:32
  • 2
    $\begingroup$ @KevinDuke: If you search for @ in the Documentation Center, you will get a link to Prefix, which explains that (for example) f@a is shorthand for f[a]. The #, #2 and & are pure function notation, the details of which can be learned at the Documentation Center page for Function. Likewise, if you search the documentation for /@ you will find that it is shorthand notation for Map, for example f/@a is Map[f,a]. All the other stuff is built-in, the details of which can be found in the docs. BTW, the documentation center is wonderful, it's how I learned the language. $\endgroup$ – DumpsterDoofus Jan 16 '15 at 22:47
3
$\begingroup$

You can do this:

necklace[n_, colors_: {Red}] := 
Block[{minCenter, maxCenter, circ, center, squares, out, 
  colorslist}, (circ := Circle[{0, 0}, 10];
  center := Table[{10*Cos[k*2*Pi/n], 10*Sin[k*2*Pi/n]}, {k, 1, n}];
  minCenter := center - 0.5;
  maxCenter := center + 0.5;
  colorslist = Flatten[Nest[Append[colors, #] &, colors, Floor[n/Length[colors]]]];
  squares := 
  Table[{colorslist[[i]], 
  Rectangle[minCenter[[i]], maxCenter[[i]]]}, {i, 1, n}];
  out := Graphics[{circ, squares}];
  Return[out])
]
{necklace[20, {Red, Green, Blue}], necklace[20]}

Which yields a necklace with the colors specified (repeated). Default value is red

enter image description here

What I basically do, is to take the list of colors provided at execution of the command (or its default value a list with only Red) and repeat it several times until it is longer than the number of circles.

Edit:

If you want a colorchange, you can use colorfunctions. Use Colordata[{schemename,{minValue,maxValue}}][value] to color your rectangles. Then you have another solution: (as default we will take rainbow colors)

 necklaceCF[n_, colorsfunctionname_: "Rainbow"] := Block[{minCenter, maxCenter, circ, center, squares, out, 
  colorslist}, (circ := Circle[{0, 0}, 10];
  center := Table[{10*Cos[k*2*Pi/n], 10*Sin[k*2*Pi/n]}, {k, 1, n}];
  minCenter := center - 0.5;
  maxCenter := center + 0.5;
  squares := 
  Table[{ColorData[{colorsfunctionname, {1, n}}][i], 
  Rectangle[minCenter[[i]], maxCenter[[i]]]}, {i, 1, n}];
  out := Graphics[{circ, squares}];
  Return[out])]
{necklaceCF[20], necklaceCF[20, "FruitPunchColors"]}

This yields:

enter image description here

$\endgroup$
  • $\begingroup$ Ahh that makes sense, thanks a lot! Sort of a separate question but let's say I wanted a gradient of colors where the first color is red, last purple, while compensating for the size of the necklace. What else would have to be done to do this? $\endgroup$ – dukevin Jan 16 '15 at 12:40
  • $\begingroup$ Since you seem new to SE, when an answer solved your problem, consider accepting the answer to credit it. $\endgroup$ – Philipp Jan 16 '15 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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