8
$\begingroup$

The MMA 10.2 help gives many examples of AnglePath lines of code but all of them are of the same color and I am quite at a loss to devise a method to add colors whenever I practise with this function. My difficulty is that you can't decide of the color in the parameters of Anglepath. If for example you want to draw a line with many segments, each segment having a different color, you have to repeat the sequence (color, graphic primitive, line) for each each segment and I'm not experienced enough to tackle this correctly.

Let's take this example:

g1 = Graphics@{NestList[
 Rotate[#, Pi/5, {0, 0}] &, {RandomColor[], 
 Line[AnglePath[{{-0.7, -0.7}, (-Pi/6)}, {{2, (7 \[Pi])/
     8}, {2, \[Pi]/8}, {2, (7 \[Pi])/8}, {2, \[Pi]/8}}]]}, 9]}

How can I get in the above graphics first a different random color for each leaf and then for each leaf a different color for each segment of line?

And in this example with a different graphic primitive Polygon how can I get for each leaf a different color ?

g2 = Graphics@{NestList[
Rotate[#, Pi/5, {0, 0}] &, {RandomColor[], 
Polygon[AnglePath[{{0, 0}, (-Pi/6)}, {{2, (7 \[Pi])/
     8}, {2, \[Pi]/8}, {2, (7 \[Pi])/8}, {2, \[Pi]/8}}]]}, 9]}

Is the answer specific to Polygon or independant of the graphic primitive? (I'm thinking of closed BSplineCurves graphics obtained with AnglePath)

Below I combined both graphics obtained in my question.

Image

$\endgroup$
4
$\begingroup$

In your examples you choose one color to begin with and then you pass that color onwards to each of the following leaves through recursion. Just like you have to apply Rotate in each step to change the angle, you also have to apply an operator that changes the color. For example:

next[{_, leaf_}] := {RandomColor[], Rotate[leaf, Pi/5, {0, 0}]}

g1 = Graphics@{
   NestList[
    next, {
     RandomColor[],
     Line[AnglePath[
       {{-0.7, -0.7}, (-Pi/6)},
       {{2, (7 π)/8}, {2, π/8}, {2, (7 π)/8}, {2, π/8}}
       ]]}, 9]
   }

Or you could skip the recursion altogether, it really just makes it more complicated since your problem doesn't really call for a recursive solution.

leaf[angle_, color_] := {color, Rotate[Line@AnglePath[
     {{-0.7, -0.7}, -Pi/6},
     {{2, (7 π)/8}, {2, π/8}, {2, (7 π)/8}, {2, π/8}}
     ], angle, {0, 0}]}

Graphics@Table[leaf[angle, RandomColor[]], {angle, 0, 2 Pi, Pi/5}]

Adapting these solutions to the second example is left as an exercise, as those solutions proceed in the same way.

To give different segments different colors you should create one line for each segment. Like this:

segments = Partition[AnglePath[{{1.2, 90. °}, {2.1, 130. °}, {0.7, -85. °}}], 2, 1];
colors = {RandomColor[], RandomColor[], RandomColor[]};

Graphics@MapThread[{#, Line[#2]} &, {colors, segments}]

This approach can be applied to your example like this:

leaf[angle_, colors_] := Thread[{
   colors,
   Rotate[Line[#], angle, {0, 0}] & /@ Partition[AnglePath[
      {{-0.7, -0.7}, -Pi/6},
      {{2, (7 π)/8}, {2, π/8}, {2, (7 π)/8}, {2, π/
         8}}
      ], 2, 1]}]

Graphics@Table[leaf[angle, Table[RandomColor[], {4}]], {angle, 0, 2 Pi, Pi/5}]

Mathematica graphics

$\endgroup$
7
$\begingroup$

Similar comments on recursion. The expression you create looks like this:

TreeForm[orig, VertexLabeling -> None]

enter image description here

Building it, e.g., like this gives you a flat structure:

new = Table[
  Rotate[
    Line[AnglePath[{{-0.7, -0.7}, (-Pi/6)}, {{2, (7 \[Pi])/8}, {2, \[Pi]/8}, {2, (7 \[Pi])/8}, {2, \[Pi]/8}}]],
    i*Pi/5, {0, 0}],
  {i, 10}];

enter image description here

Such structures are easy to operate on with replacement rules:

new /. l_Line :> ReplaceList[l, 
  Line[{___, pt1_, pt2_, ___}] :> {RandomColor[], Line[{pt1, pt2}]}] // Graphics

enter image description here

$\endgroup$
2
$\begingroup$

The answers received made me cogitate a lot these last two days and I came up with a solution based on Riffle:

All the graphics directives are stored in a table, each entry in that table corresponding to a path of the figure drawn . These paths are in a table supplied to the unique built-in drawing the figure (ie Line, Polygon....) here BezierCurve. The Riffle function is of the form Riffle[list,x,n] where n is here the number (+1) of graphics directives.

 Graphics@{NestList[Rotate[#, Pi/6, {1, -3}] &, 
  Riffle[Flatten[Table[{Hue[1/n], Thickness[0.01]}, {n, 1, 20, 2}]], 
  BezierCurve[#, SplineDegree -> 4, SplineClosed -> True] & /@ 
  Table[AnglePath[{Cos[0 + k], 
    Sin[0 + k]}, {{15, 2 Pi/3}, {20, 2 Pi/3}, {1, 2 Pi/3}}], {k, 
    0, 2, 0.2}], 3 ], 12]};

It's straightforward to change to another figure or surface. MMA has a lot of flexibility for a given task and no doubt many ways can be found to add colors to AnglePath based figures. Image

$\endgroup$

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.