TransWikia.com

Coloring lines as a function of coordinates

Mathematica Asked by Mustafa Kösem on December 15, 2020

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]

One Answer

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

Correct answer by kglr on December 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP