我想重新绘制这些立方体,使一半尺寸位于形状的末端,然后修复我的代码

我想重新绘制这些立方体,使一半尺寸位于形状的末端,然后修复我的代码

我一直在使用以下代码为体积工作表绘制立方体。完整的立方体没有问题,但是当一个维度有半个立方体时,它会将半立方体层放在中间某处。我想知道是否有人可以帮助我将分数层放在顶部表示高度,或放在末尾表示深度或宽度。我有两个问题:1. 我可以将半尺寸放在顶部或末尾吗?2. 我可以引用直接的高度或宽度,而不是我用来获得比实际值小 1 的 hack,以便代码合适。
这就是我得到的。 在此处输入图片描述

但我希望半尺寸放在上面。
也许我弄错了整个代码宏。请有人帮忙。

\documentclass[11pt,fleqn]{examdesign}
    \usepackage{savesym}
    \usepackage{amsmath}
    \savesymbol{iint}
    \usepackage{txfonts}
    \restoresymbol{TXF}{iint}
    %%\usepackage{amssymb}  causes an error when loaded
    \usepackage{pifont}
    \usepackage[usenames,dvipsnames]{xcolor}
    \usepackage{xspace}
    \SectionFont{\large\sffamily}
    \usepackage[shortlabels,inline]{enumitem}  %%shortlabels here for matching
    \usepackage{tkz-euclide} %%%%%%%%%%%for marking angles
    \usetkzobj{all}  %%%%%%%%%%%necessary cause using overleaf
    \usepackage{tikz,ifthen,tikz-3dplot}
    \usepackage[font=scriptsize,hypcap=false]{caption}
    \graphicspath{ {./dir1/} }
    \usepackage{hyperref}
    \Fullpages
    \ContinuousNumbering
    \ShortKey
    %%\NoKey
    \DefineAnswerWrapper{}{}
    \NumberOfVersions{1}
    \IncludeFromFile{foobar.tex}

    \class{necessary for examdesign}

    %%%%%%%%%%%%%%%%%%%%% tikz set for cube%%%%%%%%%%%%%%%%%%%%%%
    \newcommand{\cubelength}{0.93}
    \tikzset{
      cube/.pic={
        \draw[] (0,0,0) -- (0,\cubelength,0) -- (\cubelength,\cubelength,0) -- (\cubelength,0,0) -- cycle;
        %draw the back-right of the cube
        \draw[] (0,0,0) -- (0,\cubelength,0) -- (0,\cubelength,\cubelength) -- (0,0,\cubelength) -- cycle;
        %draw the back-left of the cube
        \draw[] (0,0,0) -- (\cubelength,0,0) -- (\cubelength,0,\cubelength) -- (0,0,\cubelength) -- cycle;
        %draw the front-right of the cube
        \draw[fill=magenta] (\cubelength,0,0) -- (\cubelength,\cubelength,0) -- (\cubelength,\cubelength,\cubelength) -- (\cubelength,0,\cubelength)-- cycle;
        %draw the front-left of the cube
        \draw[fill=orange] (0,\cubelength,0) -- (\cubelength,\cubelength,0) -- (\cubelength,\cubelength,\cubelength) -- (0,\cubelength,\cubelength) -- cycle;
        %draw the top of the cube
        \draw[fill=cyan] (0,0,\cubelength) -- (0,\cubelength,\cubelength) -- (\cubelength,\cubelength,\cubelength) -- (\cubelength,0,\cubelength) -- cycle;        
                }
            }
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%%
    %%%BEGIN DOCUMENT
    %%%%%
    \begin{document}
                \begin{scriptsize}
    \newcommand{\Depth}{3}
    \newcommand{\Height}{3.5}
    \newcommand{\Width}{4}
    \newcommand{\dcube}{\the\numexpr\Depth-1\relax}
    \newcommand{\wcube}{\the\numexpr\Width-1\relax}
    \pgfmathsetmacro{\hcube}{(\Height-1)}
 \begin{tikzpicture}[scale=1]
    \coordinate (A) at (0,0,0);
    \coordinate (B) at (\Width,0,0);
    \coordinate (C) at (\Width,0,\Depth);
    \coordinate (D) at (0,0,\Depth);
    \coordinate (E) at (0,\Height,\Depth);
    \coordinate (F) at (0,\Height,0);
    \coordinate (G) at (\Width,\Height,0);
    \coordinate (H) at (\Width,\Height,\Depth);



    \draw[fill=red!20,opacity=0.2] (A) -- (B) -- (C) -- (D) -- cycle;% Bottom Face
    \draw[fill=gray!50] (F) -- (G) -- (B) -- (A) -- cycle;% Back Face
    \draw[fill=gray!30] (A) -- (D) -- (E) -- (F) -- cycle;% Left Face
    \draw[fill=gray!30] (B) -- (C) -- (H) -- (G) -- cycle;% Right Face
    \draw[] (E) -- (F) -- (G) -- (H) -- cycle;% Top Face
    \draw[dashed,gray] (A) -- (F);
    \draw[dashed,gray] (D) -- (A) --(B);
    \draw[fill=gray!30] (B) -- (C) -- (H) -- (G); %%right face
    \draw[fill=none] (C) -- (D) --(E);
    \tkzMarkRightAngle[draw=blue,size=.2](C,B,G);
    \draw[fill=gray!50] (C) -- (D) -- (E) -- (H) -- cycle;% Front Face
    \draw[<->,blue,>=latex]([xshift=4mm]B)--node[fill=white,xshift=3mm]{\Height\text{ cm}}([xshift=4mm]G);
    \draw[<->,blue, >=latex]([yshift=-3mm]C)--node[fill=white,yshift=-3mm]{\Width \text{  cm}}([yshift=-3mm]D);
    \draw[<->,blue]([xshift=5mm]B)--node[xshift=3mm,yshift=-3mm,fill=white]{\Depth\text{ cm}}([xshift=5mm]C);

    \draw[|-|,blue,>=latex]([xshift=-4mm]E)--node[right,fill=white,xshift=-3mm,yshift=0.5mm]{$w$}([xshift=-4mm]F);

        \foreach \x in {0,...,\wcube}{
        \foreach \y in {0,1,2,\hcube}{
        \foreach \z in {0,...,\dcube}{
        \pic at (\x,\y,\z) {cube};
       }}}

       \pic at (0,\Height+2,0) {cube};

    \end{tikzpicture}

    \end{scriptsize}
    \end{document}

答案1

这是针对该问题的一个临时修复。一些整数技巧。它重新定义了分数立方的基向量。(请注意,xscaleyscale不会起作用。)

\documentclass[11pt,fleqn]{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{tkz-euclide} 
\usepackage{hyperref}
  %%%%%%%%%%%%%%%%%%%%% tikz set for cube%%%%%%%%%%%%%%%%%%%%%%
  \newcommand{\cubelength}{0.93}
  \tikzset{
    cube/.pic={
      \draw[] (0,0,0) -- (0,\cubelength,0) -- (\cubelength,\cubelength,0) -- (\cubelength,0,0) -- cycle;
      %draw the back-right of the cube
      \draw[] (0,0,0) -- (0,\cubelength,0) -- (0,\cubelength,\cubelength) -- (0,0,\cubelength) -- cycle;
      %draw the back-left of the cube
      \draw[] (0,0,0) -- (\cubelength,0,0) -- (\cubelength,0,\cubelength) -- (0,0,\cubelength) -- cycle;
      %draw the front-right of the cube
      \draw[fill=magenta] (\cubelength,0,0) -- (\cubelength,\cubelength,0) -- (\cubelength,\cubelength,\cubelength) -- (\cubelength,0,\cubelength)-- cycle;
      %draw the front-left of the cube
      \draw[fill=orange] (0,\cubelength,0) -- (\cubelength,\cubelength,0) -- (\cubelength,\cubelength,\cubelength) -- (0,\cubelength,\cubelength) -- cycle;
      %draw the top of the cube
      \draw[fill=cyan] (0,0,\cubelength) -- (0,\cubelength,\cubelength) -- (\cubelength,\cubelength,\cubelength) -- (\cubelength,0,\cubelength) -- cycle;        
              }
          }
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%%
    %%%BEGIN DOCUMENT
    %%%%%
\begin{document}
\newcommand{\Depth}{3}
\newcommand{\Height}{3.5}
\newcommand{\Width}{4}
\pgfmathsetmacro{\effDepth}{\Depth-1}
\pgfmathsetmacro{\effHeight}{\Height-1}
\pgfmathsetmacro{\effWidth}{\Width-1}
\begin{tikzpicture}[scale=1]
    \coordinate (A) at (0,0,0);
    \coordinate (B) at (\Width,0,0);
    \coordinate (C) at (\Width,0,\Depth);
    \coordinate (D) at (0,0,\Depth);
    \coordinate (E) at (0,\Height,\Depth);
    \coordinate (F) at (0,\Height,0);
    \coordinate (G) at (\Width,\Height,0);
    \coordinate (H) at (\Width,\Height,\Depth);



    \draw[fill=red!20,opacity=0.2] (A) -- (B) -- (C) -- (D) -- cycle;% Bottom Face
    \draw[fill=gray!50] (F) -- (G) -- (B) -- (A) -- cycle;% Back Face
    \draw[fill=gray!30] (A) -- (D) -- (E) -- (F) -- cycle;% Left Face
    \draw[fill=gray!30] (B) -- (C) -- (H) -- (G) -- cycle;% Right Face
    \draw[] (E) -- (F) -- (G) -- (H) -- cycle;% Top Face
    \draw[dashed,gray] (A) -- (F);
    \draw[dashed,gray] (D) -- (A) --(B);
    \draw[fill=gray!30] (B) -- (C) -- (H) -- (G); %%right face
    \draw[fill=none] (C) -- (D) --(E);
    \tkzMarkRightAngle[draw=blue,size=.2](C,B,G);
    \draw[fill=gray!50] (C) -- (D) -- (E) -- (H) -- cycle;% Front Face
    \draw[<->,blue,>=latex]([xshift=4mm]B)--node[fill=white,xshift=3mm]{\Height\text{ cm}}([xshift=4mm]G);
    \draw[<->,blue, >=latex]([yshift=-3mm]C)--node[fill=white,yshift=-3mm]{\Width \text{  cm}}([yshift=-3mm]D);
    \draw[<->,blue]([xshift=5mm]B)--node[xshift=3mm,yshift=-3mm,fill=white]{\Depth\text{ cm}}([xshift=5mm]C);

 \draw[|-|,blue,>=latex]([xshift=-4mm]E)--node[right,fill=white,xshift=-3mm,yshift=0.5mm]{$w$}([xshift=-4mm]F);
     \foreach \x in {0,...,\effWidth,\effWidth}{
        \pgfmathifisint{\x}{\pgfmathsetmacro{\myx}{1}}{\pgfmathsetmacro{\myx}{\x-int(\x))}}
     \foreach \y in {0,...,\effHeight,\effHeight}{
        \pgfmathifisint{\y}{\pgfmathsetmacro{\myy}{1}}{\pgfmathsetmacro{\myy}{\y-int(\y))}}
     \foreach \z in {0,...,\effDepth,\effDepth}{
        \pgfmathifisint{\z}{\pgfmathsetmacro{\myz}{1}}{\pgfmathsetmacro{\myz}{\z-int(\z))}}
     \path (\x+1-\myx,\y+1-\myy,\z+1-\myz) 
     pic[z={(-0.5*\myz,-0.5*\myz)},x={(\myx,0)},y={(0,\myy)}] {cube};
    }}}
    \pic at (0,\Height+2,0) {cube};

\end{tikzpicture}

\end{document}

在此处输入图片描述

如果你把\Width变成\newcommand{\Width}{4.5}

在此处输入图片描述

如果你改变\Depth成为\newcommand{\Depth}{3.5}

在此处输入图片描述

它也适用于其他分数,例如

\newcommand{\Depth}{3}
\newcommand{\Height}{3.2}
\newcommand{\Width}{4}

产量

在此处输入图片描述

当然,这些不是正交投影。有立方体阵列有可用的版本,没有这个限制。据我所知,市场上没有此类版本。上述技巧也适用于这些版本。

相关内容