私のtikzpicコードの効率的な代替品を探しています

私のtikzpicコードの効率的な代替品を探しています

繰り返し要素を持つ tikzpicture があります。現在、それらを個別に定義しています。ただし、寸法を制御する 1 つのテンプレート三角形を定義し (例で p1、q1 を介して行うように)、回転を使用して方向を制御するコードが必要です。

(この例は説明目的のみであることに注意してください。他の形状を使用してより複雑なパターンを構築したいので、これ以上のライブラリの使用は避けたいと思います)

私のコード:

\documentclass[tikz]{standalone} 
\usetikzlibrary{positioning,graphicx,calc}

\begin{document}
\begin{tikzpicture}

% Gridline

\coordinate (x1) at (-4,+0);
\coordinate (x2) at (+0,-4);
\coordinate (x3) at (+4,+0);
\coordinate (x4) at (+0,+4);

\coordinate (k) at (+0,+0);
\coordinate (p1) at (+4,+0);  %Triangle variable 
\coordinate (q1) at (+0,+4);  %Triangle variable

%---------------------------------

\coordinate (G1) at ($(x1)+(x2)$);
\coordinate (G2) at ($(x3)+(x4)$);

\draw [step=0.5cm,draw=gray] (G1) grid (G2);
%\draw [fill=yellow,opacity=0.5] ($(x1)+(x4)$)--(G2)--($(x3)+(x2)$)--(G1);

%---------------------------------
\coordinate (a1) at (k);
\coordinate (b1) at (p1);
\coordinate (c1) at (q1);

\coordinate (C1) at ($(k)$);
\coordinate (B1) at ($(C1)+(k)+(c1)$);
\coordinate (A1) at ($(B1)-(b1)$);
\draw [fill=black] (A1)--(B1)--(C1)--cycle;

\coordinate (A2) at ($(C1)$);
\coordinate (B2) at ($(A2)+(k)-(c1)$);
\coordinate (C2) at ($(B2)+(k)+(b1)$);
\draw [fill=red] (A2)--(B2)--(C2)--cycle;

\coordinate (A3) at ($(C1)$);
\coordinate (B3) at ($(A3)+(k)-(b1)$);
\coordinate (C3) at ($(B3)+(k)-(c1)$);
\draw [fill=green] (A3)--(B3)--(C3)--cycle;

\coordinate (A4) at ($(C1)$);
\coordinate (B4) at ($(A4)+(k)+(b1)$);
\coordinate (C4) at ($(B4)+(k)+(c1)$);
\draw [fill=blue] (A4)--(B4)--(C4)--cycle;

%---------------------------------
\draw[white,opacity=1] (current bounding box.south west) rectangle 
        (current bounding box.north east);
\end{tikzpicture}
\end{document}

出力 ここに画像の説明を入力してください

2番目の例

\documentclass[tikz]{standalone} 
\usetikzlibrary{positioning,calc,graphics}

\begin{document}
\begin{tikzpicture}

% Gridline

\coordinate (x1) at (-4,+0);
\coordinate (x2) at (+0,-4);
\coordinate (x3) at (+4,+0);
\coordinate (x4) at (+0,+4);

\coordinate (k) at (+0,+0);
\coordinate (p1) at (+1.75,+0); 
\coordinate (q1) at (+0,+1.75); 
\coordinate (r1) at (+4.5,+0); 
\coordinate (s1) at (+0,+4.5); 

%---------------------------------

\coordinate (G1) at ($(x1)+(x2)$);
\coordinate (G2) at ($(x3)+(x4)$);

\draw [step=0.5cm,draw=none] (G1) grid (G2);
\draw [fill=yellow] ($(x1)+(x4)$)--(G2)--($(x3)+(x2)$)--(G1);

%---------------------------------
\coordinate (a1) at (k);
\coordinate (b1) at (p1);
\coordinate (c1) at (q1);

\coordinate (A1) at ($(x1)+(x4)$);
\coordinate (B1) at ($(A1)+(k)+(b1)$);
\coordinate (C1) at ($(B1)+(k)-(c1)$);
\draw [fill=black] (A1)--(B1)--(C1)--cycle;

\coordinate (b2) at (r1);
\coordinate (c2) at (c1);

\coordinate (A2) at (B1);
\coordinate (B2) at ($(A2)+(k)+(b2)$);
\coordinate (C2) at ($(B2)+(a1)-(c2)$);
\coordinate (D2) at (C1);
\draw [fill=black] (A2)--(B2)--(C2)--(D2)--cycle;

\coordinate (b3) at (b1);
\coordinate (c3) at (c1);

\coordinate (A3) at (B2);
\coordinate (B3) at ($(A3)+(k)+(b3)$);
\coordinate (C3) at ($(B3)+(a1)-(c2)$);
\coordinate (D3) at (C2);
\draw [fill=black] (A3)--(B3)--(C3)--(D3)--cycle;

