我尝试提高我的 Tikz 技能 - 我尝试制作一个简单的图形。我想尝试一下 foreach 函数。但我在排列块方面遇到了一些麻烦。以下是代码:
\documentclass[margin=2cm]{standalone}
\usepackage{tikz}
\definecolor{c11}{HTML}{C1BFC2}
\definecolor{c12}{HTML}{C0BF02}
\definecolor{c13}{HTML}{00C0C1}
\definecolor{c14}{HTML}{02BF01}
\definecolor{c15}{HTML}{BF00C0}
\definecolor{c16}{HTML}{C10001}
\definecolor{c17}{HTML}{0100C2}
\definecolor{c21}{HTML}{0300C3}
\definecolor{c22}{HTML}{141215}
\definecolor{c23}{HTML}{BF06C3}
\definecolor{c24}{HTML}{131418}
\definecolor{c25}{HTML}{00C0BF}
\definecolor{c26}{HTML}{140F13}
\definecolor{c27}{HTML}{C0C1BC}
\definecolor{c31}{HTML}{01204E}
\definecolor{c32}{HTML}{FEFEFE}
\definecolor{c33}{HTML}{34006C}
\definecolor{c34}{HTML}{141215}
\definecolor{c35}{HTML}{120403}
\definecolor{c36}{HTML}{151316}
\definecolor{c37}{HTML}{1E1C1F}
\definecolor{c38}{HTML}{141311}
\begin{document}
\begin{tikzpicture}
\foreach[count=\i] \col in {c11,c12,c13,c14,c15,c16,c17}
\fill[\col] (10,40*\i) rectangle +(10,40);
\foreach[count=\i] \col in {c21,c22,c23,c24,c25,c26,c27}
\fill[\col] (10,5*\i) rectangle +(10,5);
\foreach[count=\i] \col in {c31,c32,c33,c34}
\fill[\col] (12,12*\i) rectangle +(12,12);
\foreach[count=\i] \col in {c35,c36,c37}
\fill[\col] (4,12*\i) rectangle +(4,12);
%the last point [bottom right] is missing
\end{tikzpicture}
\end{document}
它看起来应该是这样的。
亲切的问候!
答案1
只是为了好玩,添加电视机:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\definecolor{c11}{HTML}{C1BFC2}
\definecolor{c12}{HTML}{C0BF02}
\definecolor{c13}{HTML}{00C0C1}
\definecolor{c14}{HTML}{02BF01}
\definecolor{c15}{HTML}{BF00C0}
\definecolor{c16}{HTML}{C10001}
\definecolor{c17}{HTML}{0100C2}
\definecolor{c21}{HTML}{0300C3}
\definecolor{c22}{HTML}{141215}
\definecolor{c23}{HTML}{BF06C3}
\definecolor{c24}{HTML}{131418}
\definecolor{c25}{HTML}{00C0BF}
\definecolor{c26}{HTML}{140F13}
\definecolor{c27}{HTML}{C0C1BC}
\definecolor{c31}{HTML}{01204E}
\definecolor{c32}{HTML}{FEFEFE}
\definecolor{c33}{HTML}{34006C}
\definecolor{c34}{HTML}{141215}
\definecolor{c35}{HTML}{120403}
\definecolor{c36}{HTML}{151316}
\definecolor{c37}{HTML}{1E1C1F}
\definecolor{c38}{HTML}{141311}
\definecolor{DGray}{RGB}{140,138,141}
\definecolor{LGray}{RGB}{229,227,230}
\begin{document}
\begin{tikzpicture}[x=5mm,y=5mm]
\foreach \col in {1,...,7}
{
\fill[c1\col] (10*\col,0) rectangle ++(10,-40);
\fill[c2\col] (10*\col,-40) rectangle ++(10,-5);
\ifnum\col<5\relax
\def\Shift{-1cm}\def\Ancho{12}
\else
\def\Shift{19cm}\def\Ancho{4}
\fi
\fill[c3\col]
([xshift=\Shift]\Ancho*\col,-45) rectangle ++(\Ancho,-12);
}
\fill[black] (70,-45) rectangle ++(10,-12);
\coordinate (ul) at (10,0);
\coordinate (ll) at (10,-57);
\coordinate (ur) at (80,0);
\coordinate (lr) at (80,-57);
\filldraw[draw=black!70,top color=DGray,bottom color=DGray,middle color=LGray]
([xshift=-2.5cm,yshift=2.5cm]ul) --
([xshift=1cm,yshift=-0.5cm]ul) to[out=-93,in=93]
([xshift=1cm,yshift=0.5cm]ll) --
([xshift=-2.5cm,yshift=-2.5cm]ll) --
cycle
([xshift=2.5cm,yshift=2.5cm]ur) --
([xshift=-1cm,yshift=-0.5cm]ur) to[out=-87,in=87]
([xshift=-1cm,yshift=0.5cm]lr) --
([xshift=2.5cm,yshift=-2.5cm]lr) --
cycle;
\filldraw[draw=black!70,left color=DGray,right color=DGray,middle color=LGray]
([xshift=-2.5cm,yshift=2.5cm]ul) --
([xshift=2.5cm,yshift=2.5cm]ur) --
([xshift=-1cm,yshift=-1.5cm]ur) to[out=178,in=2]
([xshift=1cm,yshift=-1.5cm]ul) --
cycle
([xshift=-2.5cm,yshift=-2.5cm]ll) --
([xshift=2.5cm,yshift=-2.5cm]lr) --
([xshift=-1cm,yshift=1.5cm]lr) to[out=182,in=-2]
([xshift=1cm,yshift=1.5cm]ll) --
cycle;
\end{tikzpicture}
\end{document}
结果:
仅限条带的图像:
仅限条带的代码:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\definecolor{c11}{HTML}{C1BFC2}
\definecolor{c12}{HTML}{C0BF02}
\definecolor{c13}{HTML}{00C0C1}
\definecolor{c14}{HTML}{02BF01}
\definecolor{c15}{HTML}{BF00C0}
\definecolor{c16}{HTML}{C10001}
\definecolor{c17}{HTML}{0100C2}
\definecolor{c21}{HTML}{0300C3}
\definecolor{c22}{HTML}{141215}
\definecolor{c23}{HTML}{BF06C3}
\definecolor{c24}{HTML}{131418}
\definecolor{c25}{HTML}{00C0BF}
\definecolor{c26}{HTML}{140F13}
\definecolor{c27}{HTML}{C0C1BC}
\definecolor{c31}{HTML}{01204E}
\definecolor{c32}{HTML}{FEFEFE}
\definecolor{c33}{HTML}{34006C}
\definecolor{c34}{HTML}{141215}
\definecolor{c35}{HTML}{120403}
\definecolor{c36}{HTML}{151316}
\definecolor{c37}{HTML}{1E1C1F}
\definecolor{c38}{HTML}{141311}
\begin{document}
\begin{tikzpicture}
\foreach[count=\i] \col in {c11,c12,c13,c14,c15,c16,c17}
\fill[\col] (10*\i,0) rectangle ++(10,-40);
\foreach[count=\i] \col in {c21,c22,c23,c24,c25,c26,c27}
\fill[\col] (10*\i,-40) rectangle ++(10,-5);
\foreach[count=\i] \col in {c31,c32,c33,c34}
\fill[\col] ([xshift=-2cm]12*\i,-45) rectangle ++(12,-12);
\foreach[count=\i] \col in {c35,c36,c37}
\fill[\col] ([xshift=54cm]4*\i,-45) rectangle ++(4,-12);
\fill[black!90] (70,-45) rectangle ++(10,-12);
\end{tikzpicture}
\end{document}
更新
代码可以进一步简化为仅一个循环:
\begin{tikzpicture}
\foreach \col in {1,...,7}
{
\fill[c1\col] (10*\col,0) rectangle ++(10,-40);
\fill[c2\col] (10*\col,-40) rectangle ++(10,-5);
\ifnum\col<5\relax
\def\Shift{-2cm}\def\Ancho{12}
\else
\def\Shift{38cm}\def\Ancho{4}
\fi
\fill[c3\col]
([xshift=\Shift]\Ancho*\col,-45) rectangle ++(\Ancho,-12);
}
\fill[black] (70,-45) rectangle ++(10,-12);
\end{tikzpicture}
并改变下部条带的宽度,可以得到问题原始图像中的结果:
\begin{tikzpicture}
\foreach \col in {1,...,7}
{
\fill[c1\col] (10*\col,0) rectangle ++(10,-40);
\fill[c2\col] (10*\col,-40) rectangle ++(10,-5);
\ifnum\col<5\relax
\def\Shift{-2.5cm}\def\Ancho{12.5}
\else
\def\Shift{43.35cm}\def\Ancho{10/3}
\fi
\fill[c3\col]
([xshift=\Shift]\Ancho*\col,-45) rectangle ++(\Ancho,-12);
}
\fill[black] (70,-45) rectangle ++(10,-12);
\end{tikzpicture}
评论
循环内的 x 和 y 坐标的顺序错误。
您需要对下部条带进行一些变换。
你制作的图形是上下颠倒的;我改变了 y 坐标,使其处于正确的方向。
答案2
很难看到你的整个图片,但我希望我所做的会有所帮助。问题是你需要根据 \i 而不是 y 来增加 x 坐标。x 坐标应该是 \i*n,其中 n 是矩形的宽度。
\documentclass[margin=2cm]{standalone}
\usepackage{tikz}
\definecolor{c11}{HTML}{C1BFC2}
\definecolor{c12}{HTML}{C0BF02}
\definecolor{c13}{HTML}{00C0C1}
\definecolor{c14}{HTML}{02BF01}
\definecolor{c15}{HTML}{BF00C0}
\definecolor{c16}{HTML}{C10001}
\definecolor{c17}{HTML}{0100C2}
\definecolor{c21}{HTML}{0300C3}
\definecolor{c22}{HTML}{141215}
\definecolor{c23}{HTML}{BF06C3}
\definecolor{c24}{HTML}{131418}
\definecolor{c25}{HTML}{00C0BF}
\definecolor{c26}{HTML}{140F13}
\definecolor{c27}{HTML}{C0C1BC}
\definecolor{c31}{HTML}{01204E}
\definecolor{c32}{HTML}{FEFEFE}
\definecolor{c33}{HTML}{34006C}
\definecolor{c34}{HTML}{141215}
\definecolor{c35}{HTML}{120403}
\definecolor{c36}{HTML}{151316}
\definecolor{c37}{HTML}{1E1C1F}
\definecolor{c38}{HTML}{141311}
\begin{document}
\begin{tikzpicture}
\foreach[count=\i] \col in {c11,c12,c13,c14,c15,c16,c17}
\fill[\col] (\i*2,1) rectangle +(2,8);
\foreach[count=\i] \col in {c21,c22,c23,c24,c25,c26,c27}
\fill[\col] (\i*2,0) rectangle +(2,1);
\end{tikzpicture}
\end{document}