在 3D 中旋转半圈,tikzpicture 中的线条略有偏离

在 3D 中旋转半圈,tikzpicture 中的线条略有偏离

在下图中,我想展示一些 3D 形状,例如圆柱体和球体是通过围绕轴旋转平面形状而构造的。

在此处输入图片描述

圆柱体看起来还好,但是旋转的半圆的轮廓看起来有点偏离。

我觉得只需很少的努力就能纠正这个错误,但我似乎找不到什么。

生成图片的代码片段如下。这是我学习时编写的旧代码TikZ 3D,可能不是实现结果的最佳方式。它包括更改基础向量以更改默认视角,但仍然能够使用“简单”坐标(如 0、1 等)绘制形状。

是否可以通过尽可能少的更改来修复错误?

\documentclass{article}
\usepackage{tikz}
\definecolor{ocre}{RGB}{243,102,25}
\colorlet{mild}{ocre!50}
\begin{document}
\begin{tikzpicture}[y={(0,-0.385cm)},z={(0,1cm)},scale=1.25]
\fill[rotate around z=40, mild] (0,0) -- (0,0,2) -- (1,0,2) -- (1,0,0) -- cycle;
\draw (1,0) arc (0:180:1);
\draw[dashed] (-1,0) arc (180:360:1);
\draw (0,0,2) circle (1);
\draw[rotate around z=40, dashed] (1,0,0) -- (0,0) -- (0,0,2);
\draw[rotate around z=40] (0,0,2) -- (1,0,2) -- (1,0,0);
\draw (-1,0,0) -- (-1,0,2);
\draw (1,0,0) -- (1,0,2);
\draw[rotate around z=40, scale=0.8] (0,0,0.3) -- (0.3,0,0.3) -- (0.3,0,0);
\begin{scope}[xshift=3cm]
\fill[rotate around z=40, mild,y={(0,0,1)},z={(0,1,0)}] (0,0,1) -- (0,0,2) arc (90:-90:1) -- cycle;
\draw (1,0,1) arc (0:180:1);
\draw[dashed] (-1,0,1) arc (180:360:1);
\draw (0,0,1) circle[y={(0,0,1)},z={(0,1,0)}, radius=1];
\draw[dashed] (0,0) -- (0,0,2);
\end{scope}
\end{tikzpicture}
\end{document}

答案1

我自己找到了几个解决这个问题的方法。

在此处输入图片描述

解决方案 1 - 快速而粗略

通过反复试验,我发现可以通过在轴y中混合一定量的轴来修复图片中突出的部分x。这意味着线

\fill[rotate around z=40, mild,y={(0,0,1)},z={(0,1,0)}] (0,0,1) -- (0,0,2) arc (90:-90:1) -- cycle;

将更改为

\fill[rotate around z=40, mild,y={(0,0,1)},z={(0,1,0)},x={(1,0.2,0)}] (0,0,1) -- (0,0,2) arc (90:-90:1) -- cycle;

当然,这不是理想的解决方案,但我现在会采用它。

解决方案 2 - 使用 tikz-3dplot

作为一种更系统的方法,可以使用tikz-3dplot如下包:https://latexdraw.com/draw-a-sphere-in-latex-using-tikz/

我使用了以下代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\tdplotsetmaincoords{0}{0}
\definecolor{ocre}{RGB}{243,102,25}
\colorlet{mild}{ocre!50}
\begin{document}
\begin{tikzpicture}[y={(0,-0.385cm)},z={(0,1cm)},scale=1.25]
\fill[rotate around z=40, mild] (0,0) -- (0,0,2) -- (1,0,2) -- (1,0,0) -- cycle;
\draw (1,0) arc (0:180:1);
\draw[dashed] (-1,0) arc (180:360:1);
\draw (0,0,2) circle (1);
\draw[rotate around z=40, dashed] (1,0,0) -- (0,0) -- (0,0,2);
\draw[rotate around z=40] (0,0,2) -- (1,0,2) -- (1,0,0);
\draw (-1,0,0) -- (-1,0,2);
\draw (1,0,0) -- (1,0,2);
\draw[rotate around z=40, scale=0.8] (0,0,0.3) -- (0.3,0,0.3) -- (0.3,0,0);
\begin{scope}[xshift=3cm]
\tdplotsetrotatedcoords{0}{40}{0};
\fill[mild,tdplot_rotated_coords] (0,0,0) arc (-90:90:1);
\draw[tdplot_rotated_coords] (0,0,0) arc (-90:90:1);
\tdplotsetrotatedcoords{0}{0}{0};
\draw[tdplot_rotated_coords] (0,1,0) circle (1);
\draw[dashed,tdplot_rotated_coords] (0,2,0) -- (0,0,0);
\draw (1,0,1) arc (0:180:1);
\draw[dashed] (-1,0,1) arc (180:360:1);
\end{scope}
\end{tikzpicture}
\end{document}

这是我在问题中发布的相同代码,但有一些显着的区别:

  • 增加了\usepackage{tikz-3dplot}指令
  • \tdplotsetmaincoords{0}{0}在序言中添加了命令
  • \tdplotsetrotatedcoords{0}{40}{0};在绘制橙色半圆的命令之前添加。这将取代之前的rotate around z=40旋转形状以及参数y={(0,0,1)},z={(0,1,0)}用于修改原始坐标轴值的参数
  • \fill参数中,添加tdplot_rotated_coords实际使用上面定义的旋转坐标
  • 在绘制旋转形状后添加\tdplotsetrotatedcoords{0}{0}{0};以将坐标重置回“未旋转”状态
  • 使用该tdplot_rotated_coords参数对任何后续绘图/填充/等命令进行操作,以确保以下形状不再旋转

以下是使用两种解决方案后校正图像的比较:

在此处输入图片描述

这里不可能发现差异。只有在非常大的比例(800% 及以上)下才能看到非常小的差异;解决方案 #2 在任何比例下看起来都很完美。

相关内容