我在纳米结构领域工作,必须绘制许多图形,例如:
答案1
这个怎么样:
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{3d}
\begin{document}
\pgfmathtruncatemacro{\tubelength}{5} % in "double hexagon lengths"
\pgfmathtruncatemacro{\tubecirumferenceatoms}{12}
\pgfmathsetmacro{\tuberadius}{3}
\newcommand{\carboncolor}{red}
\newcommand{\bondcolor}{black}
\tikzset{yzplane/.style={canvas is yz plane at x=#1,very thin}}
\begin{tikzpicture}[x={(-0.2cm,-0.5cm)}, y={(1cm,0cm)}, z={(0cm,1cm)}]
\foreach \x in {1,...,\tubelength}
{ \foreach \y in {1,...,\tubecirumferenceatoms}
{ \pgfmathsetmacro{\ycoord}{cos(\y/\tubecirumferenceatoms*360)*\tuberadius}
\pgfmathsetmacro{\zcoord}{sin(\y/\tubecirumferenceatoms*360)*\tuberadius}
\draw[thick,\bondcolor] (\x*3+0.5,\ycoord,\zcoord) -- (\x*3+1.5,\ycoord,\zcoord);
}
\begin{scope}[yzplane=\x*3+0.5]
\foreach \y in {1,...,\tubecirumferenceatoms}
{ \shade[ball color=\carboncolor] (\y/\tubecirumferenceatoms*360:\tuberadius) circle (0.2) ;
}
\end{scope}
\foreach \y in {1,...,\tubecirumferenceatoms}
{ \pgfmathsetmacro{\ycoord}{cos(\y/\tubecirumferenceatoms*360)*\tuberadius}
\pgfmathsetmacro{\zcoord}{sin(\y/\tubecirumferenceatoms*360)*\tuberadius}
\pgfmathsetmacro{\rotangle}{\y/\tubecirumferenceatoms*360+360/2/\tubecirumferenceatoms}
\pgfmathsetmacro{\ycoordtwo}{cos(\rotangle)*\tuberadius}
\pgfmathsetmacro{\zcoordtwo}{sin(\rotangle)*\tuberadius}
\pgfmathsetmacro{\rotanglethree}{\y/\tubecirumferenceatoms*360-360/2/\tubecirumferenceatoms}
\pgfmathsetmacro{\ycoordthree}{cos(\rotanglethree)*\tuberadius}
\pgfmathsetmacro{\zcoordthree}{sin(\rotanglethree)*\tuberadius}
\draw[thick,\bondcolor] (\x*3+1.5,\ycoord,\zcoord) -- (\x*3+2,\ycoordtwo,\zcoordtwo);
\draw[thick,\bondcolor] (\x*3+1.5,\ycoord,\zcoord) -- (\x*3+2,\ycoordthree,\zcoordthree);
}
\begin{scope}[yzplane=\x*3+1.5]
\foreach \y in {1,...,\tubecirumferenceatoms}
{ \shade[ball color=\carboncolor] (\y/\tubecirumferenceatoms*360:\tuberadius) circle (0.2);
}
\end{scope}
\foreach \y in {1,...,\tubecirumferenceatoms}
{ \pgfmathsetmacro{\rotangle}{\y/\tubecirumferenceatoms*360+360/2/\tubecirumferenceatoms}
\pgfmathsetmacro{\ycoord}{cos(\rotangle)*\tuberadius}
\pgfmathsetmacro{\zcoord}{sin(\rotangle)*\tuberadius}
\draw[thick,\bondcolor] (\x*3+2,\ycoord,\zcoord) -- (\x*3+3,\ycoord,\zcoord);
}
\begin{scope}[yzplane=\x*3+2]
\foreach \y in {1,...,\tubecirumferenceatoms}
{ \pgfmathsetmacro{\rotangle}{\y/\tubecirumferenceatoms*360+360/2/\tubecirumferenceatoms}
\shade[ball color=\carboncolor] (\rotangle:\tuberadius) circle (0.2);
}
\end{scope}
\foreach \y in {1,...,\tubecirumferenceatoms}
{ \pgfmathsetmacro{\ycoord}{cos(\y/\tubecirumferenceatoms*360)*\tuberadius}
\pgfmathsetmacro{\zcoord}{sin(\y/\tubecirumferenceatoms*360)*\tuberadius}
\pgfmathsetmacro{\rotangle}{\y/\tubecirumferenceatoms*360+360/2/\tubecirumferenceatoms}
\pgfmathsetmacro{\ycoordtwo}{cos(\rotangle)*\tuberadius}
\pgfmathsetmacro{\zcoordtwo}{sin(\rotangle)*\tuberadius}
\pgfmathsetmacro{\rotanglethree}{\y/\tubecirumferenceatoms*360-360/2/\tubecirumferenceatoms}
\pgfmathsetmacro{\ycoordthree}{cos(\rotanglethree)*\tuberadius}
\pgfmathsetmacro{\zcoordthree}{sin(\rotanglethree)*\tuberadius}
\draw[thick,\bondcolor] (\x*3+3.5,\ycoord,\zcoord) -- (\x*3+3,\ycoordtwo,\zcoordtwo);
\draw[thick,\bondcolor] (\x*3+3.5,\ycoord,\zcoord) -- (\x*3+3,\ycoordthree,\zcoordthree);
}
\begin{scope}[yzplane=\x*3+3]
\foreach \y in {1,...,\tubecirumferenceatoms}
{ \pgfmathsetmacro{\rotangle}{\y/\tubecirumferenceatoms*360+360/2/\tubecirumferenceatoms}
\shade[ball color=\carboncolor] (\rotangle:\tuberadius) circle (0.2);
}
\end{scope}
}
\end{tikzpicture}
\end{document}
您可以修改一些内容:
管的长度(4个原子的倍数)
每圆上的原子数
管半径
原子的颜色
债券的颜色
上述代码生成:
编辑1:如果你尝试改变设置,例如
\pgfmathtruncatemacro{\tubelength}{20} % in "double hexagon lengths"
\pgfmathtruncatemacro{\tubecirumferenceatoms}{20}
\pgfmathsetmacro{\tuberadius}{4}
\newcommand{\carboncolor}{red!80!black}
\newcommand{\bondcolor}{blue!20!black}
你得到了这个,它看起来很“纳米管“:
编辑2:
- “后面”的原子现在褪为白色,“后面”的键现在褪为灰色
- 因为我不知道哪些原子会在后面(取决于你选择的 xyz 轴),所以我添加了一个键
\initialrotationangle
,可以将管子旋转那个角度,你必须自己找到一个合适的值 - 之前,“键长”是一个静态值 1,这对于较小的半径来说看起来不太好,所以现在它是动态计算的(它不考虑圆柱体的曲率,因此对于非常小的值来说它可能看起来不太好
\tubecircumferenceatoms
) - 原子大小以前也是静态的,现在您可以将其指定为键长的分数。您可以将此分数设置为零,这样就只剩下“键网”,没有任何原子
- 由于我无论如何都要计算键的坐标,所以我现在也要计算原子的坐标,因此
3D
如果TikZ
- 您现在可以影响债券的外观,例如
thin
或dashed
根据自己的喜好
这是新代码。我知道我有几个循环在运行\y
,但我不能把所有东西都放在一个循环中,因为它们的顺序对于绘制前景和背景很重要。
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\begin{document}
\pgfmathtruncatemacro{\tubelength}{10} % in "double hexagon lengths"
\pgfmathtruncatemacro{\tubecirumferenceatoms}{12}
\pgfmathsetmacro{\tuberadius}{3}
\newcommand{\carboncolor}{red}
\newcommand{\bondcolor}{blue}
\pgfmathsetmacro{\initialrotationangle}{270}
\pgfmathsetmacro{\atombondlengthfraction}{0.2}
\newcommand{\bonddrawoptions}{thin}
\begin{tikzpicture}[x={(-0.2cm,-0.5cm)}, y={(1cm,0cm)}, z={(0cm,1cm)}, scale=0.5]
\foreach \x in {1,...,\tubelength}
{ \pgfmathsetmacro{\bondlength}{2*3.14159265*\tuberadius/sqrt(3)/\tubecirumferenceatoms}
\pgfmathsetmacro{\atomradius}{\bondlength*\atombondlengthfraction}
\foreach \y in {1,...,\tubecirumferenceatoms}
{ \pgfmathsetmacro{\rotangle}{\y/\tubecirumferenceatoms*360}
\pgfmathsetmacro{\ycoord}{cos(\rotangle)*\tuberadius}
\pgfmathsetmacro{\zcoord}{sin(\rotangle)*\tuberadius}
\pgfmathtruncatemacro{\shadingcolor}{50*cos(\rotangle+\initialrotationangle)+50}
\draw[\bonddrawoptions,\bondcolor!\shadingcolor!gray] (\x*3*\bondlength+0.5*\bondlength,\ycoord,\zcoord) -- (\x*3*\bondlength+1.5*\bondlength,\ycoord,\zcoord);
\shade[ball color=\carboncolor!\shadingcolor] (\x*3*\bondlength+0.5*\bondlength,\ycoord,\zcoord) circle (\atomradius*1cm) ;
}
\foreach \y in {1,...,\tubecirumferenceatoms}
{
\pgfmathsetmacro{\ycoord}{cos(\y/\tubecirumferenceatoms*360)*\tuberadius}
\pgfmathsetmacro{\zcoord}{sin(\y/\tubecirumferenceatoms*360)*\tuberadius}
\pgfmathsetmacro{\rotangle}{\y/\tubecirumferenceatoms*360+360/2/\tubecirumferenceatoms}
\pgfmathsetmacro{\ycoordtwo}{cos(\rotangle)*\tuberadius}
\pgfmathsetmacro{\zcoordtwo}{sin(\rotangle)*\tuberadius}
\pgfmathsetmacro{\rotanglethree}{\y/\tubecirumferenceatoms*360-360/2/\tubecirumferenceatoms}
\pgfmathsetmacro{\ycoordthree}{cos(\rotanglethree)*\tuberadius}
\pgfmathsetmacro{\zcoordthree}{sin(\rotanglethree)*\tuberadius}
\pgfmathtruncatemacro{\shadingcolor}{50*cos(\rotangle+\initialrotationangle)+50}
\draw[\bonddrawoptions,\bondcolor!\shadingcolor!gray] (\x*3*\bondlength+1.5*\bondlength,\ycoord,\zcoord) -- (\x*3*\bondlength+2*\bondlength,\ycoordtwo,\zcoordtwo);
\draw[\bonddrawoptions,\bondcolor!\shadingcolor!gray] (\x*3*\bondlength+1.5*\bondlength,\ycoord,\zcoord) -- (\x*3*\bondlength+2*\bondlength,\ycoordthree,\zcoordthree);
\shade[ball color=\carboncolor!\shadingcolor] (\x*3*\bondlength+1.5*\bondlength,\ycoord,\zcoord) circle (\atomradius*1cm);
}
\foreach \y in {1,...,\tubecirumferenceatoms}
{ \pgfmathsetmacro{\rotangle}{\y/\tubecirumferenceatoms*360+360/2/\tubecirumferenceatoms}
\pgfmathsetmacro{\ycoord}{cos(\rotangle)*\tuberadius}
\pgfmathsetmacro{\zcoord}{sin(\rotangle)*\tuberadius}
\pgfmathtruncatemacro{\shadingcolor}{50*cos(\rotangle+\initialrotationangle)+50}
\draw[\bonddrawoptions,\bondcolor!\shadingcolor!gray] (\x*3*\bondlength+2*\bondlength,\ycoord,\zcoord) -- (\x*3*\bondlength+3*\bondlength,\ycoord,\zcoord);
\shade[ball color=\carboncolor!\shadingcolor] (\x*3*\bondlength+2*\bondlength,\ycoord,\zcoord) circle (\atomradius*1cm);
}
\foreach \y in {1,...,\tubecirumferenceatoms}
{ \pgfmathsetmacro{\ycoord}{cos(\y/\tubecirumferenceatoms*360)*\tuberadius}
\pgfmathsetmacro{\zcoord}{sin(\y/\tubecirumferenceatoms*360)*\tuberadius}
\pgfmathsetmacro{\rotangle}{\y/\tubecirumferenceatoms*360+360/2/\tubecirumferenceatoms}
\pgfmathsetmacro{\ycoordtwo}{cos(\rotangle)*\tuberadius}
\pgfmathsetmacro{\zcoordtwo}{sin(\rotangle)*\tuberadius}
\pgfmathsetmacro{\rotanglethree}{\y/\tubecirumferenceatoms*360-360/2/\tubecirumferenceatoms}
\pgfmathsetmacro{\ycoordthree}{cos(\rotanglethree)*\tuberadius}
\pgfmathsetmacro{\zcoordthree}{sin(\rotanglethree)*\tuberadius}
\pgfmathtruncatemacro{\shadingcolor}{50*cos(\rotangle+\initialrotationangle)+50}
\draw[\bonddrawoptions,\bondcolor!\shadingcolor!gray] (\x*3*\bondlength+3.5*\bondlength,\ycoord,\zcoord) -- (\x*3*\bondlength+3*\bondlength,\ycoordtwo,\zcoordtwo);
\draw[\bonddrawoptions,\bondcolor!\shadingcolor!gray] (\x*3*\bondlength+3.5*\bondlength,\ycoord,\zcoord) -- (\x*3*\bondlength+3*\bondlength,\ycoordthree,\zcoordthree);
}
\foreach \y in {1,...,\tubecirumferenceatoms}
{ \pgfmathsetmacro{\rotangle}{\y/\tubecirumferenceatoms*360+360/2/\tubecirumferenceatoms}
\pgfmathsetmacro{\ycoordtwo}{cos(\rotangle)*\tuberadius}
\pgfmathsetmacro{\zcoordtwo}{sin(\rotangle)*\tuberadius}
\pgfmathtruncatemacro{\shadingcolor}{50*cos(\rotangle+\initialrotationangle)+50}
\shade[ball color=\carboncolor!\shadingcolor] (\x*3*\bondlength+3*\bondlength,\ycoordtwo,\zcoordtwo) circle (\atomradius*1cm);
}
}
\end{tikzpicture}
\end{document}
以下是一些示例:
还有债券: