4
$\begingroup$

Update

I would like to control the direction of the color gradient to get similar to this plot but in 3D

enter image description here

Original

pol = {{1/2, 0, 0}, {1, 0, 0}, {5/4, Sqrt[3]/4, 0}, {1, Sqrt[3]/2, 
    0}, {1/2, Sqrt[3]/2, 0}, {1/4, Sqrt[3]/4, 0}, {1/2, 0, 0}};
Graphics3D[{Polygon[pol, 
   VertexColors -> {Red, Red, White, Black, Black, White}]}, 
 Boxed -> False];     

enter image description here

How can we make the gradient of color in a specific direction? for example along the green line in this fig or any arbitrary one such that it starts with red color from the bottom edge and white everywhere at the middle (white line) and smoothly goes to black at the top edge?

enter image description here

$\endgroup$

2 Answers 2

9
$\begingroup$

Updated

  • We use {x - x0, y - y0} . AngleVector[t] to calculate the sign distance from the line through the center {x0,y0} of the polygon.

  • Use Rescale to limit the range of the distance to the interval [0,1].

  • Set the Blend color from Red to White then to Blue.

pol = {{1/2, 0, 0}, {1, 0, 0}, {5/4, Sqrt[3]/4, 0}, {1, Sqrt[3]/2, 
    0}, {1/2, Sqrt[3]/2, 0}, {1/4, Sqrt[3]/4, 0}, {1/2, 0, 0}};
{x0, y0, z0} = RegionCentroid@Polygon@pol;
t = π/2;
RegionPlot3D[Polygon@pol, PlotPoints -> 100, MaxRecursion -> 6, 
 Mesh -> None, 
 ColorFunction -> 
  Function[{x, y, z}, 
   Blend[{{0, Red}, {.5, White}, {1, Blue}}, 
    Rescale[{x - x0, y - y0} . AngleVector[t], {-(Sqrt[3]/4), 
      Sqrt[3]/4}]]], ColorFunctionScaling -> False, 
 Lighting -> {{"Ambient", White}}, PerformanceGoal -> "Quality", 
 Boxed -> False, Axes -> False]

enter image description here

enter image description here

Original

Maybe similar with this result.

  • Along the y direction.
RegionPlot3D[Polygon@pol, PlotPoints -> 100, MaxRecursion -> 4, 
 ColorFunction -> 
  Function[{x, y, z}, Blend[{{0, Red}, {.5, White}, {1, Red}}, y]], 
 ColorFunctionScaling -> True, Boxed -> False]

enter image description here

  • Or set the color function be the distance from the line through the center of polygon.
{x0, y0, z0} = RegionCentroid@Polygon@pol;
ani = Manipulate[
  RegionPlot3D[Polygon@pol, PlotPoints -> 80, MaxRecursion -> 4, 
   Mesh -> None, 
   ColorFunction -> 
    Function[{x, y, z}, 
     Blend[{{1, Red}, {0, White}}, 
      2.5 Abs[{x - x0, y - y0} . AngleVector[t]]]], 
   ColorFunctionScaling -> False, Lighting -> "Neutral", 
   PerformanceGoal -> "Quality", Boxed -> False, Axes -> False], {t, 
   0, 2 Pi}]

enter image description here

$\endgroup$
3
  • $\begingroup$ That is AWESOME!, can we have different colors around the white like red and blue? $\endgroup$ Sep 12, 2022 at 5:14
  • $\begingroup$ @valarmorghulis pol = {{1/2, 0, 0}, {1, 0, 0}, {5/4, Sqrt[3]/4, 0}, {1, Sqrt[3]/2, 0}, {1/2, Sqrt[3]/2, 0}, {1/4, Sqrt[3]/4, 0}, {1/2, 0, 0}}; {x0, y0, z0} = RegionCentroid@Polygon@pol; t = \[Pi]/2; RegionPlot3D[Polygon@pol, PlotPoints -> 100, MaxRecursion -> 6, Mesh -> None, ColorFunction -> Function[{x, y, z}, Blend[{{0, White}, {.5, Blue}, {1, Red}}, 2.5 Abs[{x - x0, y - y0} . AngleVector[t]]]], ColorFunctionScaling -> False, Lighting -> {{"Ambient", White}}, PerformanceGoal -> "Quality", Boxed -> False, Axes -> False] ? $\endgroup$
    – cvgmt
    Sep 12, 2022 at 8:33
  • $\begingroup$ Not like this, kindly check my updates $\endgroup$ Sep 12, 2022 at 9:03
3
$\begingroup$

A rough approximation

$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global`*"]

pol = {{1/2, 0, 0}, {1, 0, 0}, {5/4, Sqrt[3]/4, 0}, {1, Sqrt[3]/2, 0}, {1/2, 
    Sqrt[3]/2, 0}, {1/4, Sqrt[3]/4, 0}, {1/2, 0, 0}};

Manipulate[
 pol2 = Insert[
   Insert[pol, p*pol[[4]] + (1 - p)*pol[[5]], 5],
   p*pol[[1]] + (1 - p)*pol[[2]], 2];
 Graphics3D[{
   Red,
   Polygon[pol2,
    VertexColors -> {
      Blend[{Red, White}, 1/2 - p], Red, Blend[{Red, White}, p - 1/2], White,
      Blend[{Black, White}, 1/2 - p], Black, Blend[{Black, White}, p - 1/2],
      White}],
   Thick,
   Green, Line[{pol2[[2]], pol2[[6]]}]},
  Boxed -> False],
 {{p, 0.5}, 0, 1, 0.05, Appearance -> "Labeled"}]

enter image description here

$\endgroup$
1
  • $\begingroup$ I need the color to be white a long the corners on the left and right of the green line which is not the case even in my orginal code. i.e. I need the color to change along x only while stays the same along y or vice versa. $\endgroup$ Sep 11, 2022 at 18:04

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.