3

I use tikz-3dplot and I expect that rectangle, grid and similar objects are rotated along with the rest part of the figure. However, they are drawn as if they are in 2D. How do I force a rectangle to be drawn in the rotated coodinates?

\documentclass{article}

\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{patterns, arrows, matrix, positioning, decorations.markings, arrows.meta, decorations.pathreplacing}
\usepackage{tikz-3dplot}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}

\begin{document}

\tdplotsetmaincoords{70}{110}
\def\mycolor{blue}


\begin{tikzpicture}[scale=1, tdplot_main_coords, axis/.style={->,\mycolor,thick}]

    \def\latnum{8}
    \def\lonnum{8}

    \draw[axis] (0,0,0) -- (\latnum+1,0,0) node[anchor=north east]{\huge $lat$};
    \draw[axis] (0,0,0) -- (0,\lonnum+1,0) node[anchor=north west]{\huge $lon$};

    \fill [orange] (\latnum-2,\lonnum-2,0) rectangle (2,2,0);

\end{tikzpicture}

\end{document}
Stefan Pinnow
  • 29,535
Antonio
  • 197
  • Because given two points does not determine a rectangle. You have to make clear on which plan the rectangle lies. But before you do so, you will find that it is easier to draw a rectangle with \draw(A)--(B)--(C)--(D)--cycle;. – Symbol 1 Apr 30 '17 at 18:42
  • I want to draw a filled rectangle, that is why I use \fill. How do I do that in 3D? – Antonio Apr 30 '17 at 19:50
  • I figured that out: \fill [orange] (\latnum,\lonnum,0) -- ++(-2,0,0) -- ++(0,-2,0) -- ++(2,0,0) -- cycle; Thank you. – Antonio Apr 30 '17 at 20:09
  • @Symbol1 Do you want to answer? Or should the OP? – cfr Apr 30 '17 at 21:37
  • @cfr Maybe OP. Or you. – Symbol 1 Apr 30 '17 at 21:46

1 Answers1

3

This is a very interesting question.

We expect that the following lines are equivalent. But they are not.

\draw[red](0,0)rectangle(1,1);
\draw[blue](0,0)--(1,0)--(1,1)--(0,1)--cycle;

The rectangle operator is processed by TikZ by the following lines (tikz.code.tex line 3140-3148

  \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}%
  \tikz@path@lineto{\pgfqpoint{\pgf@xa}{\tikz@lasty}}%
  \tikz@path@lineto{\pgfqpoint{\tikz@lastx}{\tikz@lasty}}%
  \tikz@path@lineto{\pgfqpoint{\tikz@lastx}{\pgf@ya}}%
  \pgfpathclose%
  \pgfpathmoveto{\pgfqpoint{\tikz@lastx}{\tikz@lasty}}%

At this level, PGF does not recognize the 3D structure of the graphics, so the rectangle are drawn on the canvas in the 2D-way.



And here is something really ironic: If you \usetikzlibrary{3d} and write

\draw[canvas is xy plane at z=0](0,0)rectangle(1,1)circle(1);
\draw[canvas is yz plane at x=0](0,0)rectangle(1,1)circle(1);
\draw[canvas is zx plane at y=0](0,0)rectangle(1,1)circle(1);

Then three circles are drawn correctly and two out of three rectangles are drawn correctly. The incorrect one is the one on the xy-plane.

This is fixed in Draw 3D rectangle.

Symbol 1
  • 36,855
  • Or one can use canvas is yx plane at z=0 (notice that it is y and then x.) This is purely evil. – Symbol 1 May 01 '17 at 00:15
  • Thank you. Does this mean that I cannot draw a rectangle not in yx or other 2 planes? I mean is there another way of specifying the plane for a rectangle except those 3 choices? – Antonio May 01 '17 at 10:28
  • Also, I had troubles with grid. Can you give some examples of drawing grid in 3D? Is the workaround the same as for a rectangle? – Antonio May 01 '17 at 10:28
  • @Antonio If you use tikz-3dplot there is a \tdplotsetthetaplanecoords. I cannot guarantee that it will work as you desired. But that is the closest thing I can think of. – Symbol 1 May 01 '17 at 17:13