4
$\begingroup$
ClearAll[prism];
prism[pts_List, h_] := 
Module[{bottoms, tops, surfacePoints, sidePoints, n}, 
surfacePoints = 
Table[Map[PadRight[#, 3, height] &, pts], {height, {0, h}}];
{bottoms, tops} = {Most[#], Rest[#]} &@surfacePoints;
sidePoints = 
Flatten[{bottoms, RotateLeft[bottoms, {0, 1}], 
RotateLeft[tops, {0, 1}], tops}, {{2, 3}, {1}}];
n = Length[sidePoints];
MapThread[
Polygon[#1, VertexNormals -> (#1 - #2), 
VertexTextureCoordinates -> #3] &, {Join[sidePoints, 
surfacePoints], 
Join[Map[{0, 0, 1} # &, sidePoints, {2}], 
 Map[({1, 1, 0} # + {0, 0, h/2}) &, surfacePoints, {2}]], 
Join[Table[{{i/n, 0}, {(i + 1)/n, 0}, {(i + 1)/n, 1}, {i/n, 
    1}}, {i, 0, n - 1}], Table[None, {Length[surfacePoints]}]]}]]
Clear[cyl];
cyl[{pt1_, pt2_}, r_: 1, n_: 90] := 
Module[{circle = 
r Table[{Cos[\[Phi]], Sin[\[Phi]]}, {\[Phi], Pi/n, 2 Pi, Pi/n}], 
h = EuclideanDistance[pt1, pt2]}, 
GeometricTransformation[prism[circle, h], 
Composition[TranslationTransform[pt1], 
Quiet[Check[RotationTransform[{{0, 0, 1.}, pt2 - pt1}], 
  Identity]]]]]
Graphics3D[{Texture[], EdgeForm[], cyl[{{0, 0, 0}, {0, 0, 2 Pi}}, 1]}, Boxed -> True, 
Axes -> True]

I want to inserted in the beginning and the end of cylinder texture but it didint work: enter image description here

$\endgroup$
  • 2
    $\begingroup$ It would be nice of you to explain better your questions, instead of dumping a page of code and a one-line request, as you are doing now AND you did in your previous question. $\endgroup$ – rhermans Oct 4 '14 at 14:03
  • $\begingroup$ You got this code from my answer here. It would have been better to include a link to that in your question, because nobody likes to see a bunch of code without explanation and context. So by beginning and end you mean the bottom and top faces of the cylinder? $\endgroup$ – Jens Oct 4 '14 at 16:46
  • $\begingroup$ Yes because I want to see like a wood beam, thanks and sorry... $\endgroup$ – Acatka Oct 4 '14 at 16:54
6
$\begingroup$

In principle, if you already have the cylinder and just want to add textured caps, you can add those with two separate Polygon commands. However, the code you are using can also be modified to include the texture in the output directly.

You copied the code from my answer to a question where only textured sides were desired. This was achieved by explicitly setting the VertexTextureCoordinates to None for the top and bottom polygons, contained in the variable surfacepoints. All that is needed is to replace None by the appropriate values, and any texture will also show up on those polygons.

Here I made that change. The function cyl hasn't been modified, I'm only including it for completeness. The only change is at the end of prism, the last Table containing None has been replaced by {#, #} &[Rescale[pts, {-h, h}/2]]. The resulting list is then fed into MapThread which assembles the oolygons from their vertices. The Rescale is added here to make sure that the image appears with a comparable magnification on all sides of the cylinder. This is determined from the relative size of the end caps versus the height h of the cylinder.

cyl[{pt1_, pt2_}, r_: 1, n_: 90] := Module[{
   circle = 
    r Table[{Cos[ϕ], Sin[ϕ]}, {ϕ, Pi/n, 2 Pi, Pi/n}], 
   h = EuclideanDistance[pt1, pt2]
   },
  GeometricTransformation[prism[circle, h], 
   Composition[TranslationTransform[pt1], 
    Quiet[Check[RotationTransform[{{0, 0, 1.}, pt2 - pt1}], Identity]]]
   ]
  ]

prism[pts_List, h_] := 
 Module[{bottoms, tops, surfacePoints, sidePoints, n}, 
  surfacePoints = Table[
    Map[PadRight[#, 3, height] &, pts], {height, {0, h}}
    ];
  {bottoms, tops} = {Most[#], Rest[#]} &@surfacePoints;
  sidePoints = 
   Flatten[{bottoms, RotateLeft[bottoms, {0, 1}], 
     RotateLeft[tops, {0, 1}], tops}, {{2, 3}, {1}}];
  n = Length[sidePoints];
  MapThread[
   Polygon[#1, VertexNormals -> (#1 - #2), 
     VertexTextureCoordinates -> #3] &,
   {
    Join[sidePoints, surfacePoints],
    Join[
     Map[{0, 0, 1} # &, sidePoints, {2}], 
     Map[({1, 1, 0} # - {0, 0, h/2}) &, surfacePoints, {2}]
     ],
    Join[
     Table[
      {{i/n, 0}, {(i + 1)/n, 0}, {(i + 1)/n, 1}, {i/n, 1}}, {i, 0, 
       n - 1}
      ],
     {#, #} &[Rescale[pts, {-h, h}/2]]
     ]
    }
   ]
  ]

img = ExampleData[{"TestImage", "Lena"}];

Graphics3D[{Texture[img], EdgeForm[], 
  cyl[{{0, 0, 0}, {0, 0, 2 Pi}}, 1]}, Boxed -> True, Axes -> True]

cyl

| improve this answer | |
$\endgroup$
  • $\begingroup$ +1 Weil ich Lena so mag: Versuchungen sollte man nachgeben. Wer weiß, ob sie wiederkommen? $\endgroup$ – eldo Oct 4 '14 at 18:41

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.