TikZ学習帳

筆者がマジでTikZのお勉強をするブログです。

図を傾ける or 回転させる

中心が原点の場合

原点を中心に普通の楕円を描いた。

<script type="text/tikz">
\begin{tikzpicture}
\draw [-stealth](-2,0)--(2,0);
\draw [-stealth](0,-2)--(0,2);
\draw (0,0) circle [x radius=2, y radius=1];
\end{tikzpicture}
</script>

これを回転させたい。

<script type="text/tikz">
\begin{tikzpicture}
\draw [-stealth](-2,0)--(2,0);
\draw [-stealth](0,-2)--(0,2);
\draw (0,0) circle [x radius=2, y radius=1, rotate=30];
\end{tikzpicture}
</script>

オプションにrotate=30を入れると、反時計回りに30°回転できる。

中心が原点でない場合

普通の正方形を描いた。

<script type="text/tikz">
\begin{tikzpicture}
\draw [-stealth](-2,0)--(2,0);
\draw [-stealth](0,-2)--(0,2);
\draw (1,1) rectangle (2,2);
\end{tikzpicture}
</script>

同じように回転させてみると…

<script type="text/tikz">
\begin{tikzpicture}
\draw [-stealth](-2,0)--(2,0);
\draw [-stealth](0,-2)--(0,2);
\draw [rotate=45] (1,1) rectangle (2,2);
\end{tikzpicture}
</script>

どんな時でも回転中心が(0,0)になってしまうようだ。(0,0)ではなく、重心を中心として回したい。どうすれば良いか?

<script type="text/tikz">
\begin{tikzpicture}
\draw [-stealth](-2,0)--(2,0);
\draw [-stealth](0,-2)--(0,2);
\draw [rotate around={45:(1.5,1.5)}] (1,1) rectangle (2,2);
\end{tikzpicture}
</script>

目的の図ができた。[rotate around={45:{1.5,1.5}}]で、座標(1.5,1.5)を中心に45°回転させるという意味になる。