\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\cubex}{2}
\pgfmathsetmacro{\cubey}{2}
\pgfmathsetmacro{\cubez}{2}
\draw[red] (0,0,0)-- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle ;
\draw[red] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw[red] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\draw (0,0,0) node {x};
\end{tikzpicture}
\end{document}
我正在尝试延长上述立方体的宽度。但是当我在代码中更改 3 元组中的某些组件时,立方体会变形。我应该怎么做才能水平延长立方体?
\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\cubex}{2}
\pgfmathsetmacro{\cubey}{2}
\pgfmathsetmacro{\cubez}{2}
\draw[red] (0,0,0)-- ++(-\cubex,2,0) -- ++(0,-\cubey,0) -- ++(\cubex,2,0) -- cycle ;
\draw[red] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw[red] (0,0,0) -- ++(-\cubex,2,0) -- ++(0,0,-\cubez) -- ++(\cubex,2,0) -- cycle;
\draw (0,0,0) node {x};
\end{tikzpicture}
\end{document}
答案1
您可以简单地更改相关内容\pgfmathsetmacro
,但如果只更改一个,它就不会是立方体。
我还会添加line join=round
一些平滑的立方体角,因为它们看起来有些参差不齐。 加倍\cubex
并改变连接会产生以下结果:
\documentclass[tikz, border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}[line join=round, draw=red]
\pgfmathsetmacro{\cubex}{4}
\pgfmathsetmacro{\cubey}{2}
\pgfmathsetmacro{\cubez}{2}
\draw (0,0,0)-- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle
(0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle
(0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\draw (0,0,0) node {x};
\end{tikzpicture}
\end{document}
为了进一步演示,以下是以下设置的结果
\pgfmathsetmacro{\cubex}{4}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{3}
本质上,这\pgfmathsetmacro
是为您提供一种沿相关轴改变每个长方体尺寸的方法。
如果这不直观,您可以定义一个键值接口来设置长方体的宽度、高度和深度。
\tikzset{%
cuboid/.code={%
\tikzset{%
cuboid dims/.cd,
#1
}
},
cuboid dims/.search also={/tikz},
cuboid dims/.cd,
width/.store in=\cubex,
height/.store in=\cubey,
depth/.store in=\cubez,
width=1,
height=1,
depth=1,
}
\begin{tikzpicture}[ draw=red, cuboid={width=5, height=2, depth=2}]
\draw (0,0,0)-- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}
或者用蓝色阴影
\documentclass[tikz, border=10pt,multi]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[line join=round, draw=red]
\pgfmathsetmacro{\cubex}{4}
\pgfmathsetmacro{\cubey}{2}
\pgfmathsetmacro{\cubez}{2}
\draw (0,0,0)-- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle
(0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle
(0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\draw (0,0,0) node {x};
\end{tikzpicture}
\begin{tikzpicture}[line join=round, draw=red]
\pgfmathsetmacro{\cubex}{4}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{3}
\draw (0,0,0)-- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle
(0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle
(0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\draw (0,0,0) node {x};
\end{tikzpicture}
\tikzset{%
cuboid/.code={%
\tikzset{%
cuboid dims/.cd,
#1
}
},
cuboid dims/.search also={/tikz},
cuboid dims/.cd,
width/.store in=\cubex,
height/.store in=\cubey,
depth/.store in=\cubez,
width=1,
height=1,
depth=1,
}
\begin{tikzpicture}[draw=red, cuboid={width=5, height=2, depth=2}]
\draw (0,0,0)-- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}
\begin{tikzpicture}[draw=blue, thick, line join=round, cuboid={width=5, height=2, depth=2}]
\draw [top color=white, bottom color=blue, fill opacity=.5, shading angle=-45] (0,0,0)-- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw [top color=white, bottom color=blue, fill opacity=.5, shading angle=45] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle ;
\draw [top color=white, bottom color=blue, fill opacity=.5, shading angle=-135] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\draw (0,0,0) node {x};
\end{tikzpicture}
\end{document}
答案2
像这样 :
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\cubex}{2}
\pgfmathsetmacro{\cubey}{2}
\pgfmathsetmacro{\cubez}{2}
\draw[red] (0,0,0)-- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle ;
\draw[red] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw[red] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\draw (0,0,0) node {x};
\end{tikzpicture}
\begin{tikzpicture}[scale=3]
\pgfmathsetmacro{\cubex}{2}
\pgfmathsetmacro{\cubey}{2}
\pgfmathsetmacro{\cubez}{2}
\draw[red] (0,0,0)-- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle ;
\draw[red] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw[red] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\draw (0,0,0) node {x};
\end{tikzpicture}
\end{document}