Generating stripes on a polygon

6

0

I want to create a Graphics3D polygon that has stripes on it. My code is

 Graphics3D[{
   LightBlue, 
   Polygon[{{0, 0, 0}, {0, 2, 0}, {1, 2, 0}, {1, 0, 0}}]}, 
   Boxed -> False,
   Lighting -> {{"Ambient", LightBlue}},
   ViewPoint -> {0.2, 0, 0.2}, 
   RotationAction -> "Clip"]

I want horizontal equidistant stripes on it. Can anyone help?

santhosh

Posted 2013-01-17T12:34:52.787

Reputation: 383

Also related: (6741)

– Mr.Wizard – 2019-01-12T07:35:05.273

6

Have a look at Texture. This might be what you need.

– cormullion – 2013-01-17T12:51:32.557

4

related: http://mathematica.stackexchange.com/q/3723/5

– rm -rf – 2013-01-17T13:54:40.887

Answers

9

I commented out some of the options, because they made it difficult for me to see what was exactly happening. [Edit: Change the texture pattern to a variable, to clarify how to modify it. A line pattern given at end.]

stripePattern = 
  Graphics[{Red, Rectangle[{0, 0}, {1, 1/2}], Blue, 
    Rectangle[{0, 1/2}, {1, 1}]}, PlotRangePadding -> 0];

Graphics3D[{Texture[stripePattern], 
  Polygon[{{0, 0, 0}, {0, 2, 0}, {1, 2, 0}, {1, 0, 0}}, 
   VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 4}, {0, 4}}]}, 
 Boxed -> False
 (*,Lighting->{{"Ambient",LightBlue}},ViewPoint->{0.2,0,0.2},RotationAction->"Clip"*)
]

Striped rectangle

If you have arbitrary polygons, then I don't know how to make the computation of VertexTextureCoordinates easily automatic. (One could compute a projection rather automatically I suppose.) @Mr.Wizard's answer might be easiest in that case.

Graphics3D[{Texture[stripePattern], 
  Polygon[{{0, 0, 0}, {0, 0, 2}, {0, 2, 2}, {0, 3, 1}, {0, 2, 0}}, 
   VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 4}, {1/2, 6}, {0, 4}}]}, 
 Boxed -> False
]

Striped pentagon

Addendum - Edited

Here's a projection function that projects the polygon onto the texture coordinate system. (Please excuse not putting in full error checking.)

I have edited the code to replace the pattern by lines instead of stripes, per the OP's query.

project[polygonVertices_, stripeDirection_, nStripeMultiplier_] := 
  Module[{normal, e1, e2},
    normal = 
     Normalize@
      Catch[(If[Chop[#] != {0, 0, 0}, Throw[#]] &@(Cross @@ Differences[#])) & /@ 
         Partition[polygonVertices, 3, 1, 1];];
    e2 = normal\[Cross]Normalize[stripeDirection];
    e1 = e2\[Cross]normal;
    polygonVertices.Transpose[{e1, nStripeMultiplier e2}]
    ] /; Length[polygonVertices] >= 3;

linePattern = 
  Graphics[{Thickness[0.15], Red, Line[{{0, 1/2}, {1, 1/2}}]}, 
   PlotRangePadding -> 0, PlotRange -> {{0, 1}, {0, 1}}];

With[{poly = 
   Table[RandomReal[{1, 2}] {Cos[t], Sin[t], 
      Cos[t] - 2 Sin[t]}, {t, π/6, 2 π, π/6}]}, 
 Graphics3D[{Texture[linePattern], 
   Polygon[poly, 
    VertexTextureCoordinates -> project[poly, {-2, -2, 2}, 4]]}, 
  Lighting -> {{"Ambient", White}}]
 ]

Striped polygon

Michael E2

Posted 2013-01-17T12:34:52.787

Reputation: 190 928

Thanks for the answer,Instead of Stripes ,how can i change it to lines on my polygon.? – santhosh – 2013-01-18T09:21:43.987

@santhosh.L See edits. The graphic in Texture[..] can be replaced by any Graphics or Image. – Michael E2 – 2013-01-18T12:40:06.117

5

Perhaps you can parametrize your polygon and use ParametricPlot3D which takes MeshFunctions / MeshShading options. Alternately you could try interpolating your data and using ListSurfacePlot3D which also takes these options.

An example from the MeshFunctions documentation (using Plot3D):

Plot3D[
 (x^2 - y^2)/(x^2 + y^2)^2, {x, -1.5, 1.5}, {y, -1.5, 1.5},
 BoxRatios -> Automatic, 
 PlotPoints -> 25,
 MeshFunctions -> {#3 &},
 MeshStyle -> Purple, 
 MeshShading -> {None, Green, None, Yellow}
]

Mathematica graphics

Another from the ParametricPlot3D documentation:

ParametricPlot3D[
  {(2 + Cos[v]) Cos[u], (2 + Cos[v]) Sin[u], Sin[v]},
  {u, 0, 2 Pi}, {v, 0, 2 Pi},
  Mesh -> 15,
  MeshShading -> {Red, Blue},
  MeshFunctions -> {#4 &}
]

Mathematica graphics

And finally one for ListSurfacePlot3D

ListSurfacePlot3D[data, MaxPlotPoints -> 50, Mesh -> 30, Axes -> None, 
 MeshFunctions -> (#1 - #2 + #3 &), MeshShading -> {None, Yellow}]

Mathematica graphics

Mr.Wizard

Posted 2013-01-17T12:34:52.787

Reputation: 259 163