我们可以画 Spikey 吗?

我们可以画 Spikey 吗?

“Spikey” 是 Wolfram Research 的标志,该公司是 Mathematica 和 Wolfram 语言的创造者。 我认为这将是一个有趣的实验,用 Tex 绘制各种版本的“Spikey”。在此处输入图片描述

答案1

这只是为了好玩。确实可以画出一些朝那个方向的东西。如果你想运行这个,请tikz-3dtools从下载库这里,并将其放在 TeX 可以找到的地方。

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3dtools}
\begin{document}
\tikzset{closed polygon/.style={insert path={foreach \Coord [count=\nCoord] in {#1}
 {\ifnum\nCoord=1
  \Coord
 \else
  -- \Coord
 \fi} -- cycle}},polygon/.style={insert path={foreach \Coord [count=\nCoord] in {#1}
 {\ifnum\nCoord=1
  \Coord
 \else
  -- \Coord
 \fi}}}}
\makeatletter


% main code
\foreach \X in {5,15,...,355}
{\tdplotsetmaincoords{90+30*sin(\X)}{\X}
\begin{tikzpicture}[tdplot_main_coords,font=\sffamily,fill opacity=1,line
join=round,scale=pi]
  \path[tdplot_screen_coords,use as bounding box] (-2,-2) rectangle (2,2);
  % define the vertices (there are certainly superior naming conventions)
  \path   (0., 0., 1.63925) coordinate (p1)  
 (0.262866, -0.809017, 1.11352) coordinate (p2)  
 (0.262866, 0.809017, 1.11352) coordinate (p3)  
 (0.850651, 0., 1.11352) coordinate (p4)  
 (0., 0., -1.63925) coordinate (p5)   
 (-0.262866, -0.809017, -1.11352) coordinate (p6)  
 (-0.262866, 0.809017, -1.11352) coordinate (p7) 
 (0.688191, -0.5, -1.11352) coordinate (p8)  
 (0.688191, 0.5, -1.11352) coordinate (p9)  
 (-0.850651, 0., -1.11352) coordinate (p10)  
 (-0.688191, -0.5, 1.11352) coordinate (p11)  
 (-0.688191, 0.5, 1.11352) coordinate (p12)  
 (-1.37638, 0., -0.262866) coordinate (p13)  
 (1.11352, -0.809017, -0.262866) coordinate (p14)  
 (1.11352, 0.809017, -0.262866) coordinate (p15) 
 (-0.425325, -1.30902, -0.262866) coordinate (p16)  
 (-0.425325, 1.30902, -0.262866) coordinate (p17) 
 (1.37638, 0., 0.262866) coordinate (p18)  
 (0.425325, -1.30902, 0.262866) coordinate (p19) 
 (0.425325, 1.30902, 0.262866) coordinate (p20)  
 (-1.11352, -0.809017, 0.262866) coordinate (p21)  
 (-1.11352, 0.809017, 0.262866) coordinate (p22) 
 (1.18617, -0.861803, 0.733094) coordinate (p23) 
 (1.18617, 0.861803, 0.733094) coordinate (p24)  
 (-1.46619, 0., 0.733094) coordinate (p25)  
 (-0.453077, -1.39443, 0.733094) coordinate (p26) 
 (-0.453077, 1.39443, 0.733094) coordinate (p27) 
 (1.46619, 0., -0.733094) coordinate (p28)  
 (-1.18617, -0.861803, -0.733094) coordinate (p29)  
 (-1.18617, 0.861803, -0.733094) coordinate (p30) 
 (0.453077, -1.39443, -0.733094) coordinate (p31) 
 (0.453077, 1.39443, -0.733094) coordinate (p32); 
  % define the plane data as a list of <drawing options>/<shape>/<vertices> 
  \def\PlaneData{%
  {draw,fill=randomcolor}/closed polygon/{(p1),(p26),(p2)},% 
 {draw,fill=randomcolor}/closed polygon/{(p5),(p31),(p6)},% 
 {draw,fill=randomcolor}/closed polygon/{(p3),(p27),(p1)},% 
 {draw,fill=randomcolor}/closed polygon/{(p7),(p32),(p5)},% 
 {draw,fill=randomcolor}/closed polygon/{(p19),(p26),(p31)},% 
 {draw,fill=randomcolor}/closed polygon/{(p32),(p27),(p20)},% 
 {draw,fill=randomcolor}/closed polygon/{(p26),(p23),(p2)},% 
 {draw,fill=randomcolor}/closed polygon/{(p3),(p24),(p27)},% 
 {draw,fill=randomcolor}/closed polygon/{(p30),(p29),(p13)},% 
 {draw,fill=randomcolor}/closed polygon/{(p29),(p26),(p21)},% 
 {draw,fill=randomcolor}/closed polygon/{(p22),(p27),(p30)},% 
 {draw,fill=randomcolor}/closed polygon/{(p16),(p26),(p29)},% 
 {draw,fill=randomcolor}/closed polygon/{(p30),(p27),(p17)},% 
 {draw,fill=randomcolor}/closed polygon/{(p31),(p29),(p6)},% 
 {draw,fill=randomcolor}/closed polygon/{(p7),(p30),(p32)},% 
 {draw,fill=randomcolor}/closed polygon/{(p31),(p23),(p19)},% 
 {draw,fill=randomcolor}/closed polygon/{(p20),(p24),(p32)},% 
 {draw,fill=randomcolor}/closed polygon/{(p18),(p24),(p23)},% 
 {draw,fill=randomcolor}/closed polygon/{(p8),(p28),(p31)},% 
 {draw,fill=randomcolor}/closed polygon/{(p9),(p32),(p28)},% 
 {draw,fill=randomcolor}/closed polygon/{(p14),(p23),(p31)},% 
 {draw,fill=randomcolor}/closed polygon/{(p32),(p24),(p15)},% 
 {draw,fill=randomcolor}/closed polygon/{(p23),(p24),(p4)},% 
 {draw,fill=randomcolor}/closed polygon/{(p31),(p28),(p14)},% 
 {draw,fill=randomcolor}/closed polygon/{(p28),(p32),(p15)},% 
 {draw,fill=randomcolor}/closed polygon/{(p21),(p25),(p29)},% 
 {draw,fill=randomcolor}/closed polygon/{(p30),(p25),(p22)},% 
 {draw,fill=randomcolor}/closed polygon/{(p28),(p23),(p14)},% 
 {draw,fill=randomcolor}/closed polygon/{(p15),(p24),(p28)},% 
 {draw,fill=randomcolor}/closed polygon/{(p18),(p23),(p28)},% 
 {draw,fill=randomcolor}/closed polygon/{(p28),(p24),(p18)},% 
 {draw,fill=randomcolor}/closed polygon/{(p29),(p25),(p13)},% 
 {draw,fill=randomcolor}/closed polygon/{(p13),(p25),(p30)},% 
 {draw,fill=randomcolor}/closed polygon/{(p2),(p23),(p1)},% 
 {draw,fill=randomcolor}/closed polygon/{(p8),(p31),(p5)},% 
 {draw,fill=randomcolor}/closed polygon/{(p1),(p24),(p3)},% 
 {draw,fill=randomcolor}/closed polygon/{(p5),(p32),(p9)},% 
 {draw,fill=randomcolor}/closed polygon/{(p19),(p23),(p26)},% 
 {draw,fill=randomcolor}/closed polygon/{(p27),(p24),(p20)},% 
 {draw,fill=randomcolor}/closed polygon/{(p11),(p26),(p1)},% 
 {draw,fill=randomcolor}/closed polygon/{(p6),(p29),(p5)},% 
 {draw,fill=randomcolor}/closed polygon/{(p1),(p27),(p12)},% 
 {draw,fill=randomcolor}/closed polygon/{(p5),(p30),(p7)},% 
 {draw,fill=randomcolor}/closed polygon/{(p1),(p25),(p11)},% 
 {draw,fill=randomcolor}/closed polygon/{(p5),(p29),(p10)},% 
 {draw,fill=randomcolor}/closed polygon/{(p12),(p25),(p1)},% 
 {draw,fill=randomcolor}/closed polygon/{(p10),(p30),(p5)},% 
 {draw,fill=randomcolor}/closed polygon/{(p1),(p23),(p4)},% 
 {draw,fill=randomcolor}/closed polygon/{(p5),(p28),(p8)},% 
 {draw,fill=randomcolor}/closed polygon/{(p4),(p24),(p1)},% 
 {draw,fill=randomcolor}/closed polygon/{(p9),(p28),(p5)},% 
 {draw,fill=randomcolor}/closed polygon/{(p10),(p29),(p30)},% 
 {draw,fill=randomcolor}/closed polygon/{(p21),(p26),(p25)},% 
 {draw,fill=randomcolor}/closed polygon/{(p22),(p25),(p27)},% 
 {draw,fill=randomcolor}/closed polygon/{(p16),(p29),(p31)},% 
 {draw,fill=randomcolor}/closed polygon/{(p32),(p30),(p17)},% 
 {draw,fill=randomcolor}/closed polygon/{(p25),(p26),(p11)},% 
 {draw,fill=randomcolor}/closed polygon/{(p27),(p25),(p12)},% 
 {draw,fill=randomcolor}/closed polygon/{(p31),(p26),(p16)},% 
 {draw,fill=randomcolor}/closed polygon/{(p17),(p27),(p32)}}
  % normal of screen (last row of the rotation matrix)
  \path[overlay] ({sin(\tdplotmaintheta)*sin(\tdplotmainphi)},
       {-1*sin(\tdplotmaintheta)*cos(\tdplotmainphi)},
       {cos(\tdplotmaintheta)}) coordinate (n); 
  % build up the list of projections  
  \foreach \Style/\Poly/\CoordLst [count=\nC] in \PlaneData
  {%
   \pgfmathsetmacro{\mybarycenter}{barycenter("\CoordLst")}
   \pgfmathsetmacro{\currproj}{TD("(n)o(\mybarycenter)")}
   \foreach \Coord [count=\nP] in \CoordLst
   {\expandafter\xdef\csname td@vertex@\romannumeral\nP\endcsname{\Coord}}
   \pgfmathsetmacro{\mynormal}{TD("\td@vertex@i-\td@vertex@ii x\td@vertex@iii-\td@vertex@ii")}
   \pgfmathtruncatemacro{\mysign}{ifthenelse(TD("(\mynormal)o(0,0,1)")<0,-1,1)}
   \pgfmathtruncatemacro{\mylightproj}{60+\mysign*40*TD("(\mynormal)o(0.3,0.1,1)")}
   %\pgfmathtruncatemacro{\mylightproj}{20+\nC}
   \expandafter\xdef\csname tikz@td@saturation\romannumeral\nC\endcsname{\mylightproj}
   \ifnum\nC=1
    \xdef\LstProj{\currproj}
  \else
    \xdef\LstProj{\LstProj,\currproj}
  \fi
  \expandafter\gdef\csname tikz@td@layer\romannumeral\nC\endcsname{}%
  \expandafter\xdef\csname tikz@td@poly\romannumeral\nC\endcsname{%
  \noexpand\path[\Style,\Poly={\CoordLst}];}
  \xdef\tikz@td@planes{\nC}%
  }
 \foreach \X [count=\nC] in \LstProj
 {\pgfmathtruncatemacro{\mypos}{pos(\X,"\LstProj")}
  \expandafter\edef\expandafter\tempa{\csname tikz@td@layer\romannumeral\mypos\endcsname}%
  \edef\tempb{}%
  \ifx\tempb\tempa\relax
    \expandafter\xdef\csname tikz@td@layer\romannumeral\mypos\endcsname{\nC}%
  \else 
    \expandafter\xdef\csname tikz@td@layer\romannumeral\mypos\endcsname{\tempa,\nC}%
  \fi
 }
 \foreach \X in {1,...,\tikz@td@planes}
 {\expandafter\edef\expandafter\tempa{\csname tikz@td@layer\romannumeral\X\endcsname}%
  \edef\tempb{}%
  \ifx\tempb\tempa\relax
  \else 
    \foreach \Y in \tempa
    {
      \edef\mysaturation{\csname tikz@td@saturation\romannumeral\Y\endcsname}
      \colorlet{randomcolor}{red!\mysaturation}
      \csname tikz@td@poly\romannumeral\Y\endcsname
    }
  \fi}
\end{tikzpicture}}
\makeatother  
\end{document}

在此处输入图片描述

它远不如 Mathematica 的版本那么花哨。绝对如此。这只是我的一个练习,看看一些 3d 排序例程的效果如何。到目前为止,我认为效果不错。

相关内容