使用 tikz 在 3D 立方体上绘图

使用 tikz 在 3D 立方体上绘图

我可以绘制一个立方体:

\documentclass[12pt]{standalone}
\usepackage{tikz}

    \begin{document}
    \begin{tikzpicture}
    \pgfmathsetmacro{\cubex}{2}
    \pgfmathsetmacro{\cubey}{1}
    \pgfmathsetmacro{\cubez}{3}
    \draw[draw = brown!30!black, fill = brown] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
    \draw[draw = brown!30!black, fill = brown] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
    \draw[draw = brown!30!black, fill = brown] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
    \end{tikzpicture}
    \end{document}

我现在的问题是,我该如何在其上绘图,即我想在此基础上绘图以扩展xyz比例。我想要做的一个例子是:

使用我已经定义的绘图,我将如何绘制图像中的水部分?我意识到这可能是使用其他软件完成的,但我想慢慢地使用它来构建它tikz,现在专注于水的部分。

答案1

需要做一些工作才能了解全貌,但我想这是一个开始:

\documentclass[border=5]{standalone}
\usepackage{tikz}
\tikzset{darken down/.style={top color=#1, bottom color=#1!75!black}}

\def\tikzcubecs#1,#2,#3\@{%
 \pgfpointxyz{(#1)*(1+(#3)/75}{(#2)*(1+(#1)/50)*(1+(#3)/50)}{(#3)*(1+(#1)/75)}%
}
\tikzdeclarecoordinatesystem{cube}{\tikzcubecs#1\@}
\colorlet{earth}{brown}
\colorlet{water}{blue!35!white}
\begin{document}

\begin{tikzpicture}[x=(340:1cm),y=(90:1cm), z=(200:1cm)]
\path 
  (cube cs:-10, 0,-10) coordinate (A1) (cube cs: 10, 0,-10) coordinate (B1)
  (cube cs: 10, 0, 10) coordinate (C1) (cube cs:-10, 0, 10) coordinate (D1);

\path 
  (cube cs:-10, -10,-10) coordinate (A2) (cube cs: 10, -10,-10) coordinate (B2)
  (cube cs: 10, -10, 10) coordinate (C2) (cube cs:-10, -10, 10) coordinate (D2);

\fill [earth] (A1) -- (B1) -- (B2) -- (C2) -- (D2) -- (D1) --  cycle;
\path  [darken down=earth!80!white] (B1) -- (B2) -- (C2) -- (C1) -- cycle;
\path [darken down=earth!80!black]  (D1) -- (D2) -- (C2) -- (C1) -- cycle;

\path [darken down=water!80!black] 
  (cube cs:-9,0,10) -- (cube cs:10,0,10) -- (cube cs:10,-9,10) 
  .. controls (cube cs:5,-9,10)  and  (cube cs:7.5,-5,10)  .. (cube cs:0,-5,10)
  .. controls (cube cs:-5,-5,10) and  (cube cs:-5,-1,10) .. (cube cs:-9,-1,10)
  -- cycle;

\path [darken down=water] 
  (cube cs:10,0,-9) -- (cube cs:10,0,10) -- (cube cs:10,-9,10) 
  .. controls (cube cs:10,-9,0)  and  (cube cs:10,-5,7.5)  .. (cube cs:10,-5,0)
  .. controls (cube cs:10,-5,-5) and  (cube cs:10,-1,-5) .. (cube cs:10,-1,-9)
  -- cycle;
\path [darken down=water!50!white] 
  (cube cs:10,0,-9) -- (cube cs:10,0,10) -- (cube cs:-9,0,10) 
  .. controls (cube cs:-9,0,5) and (cube cs:-5,0,5) ..
  (cube cs:-5,0,-5)
  .. controls (cube cs:-5,0,-9) and (cube cs:5,0,-9) ..
  cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

这里有类似的东西,但是没有花哨的坐标系。

\documentclass[border=20]{standalone}
\usepackage{tikz}
\colorlet{earth}{brown}
\colorlet{water}{blue!35!white}
\begin{document}

\begin{tikzpicture}
\fill [earth] 
  (-10,0) -- (0,4) -- (10,0) -- (10,-5) -- (0,-12)
  -- (-10,-5) -- cycle;
\fill [water]
  (0,-11) 
  .. controls ++(150:4) and ++(330:4) .. (-5, -6) 
  .. controls ++(150:4) and ++(330:4) .. (-9,-2) -- (-9,-1/2) 
  .. controls ++(30:2) and ++(210:4) ..
   (-2,1)
  .. controls ++(30:4) and ++(150:2) ..
  (9,-1/2)  -- (9,-3/2) 
  .. controls ++(210:4) and ++(30:4) .. (5,-6)
  .. controls ++(210:4) and ++(30:4) .. cycle;

\fill [black, opacity=1/3]
  (-10,0) -- (0,-5) -- (0,-12) -- (-10,-5) -- cycle;
\fill [black, opacity=1/5]
  (10,0) -- (0,-5) -- (0,-12) -- (10,-5) -- cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

首先,这是一个不同的投影,所以如果最终目标是这个图像,那么你要实现的不是一个立方体。而且在我看来,这并不是那么容易做到的。用任何基于鼠标的工具绘制它并将其转换为 TikZ 代码。然后就可以毫不费力地完成了。但如果你真的想这样做,那么你仍然可以使用 3d 坐标。我随意画了一些曲线。

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\def\cubex{4}\def\cubey{1}\def\cubez{6}
\coordinate (x) at (\cubex,0,0);
\coordinate (y) at (0,\cubey,0);
\coordinate (z) at (0,0,\cubez);
\draw[draw = brown!30!black, fill = brown] (x)|-(y) -- ($(z)+(0,\cubey,0)$)coordinate (zup) --(z)--++(\cubex,0,0) coordinate(zea)-- cycle;
\filldraw[fill=blue!30!white] (x|-y) to[in=20,out=-140] (zup) to[bend left] (zea) arc(-25:-10:6cm and 3cm) to[bend right,looseness=0.4] (x|-y);
\draw[brown!30!black] (zup) -| (zea) (zup-|zea) -- (x|-y);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容