8

I would like to generate the following image of TikZ:

The closest resemblance on the examples is this post. However, I am not sure how to add the circles in the plane of the cubes. Any help is appreciated.

5
  • 4
    Welcome to TeX.SX! The short answer is that, yes, this is possible but I have to warn you that questions of the form "Please draw this for me" that show no effort on the part of OP, often don't get answered. You will get more help if you post some code showing what you have tried and give a minimal working example. A quick search on TeX.SX for drawing functions (with tikz or pstricks) will give you an idea of where to start from.
    – user30471
    Aug 22, 2018 at 11:51
  • @Andrew Even though I agree with you to a large extent, I somehow cannot refrain from making a comment. If the OP would have just copied the code under the link, not disclosed its source, and posted it as his MWE, he would be drowning in upvotes. I personally like an honest reference and an honest "I am not sure how to add the circles in the plane of the cubes" better than the alternative which one sometimes encounters. I think the OP has done his homework, i.e. done some research, and asked a very clear question.
    – user121799
    Aug 22, 2018 at 18:42
  • @marmot In an ideal world the OP would post the code they found and give the link. The problem, as always, with external links is that they are likely to disappear at some point, significantly weakening the question.
    – user30471
    Aug 22, 2018 at 20:41
  • @Andrew I fully agree with that statement, but given the world we live in I would like to argue that the present question is actually quite nice. It is clear and honest. Of course, like in most posts, there is room for improvement.
    – user121799
    Aug 22, 2018 at 21:29
  • @Andrew, thank you for the comments! I hadn't thought of the longevity of the post and the problem external posts may have in the long run. Any future posts will take this into account.
    – Sid
    Aug 23, 2018 at 8:44

2 Answers 2

17
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{3d,decorations.markings,calc}
\usepackage{tikz-3dplot}
\makeatletter % https://tex.stackexchange.com/a/48776/121799
\tikzoption{canvas is xy plane at z}[]{%
  \def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}%
  \def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}%
  \def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}%
  \tikz@canvas@is@plane
}
\makeatother

\begin{document}
\foreach \X in {-50}  
{\tdplotsetmaincoords{110}{\X}
\begin{tikzpicture}
%\path[use as bounding box] (-6,-3) rectangle (6,5);
%\node[anchor=north west] at (-6,5) {\X,\xtest,\ytest};
\begin{scope}[tdplot_main_coords]
% ray in the back
\draw [thick,blue,postaction={decorate,decoration={markings,mark=at position 0.2
with {\arrow{stealth};}}}] (-6,0,0) -- (0,0,0);
% back
\filldraw[cyan,fill opacity=0.3] (-0.5,-2,-2) -- (-0.5,2,-2) -- (-0.5,2,2)
-- (-0.5,-2,2) -- cycle;
% bottom
\filldraw[cyan,fill opacity=0.3] (0,-2,-2) -- (-0.5,-2,-2) -- (-0.5,2,-2)
-- (0,2,-2) -- cycle;
% right
\filldraw[cyan,fill opacity=0.1] (0,2,-2) -- (-0.5,2,-2) -- (-0.5,2,2)
-- (0,2,2) -- cycle;
% left
\filldraw[cyan,fill opacity=0.3] (0,-2,-2) -- (-0.5,-2,-2) -- (-0.5,-2,2)
-- (0,-2,2) -- cycle;
% top
\filldraw[cyan,fill opacity=0.1] (0,-2,2) -- (-0.5,-2,2) -- (-0.5,2,2)
-- (0,2,2) -- cycle;
%front
\filldraw[cyan,fill opacity=0.1] (0,-2,-2) -- (0,2,-2) -- (0,2,2)
-- (0,-2,2) -- cycle;
% ray in front
\draw[thick,blue] (0,0,0) -- (8,0,0);
\begin{scope}[canvas is yz plane at x=8,xscale=-1,transform shape]
\draw[gray] (-4,0) -- (4,0);
\draw[gray] (0,-4) -- (0,4);
\draw[red,thick] (0,0) circle (2);
\node[circle,fill,red,inner sep=2pt] (A) at (45:2){};
\node[circle,fill,red,inner sep=2pt] (B) at (-135:2){};
\draw[gray] (A) circle (4.5pt) (B) circle (4.5pt);
\draw[gray,shorten >=1.5pt,shorten <=1.5pt] (A)--(B);
\end{scope}
\draw[red] (A) -- (0,0,0) (B) -- (0,0,0);
\end{scope}
\end{tikzpicture}}

