3
$\begingroup$

I have a curve in 3D space which is green curve in the following picutre

enter image description here

The code for generating the curve is as follows:

g[x_, y_, z_] := x + y + z;
h[x_, y_, z_] := x^2 + y^2 + z^2;
s = -5;
ContourPlot3D[{g[x, y, z] == 0, h[x, y, z] == 24}, {x, -s, s}, {y, -s,
   s}, {z, -s, s}, PlotTheme -> "Scientific", Mesh -> None, 
 BoundaryStyle -> {{1, 2} -> {Thick, Green}}]

I was wondering, if it is possible to depict the value of the function $2 x + y z$ on this curve with colors? Like when the color is darker the the value is larger, when the color is lighter then the value is smaller.

$\endgroup$
4
$\begingroup$

You can get the boundary using g as the MeshFunctions option value:

boundary = ContourPlot3D[h[x, y, z] == 24, {x, -s, s}, {y, -s, s}, {z, -s, s}, 
   MeshFunctions -> (g[#, #2, #3] &),
   Mesh -> {{0}}, 
   MeshStyle -> Green,
   ContourStyle -> None];

You can post-process boundary to inject desired colors using VertexColors:

coloredboundary = Normal[boundary] /. 
     Line[x_] :> {Tube[x, .3, VertexColors -> Hue /@ Rescale[2 # + #2 #3 & @@@ x]]};   

Show[ContourPlot3D[{g[x, y, z] == 0, h[x, y, z] == 24}, {x, -s, s}, {y, -s, s}, {z, -s, s}, 
  Mesh -> None], 
 coloredboundary]

enter image description here

Replace Hue with your favorite color function.

Alternatively, you can process boundary to convert the Line object into a BSplineFunction and use it with ParametricPlot3D with the option ColorFunction:

bsF = Cases[Normal[boundary], Line[x_] :> BSplineFunction[x], All][[
   1]];

coloredboundary2 = ParametricPlot3D[bsF[t], {t, 0, 1}, 
    ColorFunction -> (Hue[2 # + #2 #3] &)] /. Line[a_, b_] :> Tube[a, .3, b];

Show[ContourPlot3D[{g[x, y, z] == 0, h[x, y, z] == 24}, {x, -s, s}, {y, -s, s}, {z, -s, s},
 Mesh -> None], 
 coloredboundary2, ViewPoint -> Front]

enter image description here

$\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.