Graphics, Figures & TablesHow to define the center of rotation?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

How to define the center of rotation?

Post by topsquark »

The code is overcomplicated for the question, but I left it to make sure that I'm getting the error "correct."

I've uploading the picture I'm trying to replicate. (It's the one at the top left.) The code I'm posting below can sketch all 7 curves, but I've limited the loop to only sketch the inner loop, for clarity. I've broken it down into a smaller picture that I'm going to rotate about the origin 7 times, but when I rotate the picture, it rotates about the "wrong" point. (The first code below is the picture I'm going to rotate about the point O below it. The second code is what happens when I actually do the rotation.)

To break this down a bit more simply, in a nutshell, the code does this:
[whole bunch of calculations]
\draw (1) arc(...) arc(...);

When I include a rotation
\foreach \w in {0,...,6}{
\begin{scope}[rotate=\w*360/7]
[whole bunch of calculations]
\draw (1) arc(...) arc(...);
\end{scope}
}:

the picture rotates about the point (1). How do I define the center of rotation to be the point (0,0)? I mean, I could do the rotation manually, but the code is already much longer than I had originally thought it would be for so simple an image. And I'm curious to see how it can be done this way.

Thanks!

-Dan

Single curve:

Code: Select all

\documentclass{article}

\usepackage{tikz} 
\usepackage{etoolbox}
\usetikzlibrary{intersections}

\begin{document}

