我正在尝试使用 进行一些绘图tikz-3dplot
,类似于下图,其中轴垂直于长方体平面。
但是,在我的例子中,轴似乎与长方体表面明显错位。这可能是由于渲染问题,也可能是我做错了什么。有什么方法可以纠正这个问题吗?
以下是片段:
\documentclass{standalone}
\usepackage{tikz, tikz-3dplot}
\usetikzlibrary{arrows, arrows.meta}
\tikzset{
mycube/.style ={
color = black!80,
fill = blue!50,
line width = 0.2pt,
rounded corners=1.5pt,
opacity=0.8
}
}
% Defining the cuboid with a bottom left corner at (x,y,z) cordinate and length, width
\newcommand{\cube}[6]{
\pgfmathsetmacro{\cubelength}{#4}
\pgfmathsetmacro{\cubewidth}{#5}
\pgfmathsetmacro{\cubethickness}{#6}
\draw[mycube, tdplot_main_coords] (#1,#2,#3) -- ++(0,\cubewidth,0) -- ++(0,0, \cubethickness) -- ++(0, -\cubewidth, 0) -- cycle;
\draw[mycube, shift = {(-\cubelength, 0, 0)}, tdplot_main_coords] (#1,#2,#3) -- ++(0,\cubewidth,0) -- ++(0,0, \cubethickness) -- ++(0, -\cubewidth, 0) -- cycle;
\draw[mycube, tdplot_main_coords] (#1,#2,#3) -- ++(-\cubelength,0,0) -- ++(0,0, \cubethickness) -- ++(\cubelength, 0, 0) -- cycle;
\draw[mycube, shift = {(0, \cubewidth, 0)}, tdplot_main_coords] (#1,#2,#3) -- ++(-\cubelength,0,0) -- ++(0,0, \cubethickness) -- ++(\cubelength, 0, 0) -- cycle;
\draw[mycube, tdplot_main_coords] (#1,#2,#3) -- ++(-\cubelength,0,0) -- ++(0,\cubewidth, 0) -- ++(\cubelength, 0, 0) -- cycle;
\draw[mycube, shift = {(0, 0, \cubethickness)}, tdplot_main_coords] (#1,#2,#3) -- ++(-\cubelength,0,0) -- ++(0,\cubewidth, 0) -- ++(\cubelength, 0, 0) -- cycle;
}
%
\begin{document}
\tdplotsetmaincoords{75}{130}
\begin{tikzpicture}[scale=2,tdplot_main_coords]
% Defining the axis
\draw[>=triangle 45, ->, tdplot_main_coords,] (1.3, 0,0 ) -- (2, 0, 0) node[anchor=north east]{$x$};
\draw[>=triangle 45, ->, tdplot_main_coords,] (0, 0,0 ) -- (0, 2, 0) node[anchor=north east]{$y$};
\draw[>=triangle 45, ->, tdplot_main_coords,] (0, 0,0 ) -- (0, 0, 2) node[anchor=north east]{$z$};
\draw[>=triangle 45, ->, tdplot_main_coords,] (0, 0,0 ) -- (-2, 0, 0) node[anchor=north east]{$-x$};
\draw[>=triangle 45, ->, tdplot_main_coords,] (0, 0,0 ) -- (0, -2, 0) node[anchor=north east]{$-y$};
\draw[>=triangle 45, ->, tdplot_main_coords,] (0, 0,0 ) -- (0, 0, -1) node[anchor=north east]{$-z$};
%Draw the cuboid
%\cube{x}{y}{z}{legth}{width}{thickness}
%
\cube{1}{-1}{-0.1}{2}{2}{0.2}
\end{tikzpicture}
\end{document}
答案1
使图像更好的一种方法是在立方体后绘制正轴并调整初始点:
##代码:
\documentclass[border=5pt]{standalone}
\usepackage{tikz, tikz-3dplot}
\usetikzlibrary{arrows, arrows.meta}
\tikzset{
mycube/.style ={
color = black!80,
fill = blue!50,
line width = 0.2pt,
rounded corners=1.5pt,
opacity=0.8
}
}
% Defining the cuboid with a bottom left corner at (x,y,z) cordinate and length, width
\newcommand{\cube}[6]{
\pgfmathsetmacro{\cubelength}{#4}
\pgfmathsetmacro{\cubewidth}{#5}
\pgfmathsetmacro{\cubethickness}{#6}
\draw[mycube, tdplot_main_coords, red] (#1,#2,#3) -- ++(0,\cubewidth,0) -- ++(0,0, \cubethickness) -- ++(0, -\cubewidth, 0) -- cycle;
\draw[mycube, shift = {(-\cubelength, 0, 0)}, tdplot_main_coords] (#1,#2,#3) -- ++(0,\cubewidth,0) -- ++(0,0, \cubethickness) -- ++(0, -\cubewidth, 0) -- cycle;
\draw[mycube, tdplot_main_coords] (#1,#2,#3) -- ++(-\cubelength,0,0) -- ++(0,0, \cubethickness) -- ++(\cubelength, 0, 0) -- cycle;
\draw[mycube, shift = {(0, \cubewidth, 0)}, tdplot_main_coords] (#1,#2,#3) -- ++(-\cubelength,0,0) -- ++(0,0, \cubethickness) -- ++(\cubelength, 0, 0) -- cycle;
\draw[mycube, tdplot_main_coords] (#1,#2,#3) -- ++(-\cubelength,0,0) -- ++(0,\cubewidth, 0) -- ++(\cubelength, 0, 0) -- cycle;
\draw[mycube, shift = {(0, 0, \cubethickness)}, tdplot_main_coords] (#1,#2,#3) -- ++(-\cubelength,0,0) -- ++(0,\cubewidth, 0) -- ++(\cubelength, 0, 0) -- cycle;
}
%
\begin{document}
\tdplotsetmaincoords{75}{130}
\begin{tikzpicture}[scale=2,tdplot_main_coords]
% Defining the negative axis
\draw[>=triangle 45, ->, tdplot_main_coords,] (0, 0,0 ) -- (-2, 0, 0) node[anchor=north east]{$-x$};
\draw[>=triangle 45, ->, tdplot_main_coords,] (0, 0,0 ) -- (0, -2, 0) node[anchor=north east]{$-y$};
\draw[>=triangle 45, ->, tdplot_main_coords,] (0, 0,0 ) -- (0, 0, -1) node[anchor=north east]{$-z$};
%Draw the cuboid
%\cube{x}{y}{z}{legth}{width}{thickness}
\cube{1}{-1}{-0.1}{2}{2}{0.2}
% Defining the positive negative axis
\draw[>=triangle 45, ->, tdplot_main_coords,] (1.1, 0,0 ) -- (2, 0, 0) node[anchor=north east]{$x$};
\draw[>=triangle 45, ->, tdplot_main_coords,] (0, 1.1,0 ) -- (0, 2, 0) node[anchor=north east]{$y$};
\draw[>=triangle 45, ->, tdplot_main_coords,] (0, 0, 0.1 ) -- (0, 0, 1) node[anchor=north east]{$z$};
\end{tikzpicture}
\end{document}