2
$\begingroup$

I want to colorize lines as a function of coordinates in 3D. Here is my code. In this graphic I used one color. My aim is using a color function based on 3d coordinates.

points = RandomReal[{0, 20}, {100, 3}]
graph = Graphics3D[{Hue[0.67, 0.6, 0.6], Line[points]}, 
  PlotRange -> All, BoxRatios -> {1, 1, 1}, ImageSize -> 300]
$\endgroup$

1 Answer 1

1
$\begingroup$

You can use VertexColors to color a line segment by blending the two colors associated with the two vertices:

SeedRandom[1]
points = RandomReal[{0, 20}, {100, 3}];

Graphics3D[{Thick, Line[points, VertexColors -> (Hue @@@ Rescale[points])]}, 
 PlotRange -> All, BoxRatios -> {1, 1, 1}, ImageSize -> 300]

enter image description here

Alternatively, you can use BSplineFunction + ParametricPlot3D with a color function that depends on the first 3 arguments:

ParametricPlot3D[BSplineFunction[points, SplineDegree -> 1][t], {t, 0, 1}, 
 ColorFunction -> (Hue[#, #2, #3] &), BoxRatios -> {1, 1, 1}, 
 ImageSize -> 300]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you so much. $\endgroup$ Commented Sep 7, 2020 at 22:02
  • $\begingroup$ @Mustafa, you're welcome. And welcome to mma.se. Thank you for the accept. $\endgroup$
    – kglr
    Commented Sep 7, 2020 at 22:03

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.