\newcommand\coords[1]{
     \path[transform canvas] (#1);
     \pgfgetlastxy{\xcoord}{\ycoord}
     \pgfmathsetmacro{\Xcoord}{scalar{\xcoord}/28.452756}
     \pgfmathsetmacro{\Ycoord}{scalar{\ycoord}/28.452756}
     \xdef\x{\Xcoord}
     \xdef\y{\Ycoord}
}

\def\n{7}
\def\r{1}
\def\R{6}
\pgfmathsetmacro{\t}{360/\n}
\pgfmathsetmacro{\A}{90-\t/2}
\pgfmathsetmacro{\f}{cot(\t)/2}
\pgfmathsetmacro{\p}{\R*sqrt(2-2*cos(\t))-\r}
\pgfmathsetmacro{\d}{\R*sqrt(2-2*cos(\t)}

\pgfmathsetmacro{\step}{(\p-\r)/(\n-1)}

\begin{tikzpicture}
     \coordinate[label={O}] (O) at (0,0); \fill (O) circle(2pt);
     \coordinate (aI) at (90:\R);;
     \coordinate (aII) at ({90+\t}:\R);
     \coordinate (aIII) at ({90+2*\t}:\R);
     \coordinate (aVII) at ({90+6*\t}:\R);
     \coords{aI}; \pgfmathsetmacro{\aIx}{\x} \pgfmathsetmacro{\aIy}{\y}
     \coords{aII}; \pgfmathsetmacro{\aIIx}{\x} \pgfmathsetmacro{\aIIy}{\y}
     \coords{aIII}; \pgfmathsetmacro{\aIIIx}{\x} \pgfmathsetmacro{\aIIIy}{\y}
     \coords{aVII}; \pgfmathsetmacro{\aVIIx}{\x} \pgfmathsetmacro{\aVIIy}{\y}

     \foreach \w in {0,...,0}{

     \begin{scope}[rotate=\w*\t]
     \foreach \s in {1,...,1}{

     \pgfmathsetmacro{\mI}{\s}
     \pgfmathsetmacro{\mII}{7}
     \pgfmathsetmacro{\mIII}{8-\s}
     \pgfmathsetmacro{\mIV}{7}

     \pgfmathsetmacro{\tempradI}{\r+(\mI-1)*\step}
     \pgfmathsetmacro{\tempradII}{\r+(\mII-1)*\step}
     \pgfmathsetmacro{\tempradIII}{\r+(\mIII-1)*\step}
     \pgfmathsetmacro{\tempradIV}{\r+(\mIV-1)*\step}

     \path[name path=I] (aI) circle(\tempradI);
     \path[name path=II] (aVII) circle(\tempradII);
     \path[name path=III] (aII) circle(\tempradIII);
     \path[name path=IV] (aIII) circle(\tempradIV);

     \path[name intersections={of=I and II,name=i}]; \coordinate (1) at (i-1);
     \path[name intersections={of=I and III,name=i}]; \coordinate (2) at (i-1);
     \path[name intersections={of=III and IV,name=i}]; \coordinate (3) at (i-2);

     \coords{1}; \pgfmathsetmacro{\xI}{\x} \pgfmathsetmacro{\yI}{\y}
     \coords{2}; \pgfmathsetmacro{\xII}{\x} \pgfmathsetmacro{\yII}{\y}
     \coords{3}; \pgfmathsetmacro{\xIII}{\x} \pgfmathsetmacro{\yIII}{\y}

     \pgfmathsetmacro{\slopeI}{-(\xI-\aIx)/(\yI-\aIy))}
     \pgfmathanglebetweenlines{\pgfpoint{-0.5}{0}}{\pgfpoint{0.5}{0}}
                              {\pgfpoint{\xI+0.5}{\slopeI*((\xI+0.5)-\xI)+\yI}}{\pgfpoint{\xI-0.5}{\slopeI*((\xI-0.5)-\xI)+\yI}}
     \ifnum \s=1 \pgfmathsetmacro{\angI}{\pgfmathresult-270}
     \else \pgfmathsetmacro{\angI}{\pgfmathresult-90}
     \fi

     \pgfmathsetmacro{\slopeII}{-(\xII-\aIx)/(\yII-\aIy))}
     \pgfmathanglebetweenlines{\pgfpoint{-0.5}{0}}{\pgfpoint{0.5}{0}}
                             {\pgfpoint{\xII+0.5}{\slopeII*((\xII+0.5)-\xII)+\yII}}{\pgfpoint{\xII-0.5}{\slopeII*((\xII-0.5)-\xII)+\yII}}
     \pgfmathsetmacro{\angII}{\pgfmathresult+90}

     \pgfmathsetmacro{\slopeIIa}{-(\xII-\aIIx)/(\yII-\aIIy))}
     \pgfmathanglebetweenlines{\pgfpoint{-0.5}{0}}{\pgfpoint{0.5}{0}}
                             {\pgfpoint{\xII+0.5}{\slopeIIa*((\xII+0.5)-\xII)+\yII}}{\pgfpoint{\xII-0.5}{\slopeIIa*((\xII-0.5)-\xII)+\yII}}
     \pgfmathsetmacro{\angIIa}{\pgfmathresult+270}

     \pgfmathsetmacro{\slopeIII}{-(\xIII-\aIIx)/(\yIII-\aIIy))}
     \pgfmathanglebetweenlines{\pgfpoint{-0.5}{0}}{\pgfpoint{0.5}{0}}
                             {\pgfpoint{\xIII+0.5}{\slopeIII*((\xIII+0.5)-\xIII)+\yIII}}{\pgfpoint{\xIII-0.5}{\slopeIII*((\xIII-0.5)-\xIII)+\yIII}}
     \pgfmathsetmacro{\angIII}{\pgfmathresult+90}

     \draw[red,thick] (1) arc(\angI:\angII:\tempradI) arc(\angIIa:\angIII:\tempradIII);
     };
     \end{scope}

    };

\end{tikzpicture}

\end{document}
Three rotated copies of the curve:

Code: Select all

\documentclass{article}

\usepackage{tikz} 
\usepackage{etoolbox}
\usetikzlibrary{intersections}

\begin{document}

