2
$\begingroup$

please help me. I want to paint every line different color, tried many ways, but they don't works. Thank you.

Animate[
 Graphics[{{Style [
     Line[{{{0, 0}, {3 Cos[x], 3 Sin[x]}}, {{0, 0}, {3 Cos[x - x/10], 
         3 Sin[x - x/10]}}}], {Red, Blue}] }}, Axes -> True, 
  PlotRange -> {{-5, 5}, {-5, 5}} ], {x, 0, 2 π}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Structure your Graphics command like Graphics[{color1, line1, color2, line2}] $\endgroup$
    – Jason B.
    Jan 2, 2017 at 21:11
  • $\begingroup$ Animate[ Graphics[ Thread[{{Red, Blue}, Line /@ {{{0, 0}, {3 Cos[x], 3 Sin[x]}}, {{0, 0}, {3 Cos[x - x/10], 3 Sin[x - x/10]}}}}], Axes -> True, PlotRange -> {{-5, 5}, {-5, 5}}], {x, 0, 2 \[Pi]}] $\endgroup$
    – Jason B.
    Jan 2, 2017 at 21:11

1 Answer 1

4
$\begingroup$

The simple solution as already given in the comments is to split the Line expression and interleave color directives:

Animate[Graphics[{{Red, Line[{{0, 0}, {3 Cos[x], 3 Sin[x]}}]},
   {Blue, Line[{{0, 0}, {3 Cos[x - x/10], 3 Sin[x - x/10]}}]}}, Axes -> True, 
  PlotRange -> {{-5, 5}, {-5, 5}}], {x, 0, 2 π}]

In the case of more line segments there can be an advantage to preserving a single Line expression as this is typically rendered faster. To make that work with individual segment colors you can use VertexColors and specify the color for each end of each line segment:

Animate[Graphics[
  Line[{{{0, 0}, {3 Cos[x], 3 Sin[x]}}, {{0, 0}, {3 Cos[x - x/10], 
      3 Sin[x - x/10]}}}, VertexColors -> {{Red, Red}, {Blue, Blue}}], Axes -> True,
   PlotRange -> {{-5, 5}, {-5, 5}}], {x, 0, 2 π}]

enter image description here

A Larger example:

colors = ({#, #} & /@ RandomColor[50]);

Animate[
  Graphics[
    Line[{#, {0, 0}} & /@ CirclePoints[{1, x}, 50], VertexColors -> colors]
  ],
  {x, 0, Pi/5}
]

enter image description here

$\endgroup$
2
  • $\begingroup$ Hi, I'm having a slightly different problem. I've look everywhere but until now I couldn't finish this task. I've tried colordata,colorlist,vertexcolors, and several other techniques but it is still not working with my data. I want to make that the color on my curve changes for every iteration. Let say it has 3 iterations that will produce 3 different line, I want it to produce different color for each line. Can anyone help me? $\endgroup$
    – BayWilson
    Jan 31, 2017 at 2:44
  • $\begingroup$ @NKamaru Please post a new question with as much information as possible. Describe exactly what you want, include pictures, and show the code you are starting with. $\endgroup$
    – Mr.Wizard
    Jan 31, 2017 at 7:50

Your Answer

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

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