\foreach \X in {-50}  
{\tdplotsetmaincoords{110}{\X}
\begin{tikzpicture}
%\path[use as bounding box] (-6,-3) rectangle (6,5);
%\node[anchor=north west] at (-6,5) {\X,\xtest,\ytest};
\begin{scope}[tdplot_main_coords]
% ray in the back
\draw [thick,blue,postaction={decorate,decoration={markings,mark=at position 0.2
with {\arrow{stealth};}}}] (-6,0,0) -- (0,0,0);
% back
\filldraw[cyan,fill opacity=0.3] (-0.5,-2,-2) -- (-0.5,2,-2) -- (-0.5,2,2)
-- (-0.5,-2,2) -- cycle;
% bottom
\filldraw[cyan,fill opacity=0.3] (0,-2,-2) -- (-0.5,-2,-2) -- (-0.5,2,-2)
-- (0,2,-2) -- cycle;
% right
\filldraw[cyan,fill opacity=0.1] (0,2,-2) -- (-0.5,2,-2) -- (-0.5,2,2)
-- (0,2,2) -- cycle;
% left
\filldraw[cyan,fill opacity=0.3] (0,-2,-2) -- (-0.5,-2,-2) -- (-0.5,-2,2)
-- (0,-2,2) -- cycle;
% top
\filldraw[cyan,fill opacity=0.1] (0,-2,2) -- (-0.5,-2,2) -- (-0.5,2,2)
-- (0,2,2) -- cycle;
%front
\filldraw[cyan,fill opacity=0.1] (0,-2,-2) -- (0,2,-2) -- (0,2,2)
-- (0,-2,2) -- cycle;
% ray in front
\draw[thick,blue] (0,0,0) -- (8,0,0);
\begin{scope}[canvas is yz plane at x=8,xscale=-1,transform shape]
\draw[gray] (-5,0) -- (5,0);
\draw[gray] (0,-4) -- (0,4);
\draw[red,thick] (1,0) circle (2);
\draw[red,thick] (-1,0) circle (2);
\node[circle,fill,red,inner sep=2pt] (A) at ($(1,0)+(45:2)$) {};
\node[circle,fill,red,inner sep=2pt] (B) at ($(-1,0)+(-135:2)$) {};
\draw[gray] (A) circle (4.5pt) (B) circle (4.5pt);
\draw[gray,shorten >=1.5pt,shorten <=1.5pt] (A)--(B);
\end{scope}
\draw[red] (A) -- (0,0,0) (B) -- (0,0,0);
\end{scope}
\end{tikzpicture}}

\foreach \X in {-50}  
{\tdplotsetmaincoords{110}{\X}
\begin{tikzpicture}
%\path[use as bounding box] (-6,-3) rectangle (6,5);
%\node[anchor=north west] at (-6,5) {\X,\xtest,\ytest};
\begin{scope}[tdplot_main_coords]
% ray in the back
\draw [thick,blue,postaction={decorate,decoration={markings,mark=at position 0.2
with {\arrow{stealth};}}}] (-6,0,0) -- (0,0,0);
% back
\filldraw[cyan,fill opacity=0.3] (-0.5,-2,-2) -- (-0.5,2,-2) -- (-0.5,2,2)
-- (-0.5,-2,2) -- cycle;
% bottom
\filldraw[cyan,fill opacity=0.3] (0,-2,-2) -- (-0.5,-2,-2) -- (-0.5,2,-2)
-- (0,2,-2) -- cycle;
% right
\filldraw[cyan,fill opacity=0.1] (0,2,-2) -- (-0.5,2,-2) -- (-0.5,2,2)
-- (0,2,2) -- cycle;
% left
\filldraw[cyan,fill opacity=0.3] (0,-2,-2) -- (-0.5,-2,-2) -- (-0.5,-2,2)
-- (0,-2,2) -- cycle;
% top
\filldraw[cyan,fill opacity=0.1] (0,-2,2) -- (-0.5,-2,2) -- (-0.5,2,2)
-- (0,2,2) -- cycle;
%front
\filldraw[cyan,fill opacity=0.1] (0,-2,-2) -- (0,2,-2) -- (0,2,2)
-- (0,-2,2) -- cycle;
% ray in front
\draw[thick,blue] (0,0,0) -- (8,0,0);
\begin{scope}[canvas is yz plane at x=8,xscale=-1,transform shape]
\draw[gray] (-5,0) -- (5,0);
\draw[gray] (0,-4) -- (0,4);
\draw[red,thick] (2,0) circle (2);
\draw[red,thick] (-2,0) circle (2);
\node[circle,fill,red,inner sep=2pt] (A) at ($(2,0)+(45:2)$) {};
\node[circle,fill,red,inner sep=2pt] (B) at ($(-2,0)+(-135:2)$) {};
\draw[gray] (A) circle (4.5pt) (B) circle (4.5pt);
\draw[gray,shorten >=1.5pt,shorten <=1.5pt] (A)--(B);
\end{scope}
\draw[red] (A) -- (0,0,0) (B) -- (0,0,0);
\end{scope}
\end{tikzpicture}}

\foreach \X in {-50}  
{\tdplotsetmaincoords{110}{\X}
\begin{tikzpicture}
%\path[use as bounding box] (-6,-3) rectangle (6,5);
%\node[anchor=north west] at (-6,5) {\X,\xtest,\ytest};
\begin{scope}[tdplot_main_coords]
% ray in the back
\draw [thick,blue,postaction={decorate,decoration={markings,mark=at position 0.2
with {\arrow{stealth};}}}] (-6,0,0) -- (0,0,0);
% back
\filldraw[cyan,fill opacity=0.3] (-0.5,-2,-2) -- (-0.5,2,-2) -- (-0.5,2,2)
-- (-0.5,-2,2) -- cycle;
% bottom
\filldraw[cyan,fill opacity=0.3] (0,-2,-2) -- (-0.5,-2,-2) -- (-0.5,2,-2)
-- (0,2,-2) -- cycle;
% right
\filldraw[cyan,fill opacity=0.1] (0,2,-2) -- (-0.5,2,-2) -- (-0.5,2,2)
-- (0,2,2) -- cycle;
% left
\filldraw[cyan,fill opacity=0.3] (0,-2,-2) -- (-0.5,-2,-2) -- (-0.5,-2,2)
-- (0,-2,2) -- cycle;
% top
\filldraw[cyan,fill opacity=0.1] (0,-2,2) -- (-0.5,-2,2) -- (-0.5,2,2)
-- (0,2,2) -- cycle;
%front
\filldraw[cyan,fill opacity=0.1] (0,-2,-2) -- (0,2,-2) -- (0,2,2)
-- (0,-2,2) -- cycle;
% ray in front
\draw[thick,blue] (0,0,0) -- (8,0,0);
\begin{scope}[canvas is yz plane at x=8,xscale=-1,transform shape]
\draw[gray] (-5,0) -- (5,0);
\draw[gray] (0,-4) -- (0,4);
% \draw[red,thick] (1,0) circle (2);
% \draw[red,thick] (-1,0) circle (2);
\node[circle,fill,red,inner sep=2pt] (A) at (2.5,0) {};
\node[circle,fill,red,inner sep=2pt] (B) at (-2.5,0) {};
\draw[gray] (A) circle (4.5pt) (B) circle (4.5pt);
%\draw[gray,shorten >=1.5pt,shorten <=1.5pt] (A)--(B);
\end{scope}
\draw[red] (A) -- (0,0,0) (B) -- (0,0,0);
\end{scope}
\end{tikzpicture}}

\end{document}

enter image description here

And the nowadays mandatory animation can't be missing.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{3d,decorations.markings,calc}
\usepackage{tikz-3dplot}
\makeatletter % https://tex.stackexchange.com/a/48776/121799
\tikzoption{canvas is xy plane at z}[]{%
  \def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}%
  \def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}%
  \def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}%
  \tikz@canvas@is@plane
}
\makeatother

