I'm trying to draw a set of rotated rectangular cuboids around a centre point in Tikz. I've created an example of what I'm trying to achieve in OpenSCAD below
with the code to generate the above figure in OpenSCAD:
for (i = [0:45:360]) {
rotate([0,i,0])
{
translate([2,0,-0.5]){
cube([2,0.5,1]);
}
}
}
The purpose is to draw the ferrites in an Inductive Power Transfer Magnetic Coupler (pad), an example of the diagram I would like to draw is below
I think, the other parts are quite achievable, but the rotated blocks are a challenge. I guess because, as many people have noted, TikZ does not have a 3D engine.
I have had a go at making something like what I want here:
\documentclass[border=7pt]{standalone}
\usepackage{tikz}
\usepackage{tikz-3dplot}
%%%%%%%%%%%
\begin{document}
\tdplotsetmaincoords{-60}{-50}
\begin{tikzpicture}
[
tdplot_main_coords,
]
\def\rounding{0.2pt};
\foreach \i in {10,50,...,360}
{
\begin{scope}[rotate around={\i:(0.4,0,0.0)}]
\pgfmathsetmacro{\cubex}{1}
\pgfmathsetmacro{\cubey}{0.1}
\pgfmathsetmacro{\cubez}{0.1}
% http://tex.stackexchange.com/questions/12020/what-is-the-easiest-way-to-draw-3d-cube-with-tikz
\draw[rounded corners = \rounding,fill=black!70] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw[rounded corners = \rounding,fill=black!70] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw[rounded corners = \rounding,fill=black!70] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{scope}
}
\end{tikzpicture}
\end{document}
which currently looks like this
I had to set the \tdplotsetmaincoords{-60}{-50}
to a negative number, otherwise you see the 'back' of the cuboid.
I don't know, how to make the circle of cuboids lean back into the page, any help would be greatly appreciated. I also don't know, how to rotate the cubes so they look right, at the moment it looks quite odd.
Best Answer
The proper way to rotate things in 3d with
tikz-3dplot
is to use rotated coordinates, for rotations about the z-axis you can use\tdplotsetrotatedcoords{\i}{00}{0}
. Then the cubes which you are using were drawn for specific view angles. In order to make them fully rotatable you need to, depending on the view angles, draw different faces. To this end, I am using a tool that figures out what the projections are:get projections
. This allows one to draw (only) the faces that are closest to the observer (and which is why there are the\ifnum
s that look at the x, y and z projections).I personally would prefer to dial a positive theta and positive lengths, then the completion of the figure is arguably much more straightforward. Then, in order to come closer to the OpenSCAD syntax, I stored the definition of the cuboid in a style (this is certainly not the first post defining a cuboid pic), in which there is an attempt of automatic shading, less sophisticated than what pstricks has to offer and certainly much less less convincing than asymptote. This pic can be used as
and certainly can be made more flexible, if needed (but this flexibility comes with a slightly longer code). Note also that if you want the cuboids to go closer to each other, some care has to be taken in which order they are drawn: you need to draw those which are in be back first.
As usual, the view can be rotated, and the appropriate ordering of the cuboids can be automatized.