使用相对位置绘制矩形

使用相对位置绘制矩形

我使用以下代码在河边画了一棵树:

\begin{tikzpicture}[{x=(1mm,0)},{y=(0,1mm)}]
%-> DEFINITIONS
%... colors
\def\rivercolor{gray!25}
\def\earthcolor{gray!50}
\def\treecolor{gray!75}

%... sizes
\def\treehgt{5}            % the height of the tree
\def\treewdt{\treehgt/4}    % the width of the tree
\def\treerad{\treehgt/4}    % the radius of the tree's cup
\def\riverwd{25}            % the width of the river
\def\riverlg{2*\riverwd}    % the length of the river

%... the tree
\def\tree#1{% #1=position
    \draw[fill=\treecolor]
        #1++(-\treewdt/2,0)--++
            (\treewdt,0)--++
            (0,\treehgt)--++
            (-\treewdt,0)--cycle
        ;
    \foreach \ang in {0,60,...,300}
        \draw[fill=\treecolor]
            {#1++(0,\treehgt+\treerad)++(\ang:\treerad)}
                circle(\treerad)
            ;
    \draw[\treecolor,fill=\treecolor]
        #1++(0,\treehgt+\treerad)
            circle(\treerad)
        ;
    }

%-> DRAW THE MARGINS
\draw[\earthcolor,fill=\earthcolor]
    (-\riverlg/5,-\riverwd/5) rectangle(\riverlg,6*\riverwd/5)
    ;
%- DRAW THE RIVER
\draw[\rivercolor,fill=\rivercolor]
    (-\riverlg/5,0) rectangle(\riverlg,\riverwd)
    ;
%-> DRAW THE TREE
\tree{(4*\riverlg/5,\riverwd)}
\end{tikzpicture}

但是我想用矩形来画它的茎。我尝试了以下代码

%... the tree
\def\tree#1{% #1=position
    \draw[fill=\treecolor]
        {#1++(-\treewdt/2,0)} rectangle(\treewdt,\treehgt)
        ;
    \foreach \ang in {0,60,...,300}
        \draw[fill=\treecolor]
            {#1++(0,\treehgt+\treerad)++(\ang:\treerad)}
                circle(\treerad)
            ;
    \draw[\treecolor,fill=\treecolor]
        #1++(0,\treehgt+\treerad)
            circle(\treerad)
        ;
    }

但它不起作用。

我的问题是:矩形为什么不遵循相对定位坐标?

答案1

您没有使用相对坐标作为矩形的端点,请尝试

... rectangle +(\treewdt,\treehgt)

注意添加的+

相关内容