Add text to faces of polyhedron

和自甴很熟 提交于 2019-12-22 03:20:47

问题


Is it possible to automate the addition of any text to the faces of a polyhedron, like this manually-drawn graphic shows (the example's odd numbering scheme isn't relevant):

It was easy enough to label the vertices:

c = 1;
Show[{Graphics3D[
   Text[c++, #] & /@ PolyhedronData["Dodecahedron", "VertexCoordinates"]], 
   PolyhedronData["Dodecahedron"]},
   Boxed -> False]

(even though some of the text is placed in front of the shape for vertices that are hidden. That's probably soluble.)

But when I tried to do the same thing for faces, nothing worked. PolyhedronData["Dodecahedron", "Faces"] returns a GraphicsComplex, rather than coordinates.

Am I overlooking an easy solution/option?

Edit: thanks for these answers, they're all brilliant. If I could combine the text placing of szabolcs' answer with the text quality of belisarius', the perfect solution is in sight!


回答1:


Here's a funky approach:

(* this function just transforms the polygon onto the [0,1] 2D square *)
vtc[face_, up_:{0,0,1}] := Module[{pts, pts2, centre, r, r2, topmost},
  pts = N@face;
  centre = Mean[pts];
  pts = (# - centre & /@ pts);
  r = SingularValueDecomposition[pts][[3]];

  (* these two lines ensure that the text on the outer face 
     of a convex polyhedron is not mirrored *)
  If[Det[r] < 0, r = -r];
  If[Last[centre.r] < 0, r = r.RotationMatrix[\[Pi], {1, 0, 0}]];

  pts2 = Most /@ (pts.r);
  topmost = Part[pts2, First@Ordering[up.# &  /@ pts, -1]];
  r2 = Transpose[{{#2, -#1} & @@ topmost, topmost}];
  r2 /= Norm[r2];
  Rescale[pts2.r2]
]

faces = First /@ First@Normal@PolyhedronData["Dodecahedron", "Faces"];

numbers = 
  Graphics[Text[
      Style[#, Underlined, FontFamily -> "Georgia", 
       FontSize -> Scaled[.3]]]] & /@ Range@Length[faces];

Graphics3D[
 MapThread[{Texture[#1], 
    Polygon[#2, VertexTextureCoordinates -> vtc[#2]]} &, {numbers, 
   faces}],
 Boxed -> False
 ]

Demoing a "SmallRhombicosidodecahedron":




回答2:


a = PolyhedronData["Dodecahedron", "Faces"] /.    GraphicsComplex -> List;
c = 1;
Show[{Graphics3D[
   Text[c++, #] & /@ (Mean /@ (a[[1, #]] & /@ a[[2, 1]]))], 
    PolyhedronData["Dodecahedron"]}, Boxed -> False]

Edit

Perhaps better:

Show[{Graphics3D[
         MapIndexed[Text[#2, #1] &, 
             Mean /@ (PolyhedronData["Dodecahedron", "VertexCoordinates"][[#]] & /@ 
                      PolyhedronData["Dodecahedron", "FaceIndices"])]], 
         PolyhedronData["Dodecahedron"]}, Boxed -> False]

Edit

Or

text = Style[#, 128] & /@ Range[12]
Graphics3D@
 Riffle[Texture /@ text, 
       (Append[#1, {VertexTextureCoordinates -> 
          With[{n = Length[First[#1]]}, Table[1/2 {Cos[2 Pi i/n], Sin[2 Pi i/n]}+ 
                                           {1/2, 1/2}, {i, 0, n - 1}]]}] &) /@ 
   Flatten[Normal[PolyhedronData["Dodecahedron", "Faces"]]]]



来源:https://stackoverflow.com/questions/8154079/add-text-to-faces-of-polyhedron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!