TikZで3Dグラフを作図する例
座標軸の変更方法
相対変換
基底vectorを直接指定する
x=:value,y=:value,z=:value optionで決める
初期値はx={(1cm,0)},y={(0,1cm)},z={(-3.85mm,-3,85mm)}
code:xyz.tikz(tex)
\usepackage{amsmath}
\def\bm#1{\boldsymbol{#1}}
\begin{document}
\begin{tikzpicture}
\tikzset{every path/.style={->}}
\draw (0,0) - - (1,0,0) noderight {$x$}; \draw (0,0) - - (0,1,0) nodeabove {$y$}; \end{tikzpicture}
\end{document}
なんか傾いちゃった
https://scrapbox.io/files/65b5a0e8e9737f002651826b.svg
code:3d-without-tikz-3dplot.tikz(tex)
\usepackage{amsmath}
\usetikzlibrary{angles,quotes}
\def\bm#1{\boldsymbol{#1}}
\begin{document}
\begin{tikzpicture}
\tikzset{
vector/.style={-stealth,thick},
on circle/.style={-stealth,dashed}
}
\drawvector (O) -- (3,5,0) coordinate (B); \drawvector (O) -- ({3/2},5,{3*sqrt(3)/2}) coordinate (C); \end{tikzpicture}
\end{document}
zを上向きにしてやり直す
わかった。x={(220:1cm)},y={(-10:1cm)},z={(90:1cm)}は等測投象だ code:3d-without2.tikz(tex)
\usepackage{bm}
\usetikzlibrary{angles,quotes}
\begin{document}
\tikzset{
vector/.style={-stealth,thick},
on circle/.style={-stealth,dashed}
}
\drawvector (O) -- (0,3,5) coordinate (B); \drawvector (O) -- ({3*sqrt(3)/2},3/2,5) coordinate (C); \end{tikzpicture}
\end{document}
近づけてみた
code:3d-without3.tikz(tex)
\usepackage{bm}
\usetikzlibrary{angles,quotes}
\begin{document}
\tikzset{
vector/.style={-stealth,thick},
on circle/.style={-stealth,dashed}
}
\drawvector (O) -- (0,3,5) coordinate (B); \drawvector (O) -- ({3*sqrt(3)/2},3/2,5) coordinate (C); \end{tikzpicture}
\end{document}
マシになった
なるほどね。標準機能だけだとパースをいじるのが大変なわけだtakker.icon
作例
code:use-3d.tikz(tex)
\usetikzlibrary {3d}
\begin{document}
\def\wave{
(0,0) sin (1,1) cos (2,0) sin (3,-1) cos (4,0)
sin (5,1) cos (6,0) sin (7,-1) cos (8,0)
sin (9,1) cos (10,0)sin (11,-1)cos (12,0);
\foreach \shift in {0,4,8}
{
\draw (.5,0) -- (0.5,0 |- 45:1cm);
\draw (1,0) -- (1,1);
\draw (1.5,0) -- (1.5,0 |- 45:1cm);
\draw (2.5,0) -- (2.5,0 |- -45:1cm);
\draw (3,0) -- (3,-1);
\draw (3.5,0) -- (3.5,0 |- -45:1cm);
\end{scope}
}
}
\wave
\end{scope}
\wave
\end{scope}
\end{tikzpicture}
\end{document}
xy-平面,yz-平面,zx-平面に描画する
サンプルとしてはこっちのほうが何やっているかわかりやすい
改変:\defで繰り返しをまとめた
code:sphere.tikz(tex)
\usetikzlibrary {3d}
\begin{document}
\begin{tikzpicture}
\def\crosscircle{
\draw (0,0) circle (1cm);
\draw (-1,0) -- (1,0) (0,-1) -- (0,1);
}
\crosscircle
\end{scope}
\crosscircle
\end{scope}
\crosscircle
\end{scope}
\end{tikzpicture}
\end{document}
座標系の設定を回転角度などで簡単に指定できるようにするpackage
三次元描画機能自体はtikzの標準機能で賄っている。このpackageが提供するのはあくまで座標系の変換補助のみ
optionにtdplot_main_coordsを入れると計算結果が入力される
https://scrapbox.io/files/65b5c95527f7a10025dbba37.svg
code:3d.tikz(tex)
\usepackage{tikz-3dplot}
\tdplotsetmaincoords{70}{110}
\tikzset{my plot/.style={samples=61,smooth,gray}}
\begin{document}
\begin{tikzpicture}[
font=\sffamily,
tdplot_main_coords,
declare function={
f(\x,\y)=3+0.075*cos(\x*100)*cos(\y*100)-0.035*(\y-5)^2-0.01*(\x-5)^2;
}]
\coordinate (O) at (0,0,0);
% axes
\end{scope}
% grids
\foreach \a in {3,...,7} {
\draw plot ({\a},{\b},0);
\draw plot ({\b},{\a},0);
\draw plot ({\a},{\b},{f(\a,\b)});
\draw plot ({\b},{\a},{f(\b,\a)});
\end{scope}
}
% Circle on the surface
plot ({5+cos(\x)},{5+sin(\x)},{f(5+cos(\x),5+sin(\x)}) coordinate (x1);
\node at (x1) above=1cm {$f:(x,y)\mapsto 3+0.075\cos(100x)\cos(100y)-0.035(y-5)^2-0.01(x-5)^2$}; % Point and other circle
\coordinate (x0) at (5,5,0);
code:3d.tikz(tex)
\end{tikzpicture}
\end{document}
code:polygon.tikz(tex)
\usepackage{tikz-3dplot}
\usetikzlibrary{math}
\begin{document}
\tdplotsetmaincoords{70}{70}
\tikzmath{ \x=(1+sqrt(5))/2; }
\coordinate (A) at (1,0,0);
\coordinate (B) at ({cos(72)},{sin(72)},0);
\coordinate (C) at ({-cos(36)},{sin(36)},0);
\coordinate (D) at ({-cos(36)},{-sin(36)},0);
\coordinate (E) at ({cos(72)},{-sin(72)},0);
\coordinate (F) at (\x,0,1);
\coordinate (G) at ({\x*cos(36)},{\x*sin(36)},\x);
\coordinate (H) at ({\x*cos(72)},{\x*sin(72)},1);
\coordinate (I) at ({-\x*cos(72)},{\x*sin(72)},\x);
\coordinate (J) at ({-\x*cos(36)},{\x*sin(36)},1);
\coordinate (K) at (-\x,0,\x);
\coordinate (L) at ({-\x*cos(36)},{-\x*sin(36)},1);
\coordinate (M) at ({-\x*cos(72)},{-\x*sin(72)},\x);
\coordinate (N) at ({\x*cos(72)},{-\x*sin(72)},1);
\coordinate (O) at ({\x*cos(36)},{-\x*sin(36)},\x);
\coordinate (P) at (-1,0,\x+1);
\coordinate (Q) at ({-cos(72)},{-sin(72)},\x+1);
\coordinate (R) at ({cos(36)},{-sin(36)},\x+1);
\coordinate (S) at ({cos(36)},{sin(36)},\x+1);
\coordinate (T) at ({-cos(72)},{sin(72)},\x+1);
\drawthick (Q)--(M)--(N)--(E)--(A)--(B)--(H)--(I)--(T) (P)--(Q)--(R)--(S)--(T)--cycle
(N)--(O)--(R)
(O)--(F)--(G)--(H)
(S)--(G)
(F)--(A);
\drawdashed (B)--(C)--(D)--(E) (C)--(J)--(I)
(M)--(L)--(D)
(L)--(K)--(J)
(K)--(P);
\foreach \P in{A,...,T} \draw (\P) node{\P};
\end{tikzpicture}
\end{document}