3

I wanted to draw a rectangle parallel to a radial direction and I achieved this, but I would like to know if there is a more elegant way to do it.

Here is my MWE:

\documentclass[border = 0.5cm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
  \usetikzlibrary{calc, arrows, shapes, decorations.pathmorphing, arrows.meta}

\begin{document}
\begin{tikzpicture}
\draw[help lines,step=1.0,green] (-10,-7) grid (7,7); % help grid

\coordinate (A) at (210:6cm);
\draw[fill=black!50!white] (210:6cm) -- ($(A) +(120:1cm)$) -- ($(A)+(120:1cm)+(210:3cm)$) -- ($(A)+(120:1cm)+(210:3cm)+(300:2cm)$) -- ($(A)+(120:1cm)+(210:3cm)+(300:2cm)+(30:3cm)$) -- ($(A)+(120:1cm)+(210:3cm)+(300:2cm)+(30:3cm)+(120:1cm)$) -- cycle;

\draw[cyan] (0:0) -- (210:11cm); % help radial axe
\end{tikzpicture}
\end{document}
4

You're using six segments to draw a rectangle, so obviously you can shorten it at least, by using only the corners. Another way to make the code shorter is to use relative coordinates, i.e. add ++ before the coordinate.

\documentclass[border = 0.5cm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
  \usetikzlibrary{calc, arrows, shapes, decorations.pathmorphing, arrows.meta}

\begin{document}
\begin{tikzpicture}
\draw[help lines,step=1.0,green] (-10,-7) grid (7,7); % help grid

\coordinate (A) at (210:6cm);


\draw[fill=black!50!white] ($(A) +(120:1cm)$) -- ++(210:3cm) -- ++(300:2cm) -- ++(30:3cm) -- cycle;


\draw[cyan] (0:0) -- (210:11cm); % help radial axe
\end{tikzpicture}
\end{document}

Another possible improvement is to make all the drawing angles relative to a predefined angle. Below I use declare function={a=210;} to make a new constant, and then use e.g. a+90 in the polar coordinates of the relative coordinates:

\documentclass[border = 0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[
  declare function={a=210;}
]

\draw[help lines,step=1.0,green] (-10,-7) grid (7,7); % help grid

\coordinate (A) at (210:6cm);


\draw[fill=black!50!white] ($(A) +(a-90:1cm)$) -- ++(a:3cm) -- ++(a+90:2cm) -- ++(a+180:3cm) -- cycle;


\draw[cyan] (0:0) -- (210:11cm); % help radial axe
\end{tikzpicture}
\end{document}
  • Thanks ! (++) is exactly the trick I was mising and looking for – DRi Jun 20 '18 at 16:29
5

Yes. You can just rotate the rectangle. In this example, the around is not necessary because you rotate around the origin but in general it is. So you just need to put

\draw[fill=black!50!white,rotate around={210:(0,0)}] (6,-1) rectangle (9,1);

or in this example

\draw[fill=black!50!white,rotate=210] (6,-1) rectangle (9,1);

to obtain

enter image description here

\documentclass[border = 0.5cm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
  \usetikzlibrary{calc, arrows, shapes, decorations.pathmorphing, arrows.meta}

\begin{document}
\begin{tikzpicture}
\draw[help lines,step=1.0,green] (-10,-7) grid (7,7); % help grid

\coordinate (A) at (210:6cm);
%\draw[fill=black!50!white] (210:6cm) -- ($(A) +(120:1cm)$) -- ($(A)+(120:1cm)+(210:3cm)$) -- ($(A)+(120:1cm)+(210:3cm)+(300:2cm)$) -- ($(A)+(120:1cm)+(210:3cm)+(300:2cm)+(30:3cm)$) -- ($(A)+(120:1cm)+(210:3cm)+(300:2cm)+(30:3cm)+(120:1cm)$) -- cycle;

\draw[fill=black!50!white,rotate around={210:(0,0)}] (6,-1) rectangle (9,1);

\draw[cyan] (0:0) -- (210:11cm); % help radial axe

\end{tikzpicture}
\end{document}
3

Another solution could be a node at end of path and sloped.

This option sloped forces a node to be drawn tangent to the path. If you draw a path (0:0) -- (210:6cm) that ends where you want the node, option at end (or pos=1) and correct anchor will asure its position. And allow upside down options doesn't change node orientation to keep its contents upside-down, this way the west (or east) anchor can always be used as reference.

\documentclass[border = 0.5cm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
  \usetikzlibrary{calc, arrows, shapes, decorations.pathmorphing, arrows.meta}

\begin{document}
\begin{tikzpicture}[
    mynode/.style={rectangle, 
        minimum width=3cm, 
        minimum height=2cm, 
        draw=black, 
        fill=black!50, 
        sloped, 
        allow upside down, 
        anchor=west}]

\draw[help lines,step=1.0,green] (-10,-7) grid (7,7); % help grid

\foreach \i in {0,30,...,330}
    \draw[cyan] (0:0) -- (\i:6cm) node[mynode, at end]{}; % help radial axe

\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.