\begin{document}
\foreach \Z in {0,0.075,...,4}  
{\pgfmathsetmacro{\X}{min(\Z,2)}
\pgfmathsetmacro{\Y}{min(4-\Z,2)}
\tdplotsetmaincoords{110}{-50}
\begin{tikzpicture}
%\path[use as bounding box] (-6,-3) rectangle (6,5);
%\node[anchor=north west] at (-6,5) {\X,\xtest,\ytest};
\begin{scope}[tdplot_main_coords]
% ray in the back
\draw [thick,blue,postaction={decorate,decoration={markings,mark=at position 0.2
with {\arrow{stealth};}}}] (-6,0,0) -- (0,0,0);
% back
\filldraw[cyan,fill opacity=0.3] (-0.5,-2,-2) -- (-0.5,2,-2) -- (-0.5,2,2)
-- (-0.5,-2,2) -- cycle;
% bottom
\filldraw[cyan,fill opacity=0.3] (0,-2,-2) -- (-0.5,-2,-2) -- (-0.5,2,-2)
-- (0,2,-2) -- cycle;
% right
\filldraw[cyan,fill opacity=0.1] (0,2,-2) -- (-0.5,2,-2) -- (-0.5,2,2)
-- (0,2,2) -- cycle;
% left
\filldraw[cyan,fill opacity=0.3] (0,-2,-2) -- (-0.5,-2,-2) -- (-0.5,-2,2)
-- (0,-2,2) -- cycle;
% top
\filldraw[cyan,fill opacity=0.1] (0,-2,2) -- (-0.5,-2,2) -- (-0.5,2,2)
-- (0,2,2) -- cycle;
%front
\filldraw[cyan,fill opacity=0.1] (0,-2,-2) -- (0,2,-2) -- (0,2,2)
-- (0,-2,2) -- cycle;
% ray in front
\draw[thick,blue] (0,0,0) -- (8,0,0);
\begin{scope}[canvas is yz plane at x=8,xscale=-1,transform shape]
\draw[gray] (-5,0) -- (5,0);
\draw[gray] (0,-4) -- (0,4);
\draw[red,thick] (\X,0) circle (\Y);
\draw[red,thick] (-\X,0) circle (\Y);
\node[circle,fill,red,inner sep=2pt] (A) at ($(\X,0)+(45:\Y)$) {};
\node[circle,fill,red,inner sep=2pt] (B) at ($(-\X,0)+(-135:\Y)$) {};
\draw[gray] (A) circle (4.5pt) (B) circle (4.5pt);
\draw[gray,shorten >=1.5pt,shorten <=1.5pt] (A)--(B);
\end{scope}
\draw[red] (A) -- (0,0,0) (B) -- (0,0,0);
\end{scope}
\end{tikzpicture}}
\end{document}

enter image description here

6
  • 1
    +1! You don't have to define canvas is xy plane at z, it's defined in the 3d library.
    – Max
    Aug 22, 2018 at 12:06
  • @Max Thanks! I know that it is defined there but the definition is wrong. I am not using this very canvas here but decided to carry the correct definition with me in order to avoid the complications I have experienced here, and to spare others to go through that.
    – user121799
    Aug 22, 2018 at 12:11
  • 2
    Ah I've never encountered that bug! Thanks for the heads-up
    – Max
    Aug 22, 2018 at 12:14
  • 1
    @Sid Is there a \tikzoption? The plane is selected/defined through \begin{scope}[canvas is yz plane at x=8,xscale=-1,transform shape] (xscale=-1 is just for convenience), and the view angles are specified in \tdplotsetmaincoords{110}{-50}. Thanks for updating the title of your nice question!
    – user121799
    Aug 22, 2018 at 13:44
  • 1
    For the record: Jake's patch is now incorporated in v3.1 of TikZ. Jan 15, 2019 at 19:01
1

Here is another quick example adapted from the TikZ manual (Section 40.2 Coordinate Planes, and 40.2.1 Switching to an arbitrary plane).

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{3d}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}%[ ->, plane x={(0.707,-0.707, 0)}, plane y={(0.707,0.707, 1)}, canvas is plane, ]
    \begin{axis}[
        xmin=-3, xmax=3,
        ymin=-3, ymax=3,
        zmin=-3, zmax=3,
        xlabel=$x$,
        ylabel=$y$,
        zlabel=$z$,
        ]

        \draw[fill=blue] (0,0,0) circle(1cm);
        \draw (0,0,0) -- (1,0,0); 
        \draw (0,0,1) -- (0,1,1);
    \end{axis}

    \begin{scope}[plane x={(-1,-1, 0)}, plane y={(1,-1, 1)}, canvas is plane, ]
        \begin{axis}[
            xmin=-3, xmax=3,
            ymin=-3, ymax=3,
            zmin=-3, zmax=3,
            xlabel=$x$,
            ylabel=$y$,
            zlabel=$z$,
            ]
            \draw[fill=green] (0,0.5,0) circle(1.5cm);
            \draw[fill=red] (0,0.5,1) circle(1.5cm);
        \end{axis}
    \end{scope}
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .