2

This is related to my question: using tikz to draw a flattened polyhedron and is similar, but in my interpretation not the same as the question:Rotate a path around a point.

The following MWE shows what I want as an output, but I was hoping there was an easier way, something like: draw the second trapezium by y-shifting it up so that cordinate A1 is at B2 and then rotating so that C2 is at D1. Do such elegant transformations exist? My aim is to then be able to repeat this many times to get an arch of such trapeziums.

\documentclass{article}

\usepackage{tikz}

\newcommand{\trapezium}[1]{%
\coordinate[label=A#1] (A#1) at (0,0);
\path (A#1)--++(-90:2) coordinate[label=B#1] (B#1);
\path (B#1)--++({-10}:1) coordinate[label=C#1] (C#1);
\path (A#1)--++({10}:1) coordinate[label=D#1] (D#1);
\draw (A#1)--(B#1)--(C#1)--(D#1)--cycle;
}


\begin{document}

\begin{tikzpicture}
\trapezium{1}

\begin{scope}[yshift=57,rotate around={20:(A1)}]
\trapezium{2}
\end{scope}

\end{tikzpicture}

\end{document}
4
  • 2
    Does the answer to this question help you?
    – Thruston
    Jan 28, 2014 at 17:42
  • @Thruston Thanks for the tip! There was an answer there that does an explicitly shift and rotation: what I'd like is a shift relative to a coordinate. Otherwise I end up doing all the calculations first!
    – Geoff
    Jan 28, 2014 at 17:58
  • You could make your life easier by making the origin the centre of the arch. I don't think you can define a transformation for Tikz by saying "point A = Point B transformed by T" the way you can in Metapost but others will correct me if I'm wrong.
    – Thruston
    Jan 28, 2014 at 18:07
  • @Thruston I dug a little deeper into one of the solutions from your tip and have something that works a treat! I will post it as an answer.
    – Geoff
    Jan 28, 2014 at 18:12

2 Answers 2

2

I followed Thruston's tip (the question being: Transform defined coordinates in TikZ) and found an interesting solution from Mark Wibrow. The code below borrows heavily from there.

\documentclass{article}
\usepackage{tikz}

\makeatletter
\newif\iftikztransformnodecoordinates
\tikzset{transform node coordinates/.is if=tikztransformnodecoordinates}


\def\tikz@parse@node#1(#2){%
    \pgfutil@in@.{#2}%
    \ifpgfutil@in@
        \tikz@calc@anchor#2\tikz@stop%
    \else%
        \tikz@calc@anchor#2.center\tikz@stop%
        \expandafter\ifx\csname pgf@sh@ns@#2\endcsname\tikz@coordinate@text%
        \else
            \tikz@shapebordertrue%
            \def\tikz@shapeborder@name{#2}%
        \fi%
    \fi%
    \iftikztransformnodecoordinates%
        \pgf@pos@transform{\pgf@x}{\pgf@y}%
    \fi
    \edef\tikz@marshal{\noexpand#1{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}}%
    \tikz@marshal%
}

\begin{document}

\begin{tikzpicture}

\coordinate (A1) at (0,0);
\path (A1)--++(-90:2) coordinate (B1);
\path (B1)--++({-10}:1) coordinate (C1);
\path (A1)--++({10}:1) coordinate (D1);

\draw (A1)--(B1)--(C1)--(D1)--cycle;

\foreach \n/\m in {1/2,2/3,3/4,4/5,5/6}{%
\draw [transform node coordinates, rotate=20, shift={(0,2)}] 
(A\n) coordinate (A\m)-- (B\n) coordinate (B\m) -- (C\n) coordinate (C\m) -- (D\n) coordinate (D\m) -- cycle;
}


\end{tikzpicture}

\end{document}

enter image description here

0
1

Your code was not far away from a easy solution.

I've changed trapezium's origin to bottom left corner (B1). If you want to draw another one on top of it, shift its origin to A1 and rotate it proportionally to trapezium slope (in your case 20 degrees for every new trapezium).

The code used to clip an image with trapezium border has been borrowed from JLDiaz answer to TikZ: Drawing regular hexagons with pictures inside

\begin{scope}[shift={(A1)}, rotate=20]
\trapezium{2};
\end{scope}


\documentclass[tikz]{standalone}

\usepackage{mwe}

\newcommand{\trapezium}[1]
{
    \path (0,0) coordinate[label=B#1] (B#1) --++(90:2) coordinate[label=A#1] (A#1);
    \path (B#1)--++({-10}:1) coordinate[label=C#1] (C#1);
    \path (A#1)--++({10}:1) coordinate[label=D#1] (D#1);
    \draw (A#1)--(B#1)--(C#1)--(D#1)--cycle;
    \begin{scope}
        \clip (A#1)--(B#1)--(C#1)--(D#1)--cycle;
        \node{\includegraphics{image-A}};
    \end{scope}
}

\begin{document}

\begin{tikzpicture}
   \trapezium{1}
   \begin{scope}[shift={(A1)}, rotate=20]
      \trapezium{2};
   \end{scope}
   \begin{scope}[shift={(A2)}, rotate=40]
      \trapezium{3};
   \end{scope}
   \begin{scope}[shift={(A3)}, rotate=60]
      \trapezium{4};
   \end{scope}
\end{tikzpicture}
\end{document}

Just for fun I've also tested with a TiKZ 3 pic

\documentclass[tikz]{standalone}

\usepackage{mwe}

\tikzset{
    pics/trapezium/.style={
       code = {
        \path (0,0) coordinate[label=B#1] (B#1)--++(90:2) coordinate[label=A#1] (A#1);
        \path (B#1)--++({-10}:1) coordinate[label=C#1] (C#1);
        \path (A#1)--++({10}:1) coordinate[label=D#1] (D#1);
        \draw[clip] (A#1)--(B#1)--(C#1)--(D#1)--cycle;
        \node{\includegraphics{image-A}};
        }
    }
}


\begin{document}

\begin{tikzpicture}
\path pic[rotate=0] {trapezium=1};
\path (A1) pic[rotate=20] {trapezium=2};
\path (A2) pic[rotate=40] {trapezium=3};
\path (A3) pic[rotate=60] {trapezium=3};
\end{tikzpicture}

\end{document}

Both codes produce:

enter image description here

You must log in to answer this question.

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