\coordinate (b4) at (b1);
\coordinate (c4) at (s1);

\coordinate (A4) at (C2);
\coordinate (B4) at ($(A4)+(k)+(b4)$);
\coordinate (C4) at ($(B4)+(k)-(c4)$);
\coordinate (D4) at ($(C4)+(k)-(b4)$);
\draw [fill=black] (A4)--(B4)--(C4)--(D4)--cycle;

\coordinate (A5) at (D4);
\coordinate (B5) at ($(A5)+(k)+(b1)$);
\coordinate (C5) at ($(B5)+(k)-(c1)$);
\draw [fill=black] (A5)--(B5)--(C5)--cycle;

\coordinate (A6) at (C1);
\coordinate (B6) at ($(C1)+(k)-(s1)$);
\coordinate (C6) at (A5);
\draw [fill=black] (A6)--(B6)--(C6)--cycle;

%---------------------------------

\draw[white,opacity=1] (current bounding box.south west) rectangle 
        (current bounding box.north east);

\end{tikzpicture}
\end{document}

出力 ここに画像の説明を入力してください

答え1

これを処理するのに最適なtikzスタイルではないかもしれませんが、例に欠けているライブラリがなくても機能します。

\documentclass[tikz]{standalone} 

\newcommand{\tri}[1]{%
    \draw[#1] (0,0) -- (4,0) -- (4,4) -- cycle;
}

\begin{document}
\begin{tikzpicture}

\tri{fill=black}
\tri{fill=red,rotate=90}
\tri{fill=green,rotate=180}
\tri{fill=blue,rotate=270}

\end{tikzpicture}
\end{document}

答え2

の使用を検討することもできますpics。これらを使用して複雑な tikz 図を定義し、再利用することができます。引数を使用して定義したり、最終的に描画されるときにいくつかのプロパティを変更したりすることも可能です。

次のコードは、図の例を 2 つ示しています。1mytriangleつの頂点を原点とし、塗りつぶしの色と 2 辺の長さという 3 つの引数を持つ正方形の三角形を定義します。2 番目の例は、「複雑な」画像を定義します。

\documentclass[tikz, border=2mm]{standalone} 
\usetikzlibrary{positioning}

\tikzset{
    pics/mytriangle/.style n args={3}{
        code={
            \fill[#1] (0,0)--++(0,#2)--++(#3,0)--cycle;
        }
    },
    myfigure/.pic={
        \fill[black] (0,0) rectangle ++(-1,-1);
        \fill[blue] (-1,0) rectangle ++(-3,-1);
        \fill[green] (-4,0)-- ++(-1,0)--++(1,-1)--cycle;
        \fill[blue] (0,-1) rectangle ++(-1,-3);
        \fill[green] (0,-4)-- ++(-1,0)--++(1,-1)--cycle;
        \fill[red] (-4,-1)-- ++(0,-3)--++(3,0)--cycle;
    }
}

\begin{document}
\begin{tikzpicture}
\foreach \a/\c in {0/red,90/green,180/blue,270/black}
    \pic[rotate=\a] at (0,0) {mytriangle={\c}{2}{2}};

\begin{scope}[xshift=4cm]
\foreach \a/\c in {0/red,90/green,180/blue,270/black}
    \pic[rotate=\a] at (0,0) {mytriangle={\c}{1}{1.8}};
\end{scope}

\begin{scope}[xshift=8cm]
\foreach \a/\c in {0/red,90/green,180/blue,270/black}
    \pic[rotate=\a] at (0,0) {mytriangle={\c}{1.5}{1}};
\end{scope}

\end{tikzpicture}

\begin{tikzpicture}
\foreach \a in {30,120,210,300}
    \pic[rotate=\a] at (\a:-1cm) {myfigure};
\end{tikzpicture}
\end{document}

ここに画像の説明を入力してください

ここに画像の説明を入力してください

答え3

\documentclass[tikz]{standalone} 
\usetikzlibrary{positioning,calc}

\begin{document}
\begin{tikzpicture}


\coordinate (O) at (0,0);
\foreach \X/\Col in{1/red,2/green,3/black,4/blue}
{
\coordinate (x\X) at ({-90*\X}:4);
\draw[fill=\Col] (x\X) -- +({-90*(\X-1)}:4) -- (O) -- cycle;
}

%Gridline
\coordinate (G1) at ($(x1)+(x2)$);
\coordinate (G2) at ($(x3)+(x4)$);
\draw [step=0.5cm,draw=gray] (G1) grid (G2);
\draw[white,opacity=1] (current bounding box.south west) rectangle 
        (current bounding box.north east);
\end{tikzpicture}
\end{document}

関連情報