TeX & TikZ IV
9 正多角形
9.1 正方形(1辺 2 とする)
9.1.1 座標指定
\begin{tikzpicture}
\draw(0,0)coordinate(B)node[below left]{B}
--(2,0)coordinate(C)node[below right]{C}
--(2,2)coordinate(D)node[above right]{D}
--(0,2)coordinate(A)node[above left]{A}
--cycle;
\end{tikzpicture}
出力結果

9.1.2 始点 (外接円の中心) から、++(a,b) を足した座標を結び、 (新たな始点として定義する)
\begin{tikzpicture}
\draw(0,0)coordinate(B)node[below left]{B}
--++(2,0)coordinate(C)node[below right]{C}
--++(0,2)coordinate(D)node[above right]{D}
--++(-2,0)coordinate(A)node[above left]{A}
--cycle;
\end{tikzpicture}
出力結果

9.2 正五角形
9.2.1 始点から、+(角度: 半径) で指定した座標を結び (新たな始 点として定義しない)
\begin{tikzpicture}
\draw(0,0)coordinate(O)
+(18:2)coordinate(E)node[above right]{E}
--+(90:2)coordinate(A)node[above]{A}
--+(162:2)coordinate(B)node[below left]{B}
--+(234:2)coordinate(C)node[below left]{C}
--+(306:2)coordinate(D)node[below right]{D}
--cycle;
\end{tikzpicture}
出力結果

9.2.2 頂点の座標を計算し結ぶ (半径 1 とする)
\begin{tikzpicture}
\tikzmath{
real \ax \ay \bx \by \cx \cy \dx \dy \ex \ey ;
%※ +90 することで、頂点 A が真上に来るようにする
\ax=cos(0+90);
\ay=sin(0+90);
\bx=cos(72+90);
\by=sin(72+90);
\cx=cos(144+90);
\cy=sin(144+90);
\dx=cos(216+90);
\dy=sin(216+90);
\ex=cos(288+90);
\ey=sin(288+90);
}
\draw(\ax,\ay)coordinate(A)node[above]{A}
--(\bx,\by)coordinate(B)node[above left]{B}
--(\cx,\cy)coordinate(C)node[below left]{C}
--(\dx,\dy)coordinate(D)node[below right]{D}
--(\ex,\ey)coordinate(E)node[above right]{E}
--cycle;
\end{tikzpicture}
出力結果

9.3 正 n 角形
9.3.1 正 n 角形:始点 (中心) からの頂点の角度を foreach で計算し、始点 (中心) から、 +(角度: 半径) で指定した座標を結ぶ。
(半径 2 とする)
\begin{tikzpicture}
\newcounter{i}
\setcounter{i}{0}
%頂点に記号を打つ場合
\newcounter{I}
\setcounter{I}{1}
% 多角形の頂点数
\def\n{12} % 任意の n を指定(ここでは 12 角形)
% 多角形の各頂点を計算
\foreach \t in {1, ..., \n} {
\pgfmathsetmacro\theta{360/\n * (\t - 1)+90}
\coordinate (point-\arabic{i}) at (\theta:2);
%頂点に記号を打つ場合(中心から頂点までの距離の1.1倍)
\coordinate(point-\arabic{I}) at (\theta:2.2);
\draw(point-\arabic{I})node{\Alph{I}};
\stepcounter{i}
\stepcounter{I}
}
% n角形を描く
\path[draw] (point-0)
\foreach \x in {1, ..., \the\numexpr \n-1\relax}
{
-- (point-\x)
}
-- cycle;
\end{tikzpicture}
出力結果