\newcommand\coords[1]{
     \path[transform canvas] (#1);
     \pgfgetlastxy{\xcoord}{\ycoord}
     \pgfmathsetmacro{\Xcoord}{scalar{\xcoord}/28.452756}
     \pgfmathsetmacro{\Ycoord}{scalar{\ycoord}/28.452756}
     \xdef\x{\Xcoord}
     \xdef\y{\Ycoord}
}

\def\n{7}
\def\r{1}
\def\R{6}
\pgfmathsetmacro{\t}{360/\n}
\pgfmathsetmacro{\A}{90-\t/2}
\pgfmathsetmacro{\f}{cot(\t)/2}
\pgfmathsetmacro{\p}{\R*sqrt(2-2*cos(\t))-\r}
\pgfmathsetmacro{\d}{\R*sqrt(2-2*cos(\t)}

\pgfmathsetmacro{\step}{(\p-\r)/(\n-1)}

\begin{tikzpicture}
     \coordinate[label={O}] (O) at (0,0); \fill (O) circle(2pt);
     \coordinate (aI) at (90:\R);;
     \coordinate (aII) at ({90+\t}:\R);
     \coordinate (aIII) at ({90+2*\t}:\R);
     \coordinate (aVII) at ({90+6*\t}:\R);
     \coords{aI}; \pgfmathsetmacro{\aIx}{\x} \pgfmathsetmacro{\aIy}{\y}
     \coords{aII}; \pgfmathsetmacro{\aIIx}{\x} \pgfmathsetmacro{\aIIy}{\y}
     \coords{aIII}; \pgfmathsetmacro{\aIIIx}{\x} \pgfmathsetmacro{\aIIIy}{\y}
     \coords{aVII}; \pgfmathsetmacro{\aVIIx}{\x} \pgfmathsetmacro{\aVIIy}{\y}

     \foreach \w in {0,...,2}{

     \begin{scope}[rotate=\w*\t]
     \foreach \s in {1,...,1}{

     \pgfmathsetmacro{\mI}{\s}
     \pgfmathsetmacro{\mII}{7}
     \pgfmathsetmacro{\mIII}{8-\s}
     \pgfmathsetmacro{\mIV}{7}

     \pgfmathsetmacro{\tempradI}{\r+(\mI-1)*\step}
     \pgfmathsetmacro{\tempradII}{\r+(\mII-1)*\step}
     \pgfmathsetmacro{\tempradIII}{\r+(\mIII-1)*\step}
     \pgfmathsetmacro{\tempradIV}{\r+(\mIV-1)*\step}

     \path[name path=I] (aI) circle(\tempradI);
     \path[name path=II] (aVII) circle(\tempradII);
     \path[name path=III] (aII) circle(\tempradIII);
     \path[name path=IV] (aIII) circle(\tempradIV);

     \path[name intersections={of=I and II,name=i}]; \coordinate (1) at (i-1);
     \path[name intersections={of=I and III,name=i}]; \coordinate (2) at (i-1);
     \path[name intersections={of=III and IV,name=i}]; \coordinate (3) at (i-2);

     \coords{1}; \pgfmathsetmacro{\xI}{\x} \pgfmathsetmacro{\yI}{\y}
     \coords{2}; \pgfmathsetmacro{\xII}{\x} \pgfmathsetmacro{\yII}{\y}
     \coords{3}; \pgfmathsetmacro{\xIII}{\x} \pgfmathsetmacro{\yIII}{\y}

     \pgfmathsetmacro{\slopeI}{-(\xI-\aIx)/(\yI-\aIy))}
     \pgfmathanglebetweenlines{\pgfpoint{-0.5}{0}}{\pgfpoint{0.5}{0}}
                              {\pgfpoint{\xI+0.5}{\slopeI*((\xI+0.5)-\xI)+\yI}}{\pgfpoint{\xI-0.5}{\slopeI*((\xI-0.5)-\xI)+\yI}}
     \ifnum \s=1 \pgfmathsetmacro{\angI}{\pgfmathresult-270}
     \else \pgfmathsetmacro{\angI}{\pgfmathresult-90}
     \fi

     \pgfmathsetmacro{\slopeII}{-(\xII-\aIx)/(\yII-\aIy))}
     \pgfmathanglebetweenlines{\pgfpoint{-0.5}{0}}{\pgfpoint{0.5}{0}}
                             {\pgfpoint{\xII+0.5}{\slopeII*((\xII+0.5)-\xII)+\yII}}{\pgfpoint{\xII-0.5}{\slopeII*((\xII-0.5)-\xII)+\yII}}
     \pgfmathsetmacro{\angII}{\pgfmathresult+90}

     \pgfmathsetmacro{\slopeIIa}{-(\xII-\aIIx)/(\yII-\aIIy))}
     \pgfmathanglebetweenlines{\pgfpoint{-0.5}{0}}{\pgfpoint{0.5}{0}}
                             {\pgfpoint{\xII+0.5}{\slopeIIa*((\xII+0.5)-\xII)+\yII}}{\pgfpoint{\xII-0.5}{\slopeIIa*((\xII-0.5)-\xII)+\yII}}
     \pgfmathsetmacro{\angIIa}{\pgfmathresult+270}

     \pgfmathsetmacro{\slopeIII}{-(\xIII-\aIIx)/(\yIII-\aIIy))}
     \pgfmathanglebetweenlines{\pgfpoint{-0.5}{0}}{\pgfpoint{0.5}{0}}
                             {\pgfpoint{\xIII+0.5}{\slopeIII*((\xIII+0.5)-\xIII)+\yIII}}{\pgfpoint{\xIII-0.5}{\slopeIII*((\xIII-0.5)-\xIII)+\yIII}}
     \pgfmathsetmacro{\angIII}{\pgfmathresult+90}

     \draw[red,thick] (1) arc(\angI:\angII:\tempradI) arc(\angIIa:\angIII:\tempradIII);
     };
     \end{scope}

    };

\end{tikzpicture}

\end{document}
6 pack.jpg
6 pack.jpg (588 KiB) Viewed 7876 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

How to define the center of rotation?

Post by topsquark »

Okay, I found a solution. The command rotate will latch onto the point in the draw command, in this case the point (1). Not only do I need to rotate the diagram (which is already happening), I also need to rotate the point (1), which is "static" in TikZ as it is a named coordinate. To fix this, I can use
[rotate around={angle:point}]
acting on the point name.

So the draw command is now
\draw[rotate=\w*\t] ([rotate around={\w*\t:(0,0)}]1) arc(...) arc(...);

and I don't need to scope it or anything, so I've removed that.

The code below does not produce the entire image. I'm guessing the error is the time it takes to compile: it works fine on LyX, but it does take a couple of moments to draw it. If you want the whole image, copy:
\foreach \w in {0,...,6}{

\foreach \s in {1,...,7}{

-Dan

Code: Select all

\documentclass{article}

\usepackage{tikz} 
\usepackage{etoolbox}
\usetikzlibrary{intersections}

\begin{document}

\newcommand\coords[1]{
     \path[transform canvas] (#1);
     \pgfgetlastxy{\xcoord}{\ycoord}
     \pgfmathsetmacro{\Xcoord}{scalar{\xcoord}/28.452756}
     \pgfmathsetmacro{\Ycoord}{scalar{\ycoord}/28.452756}
     \xdef\x{\Xcoord}
     \xdef\y{\Ycoord}
}

\def\n{7}
\def\r{1}
\def\R{4}
\pgfmathsetmacro{\t}{360/\n}
\pgfmathsetmacro{\A}{90-\t/2}
\pgfmathsetmacro{\f}{cot(\t)/2}
\pgfmathsetmacro{\p}{\R*sqrt(2-2*cos(\t))-\r}
\pgfmathsetmacro{\d}{\R*sqrt(2-2*cos(\t)}

\pgfmathsetmacro{\step}{(\p-\r)/(\n-1)}

\begin{tikzpicture}
     \coordinate (aI) at (90:\R);;
     \coordinate (aII) at ({90+\t}:\R);
     \coordinate (aIII) at ({90+2*\t}:\R);
     \coordinate (aVII) at ({90+6*\t}:\R);
     \coords{aI}; \pgfmathsetmacro{\aIx}{\x} \pgfmathsetmacro{\aIy}{\y}
     \coords{aII}; \pgfmathsetmacro{\aIIx}{\x} \pgfmathsetmacro{\aIIy}{\y}
     \coords{aIII}; \pgfmathsetmacro{\aIIIx}{\x} \pgfmathsetmacro{\aIIIy}{\y}
     \coords{aVII}; \pgfmathsetmacro{\aVIIx}{\x} \pgfmathsetmacro{\aVIIy}{\y}

     \foreach \w in {0,...,2}{

     \foreach \s in {1,...,3}{

     \pgfmathsetmacro{\mI}{\s}
     \pgfmathsetmacro{\mII}{7}
     \pgfmathsetmacro{\mIII}{8-\s}
     \pgfmathsetmacro{\mIV}{7}

     \pgfmathsetmacro{\tempradI}{\r+(\mI-1)*\step}
     \pgfmathsetmacro{\tempradII}{\r+(\mII-1)*\step}
     \pgfmathsetmacro{\tempradIII}{\r+(\mIII-1)*\step}
     \pgfmathsetmacro{\tempradIV}{\r+(\mIV-1)*\step}

     \path[name path=I] (aI) circle(\tempradI);
     \path[name path=II] (aVII) circle(\tempradII);
     \path[name path=III] (aII) circle(\tempradIII);
     \path[name path=IV] (aIII) circle(\tempradIV);

     \path[name intersections={of=I and II,name=i}]; \coordinate (1) at (i-1);
     \path[name intersections={of=I and III,name=i}]; \coordinate (2) at (i-1);
     \path[name intersections={of=III and IV,name=i}]; \coordinate (3) at (i-2);

     \coords{1}; \pgfmathsetmacro{\xI}{\x} \pgfmathsetmacro{\yI}{\y}
     \coords{2}; \pgfmathsetmacro{\xII}{\x} \pgfmathsetmacro{\yII}{\y}
     \coords{3}; \pgfmathsetmacro{\xIII}{\x} \pgfmathsetmacro{\yIII}{\y}

     \pgfmathsetmacro{\slopeI}{-(\xI-\aIx)/(\yI-\aIy))}
     \pgfmathanglebetweenlines{\pgfpoint{-0.5}{0}}{\pgfpoint{0.5}{0}}
                              {\pgfpoint{\xI+0.5}{\slopeI*((\xI+0.5)-\xI)+\yI}}{\pgfpoint{\xI-0.5}{\slopeI*((\xI-0.5)-\xI)+\yI}}
     \ifnum \s=1 \pgfmathsetmacro{\angI}{\pgfmathresult-270}
     \else \pgfmathsetmacro{\angI}{\pgfmathresult-90}
     \fi

     \pgfmathsetmacro{\slopeII}{-(\xII-\aIx)/(\yII-\aIy))}
     \pgfmathanglebetweenlines{\pgfpoint{-0.5}{0}}{\pgfpoint{0.5}{0}}
                             {\pgfpoint{\xII+0.5}{\slopeII*((\xII+0.5)-\xII)+\yII}}{\pgfpoint{\xII-0.5}{\slopeII*((\xII-0.5)-\xII)+\yII}}
     \pgfmathsetmacro{\angII}{\pgfmathresult+90}

     \pgfmathsetmacro{\slopeIIa}{-(\xII-\aIIx)/(\yII-\aIIy))}
     \pgfmathanglebetweenlines{\pgfpoint{-0.5}{0}}{\pgfpoint{0.5}{0}}
                             {\pgfpoint{\xII+0.5}{\slopeIIa*((\xII+0.5)-\xII)+\yII}}{\pgfpoint{\xII-0.5}{\slopeIIa*((\xII-0.5)-\xII)+\yII}}
     \pgfmathsetmacro{\angIIa}{\pgfmathresult+270}

     \pgfmathsetmacro{\slopeIII}{-(\xIII-\aIIx)/(\yIII-\aIIy))}
     \pgfmathanglebetweenlines{\pgfpoint{-0.5}{0}}{\pgfpoint{0.5}{0}}
                             {\pgfpoint{\xIII+0.5}{\slopeIII*((\xIII+0.5)-\xIII)+\yIII}}{\pgfpoint{\xIII-0.5}{\slopeIII*((\xIII-0.5)-\xIII)+\yIII}}
     \pgfmathsetmacro{\angIII}{\pgfmathresult+90}

     \draw[rotate=\t*\w] ([rotate around={\t*\w:(0,0)}]1) arc(\angI:\angII:\tempradI) arc(\angIIa:\angIII:\tempradIII);
     };

    };

\end{tikzpicture}
\end{document}
Post Reply