答案1
下面定义了一个pic
。您可以通过设置来控制行数CP num
,通过设置来控制颜色CP color 1
,CP color 2
通过设置来控制大小CP radius
。
金字塔是从上到下绘制的,因此,最底下的一行的颜色不是固定的,而是顶行的颜色。
\documentclass[tikz,border=3.14]{standalone}
\makeatletter
\newcommand*\CP@colorA{}
\newcommand*\CP@colorB{}
\newcommand*\CP@radius{}
\newcommand*\CP@tmp{}
\tikzset
{
circle pyramid/.pic =
{
\begin{scope}
\pgfkeysgetvalue{/tikz/CP color 1}\CP@colorA
\pgfkeysgetvalue{/tikz/CP color 2}\CP@colorB
\pgfkeysgetvalue{/tikz/CP radius}\CP@radius
\foreach \CP@x in {1,...,\pgfkeysvalueof{/tikz/CP num}}
{
\draw[fill=\CP@colorA]
\ifnum\CP@x=1
(0,0)
\else
(circle@pyramid@lastrow) ++(240:2*\CP@radius)
\fi
coordinate(circle@pyramid@lastrow)
\foreach \CP@y in {1,...,\CP@x}
{
circle[radius=\CP@radius]
\unless\ifnum\CP@x=\CP@y
++(2*\CP@radius,0)
\fi
}
;
\let\CP@tmp\CP@colorA
\global\let\CP@colorA\CP@colorB
\global\let\CP@colorB\CP@tmp
}
\end{scope}
}
,CP color 1/.initial=red
,CP color 2/.initial=green
,CP radius/.initial=5mm
,CP num/.initial=2
}
\makeatother
\begin{document}
\begin{tikzpicture}
\path
(0,0) pic[CP num=5]{circle pyramid}
(7,0) pic[CP num=2]{circle pyramid}
;
\end{tikzpicture}
\end{document}
一种从最底下一行向上至顶部构建金字塔的变体。
\documentclass[tikz,border=3.14]{standalone}
\usetikzlibrary{calc}
\makeatletter
\newcommand*\CP@colorA{}
\newcommand*\CP@colorB{}
\newcommand*\CP@radius{}
\newcommand*\CP@tmp{}
\newcommand*\CP@num{}
\tikzset
{
circle pyramid/.pic =
{
\begin{scope}
\pgfkeysgetvalue{/tikz/CP color 1}\CP@colorA
\pgfkeysgetvalue{/tikz/CP color 2}\CP@colorB
\pgfkeysgetvalue{/tikz/CP radius}\CP@radius
\pgfkeysgetvalue{/tikz/CP num}\CP@num
\foreach \CP@x in {\CP@num,...,1}
{
\draw[fill=\CP@colorA]
\ifnum\CP@x=\CP@num
(0,0)
\else
(circle@pyramid@lastrow) ++(60:2*\CP@radius)
\fi
coordinate(circle@pyramid@lastrow)
\foreach \CP@y in {1,...,\CP@x}
{
circle[radius=\CP@radius]
\unless\ifnum\CP@x=\CP@y
++(2*\CP@radius,0)
\fi
}
;
\let\CP@tmp\CP@colorA
\global\let\CP@colorA\CP@colorB
\global\let\CP@colorB\CP@tmp
}
\end{scope}
}
,CP color 1/.initial=red
,CP color 2/.initial=green
,CP radius/.initial=5mm
,CP num/.initial=2
}
\makeatother
\begin{document}
\begin{tikzpicture}
\path
(0,0) pic[CP num=5]{circle pyramid}
(7,0) pic[CP num=2]{circle pyramid}
;
\end{tikzpicture}
\end{document}
答案2
只是为了好玩(没有 MWE 的用户)用 Mathcha 绘制了这幅画。
\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
%uncomment if require: \path (0,300); %set diagram left start at 0, and has height of 300
%Shape: Circle [id:dp45360336507060484]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (8,177) .. controls (8,165.95) and (16.95,157) .. (28,157) .. controls (39.05,157) and (48,165.95) .. (48,177) .. controls (48,188.05) and (39.05,197) .. (28,197) .. controls (16.95,197) and (8,188.05) .. (8,177) -- cycle ;
%Shape: Circle [id:dp252507625657582]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (48,177) .. controls (48,165.95) and (56.95,157) .. (68,157) .. controls (79.05,157) and (88,165.95) .. (88,177) .. controls (88,188.05) and (79.05,197) .. (68,197) .. controls (56.95,197) and (48,188.05) .. (48,177) -- cycle ;
%Shape: Circle [id:dp04721782504962024]
\draw [fill={rgb, 255:red, 65; green, 117; blue, 5 } ,fill opacity=1 ] (28,141) .. controls (28,129.95) and (36.95,121) .. (48,121) .. controls (59.05,121) and (68,129.95) .. (68,141) .. controls (68,152.05) and (59.05,161) .. (48,161) .. controls (36.95,161) and (28,152.05) .. (28,141) -- cycle ;
%Shape: Circle [id:dp0046652885895916985]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (145,178) .. controls (145,166.95) and (153.95,158) .. (165,158) .. controls (176.05,158) and (185,166.95) .. (185,178) .. controls (185,189.05) and (176.05,198) .. (165,198) .. controls (153.95,198) and (145,189.05) .. (145,178) -- cycle ;
%Shape: Circle [id:dp4267093059670499]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (105,178) .. controls (105,166.95) and (113.95,158) .. (125,158) .. controls (136.05,158) and (145,166.95) .. (145,178) .. controls (145,189.05) and (136.05,198) .. (125,198) .. controls (113.95,198) and (105,189.05) .. (105,178) -- cycle ;
%Shape: Circle [id:dp7193004043829605]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (185,178) .. controls (185,166.95) and (193.95,158) .. (205,158) .. controls (216.05,158) and (225,166.95) .. (225,178) .. controls (225,189.05) and (216.05,198) .. (205,198) .. controls (193.95,198) and (185,189.05) .. (185,178) -- cycle ;
%Shape: Circle [id:dp19724618901819002]
\draw [fill={rgb, 255:red, 65; green, 117; blue, 5 } ,fill opacity=1 ] (125,142) .. controls (125,130.95) and (133.95,122) .. (145,122) .. controls (156.05,122) and (165,130.95) .. (165,142) .. controls (165,153.05) and (156.05,162) .. (145,162) .. controls (133.95,162) and (125,153.05) .. (125,142) -- cycle ;
%Shape: Circle [id:dp44803599900304203]
\draw [fill={rgb, 255:red, 65; green, 117; blue, 5 } ,fill opacity=1 ] (165,142) .. controls (165,130.95) and (173.95,122) .. (185,122) .. controls (196.05,122) and (205,130.95) .. (205,142) .. controls (205,153.05) and (196.05,162) .. (185,162) .. controls (173.95,162) and (165,153.05) .. (165,142) -- cycle ;
%Shape: Circle [id:dp30141546455738966]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (145,106) .. controls (145,94.95) and (153.95,86) .. (165,86) .. controls (176.05,86) and (185,94.95) .. (185,106) .. controls (185,117.05) and (176.05,126) .. (165,126) .. controls (153.95,126) and (145,117.05) .. (145,106) -- cycle ;
%Shape: Circle [id:dp47812251948872886]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (243,178) .. controls (243,166.95) and (251.95,158) .. (263,158) .. controls (274.05,158) and (283,166.95) .. (283,178) .. controls (283,189.05) and (274.05,198) .. (263,198) .. controls (251.95,198) and (243,189.05) .. (243,178) -- cycle ;
%Shape: Circle [id:dp4281409323452088]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (429,179) .. controls (429,167.95) and (437.95,159) .. (449,159) .. controls (460.05,159) and (469,167.95) .. (469,179) .. controls (469,190.05) and (460.05,199) .. (449,199) .. controls (437.95,199) and (429,190.05) .. (429,179) -- cycle ;
%Shape: Circle [id:dp11250558414625078]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (323,178) .. controls (323,166.95) and (331.95,158) .. (343,158) .. controls (354.05,158) and (363,166.95) .. (363,178) .. controls (363,189.05) and (354.05,198) .. (343,198) .. controls (331.95,198) and (323,189.05) .. (323,178) -- cycle ;
%Shape: Circle [id:dp7098878000178785]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (283,178) .. controls (283,166.95) and (291.95,158) .. (303,158) .. controls (314.05,158) and (323,166.95) .. (323,178) .. controls (323,189.05) and (314.05,198) .. (303,198) .. controls (291.95,198) and (283,189.05) .. (283,178) -- cycle ;
%Shape: Circle [id:dp7096872534953822]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (323,108) .. controls (323,96.95) and (331.95,88) .. (343,88) .. controls (354.05,88) and (363,96.95) .. (363,108) .. controls (363,119.05) and (354.05,128) .. (343,128) .. controls (331.95,128) and (323,119.05) .. (323,108) -- cycle ;
%Shape: Circle [id:dp3296115740950498]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (283,108) .. controls (283,96.95) and (291.95,88) .. (303,88) .. controls (314.05,88) and (323,96.95) .. (323,108) .. controls (323,119.05) and (314.05,128) .. (303,128) .. controls (291.95,128) and (283,119.05) .. (283,108) -- cycle ;
%Shape: Circle [id:dp6396123614850209]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (469,179) .. controls (469,167.95) and (477.95,159) .. (489,159) .. controls (500.05,159) and (509,167.95) .. (509,179) .. controls (509,190.05) and (500.05,199) .. (489,199) .. controls (477.95,199) and (469,190.05) .. (469,179) -- cycle ;
%Shape: Circle [id:dp033733994665388733]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (363,178) .. controls (363,166.95) and (371.95,158) .. (383,158) .. controls (394.05,158) and (403,166.95) .. (403,178) .. controls (403,189.05) and (394.05,198) .. (383,198) .. controls (371.95,198) and (363,189.05) .. (363,178) -- cycle ;
%Shape: Circle [id:dp8098933625450455]
\draw [fill={rgb, 255:red, 65; green, 117; blue, 5 } ,fill opacity=1 ] (263,143) .. controls (263,131.95) and (271.95,123) .. (283,123) .. controls (294.05,123) and (303,131.95) .. (303,143) .. controls (303,154.05) and (294.05,163) .. (283,163) .. controls (271.95,163) and (263,154.05) .. (263,143) -- cycle ;
%Shape: Circle [id:dp712692864836656]
\draw [fill={rgb, 255:red, 65; green, 117; blue, 5 } ,fill opacity=1 ] (303,143) .. controls (303,131.95) and (311.95,123) .. (323,123) .. controls (334.05,123) and (343,131.95) .. (343,143) .. controls (343,154.05) and (334.05,163) .. (323,163) .. controls (311.95,163) and (303,154.05) .. (303,143) -- cycle ;
%Shape: Circle [id:dp23991884133896924]
\draw [fill={rgb, 255:red, 65; green, 117; blue, 5 } ,fill opacity=1 ] (343,143) .. controls (343,131.95) and (351.95,123) .. (363,123) .. controls (374.05,123) and (383,131.95) .. (383,143) .. controls (383,154.05) and (374.05,163) .. (363,163) .. controls (351.95,163) and (343,154.05) .. (343,143) -- cycle ;
%Shape: Circle [id:dp807582714276613]
\draw [fill={rgb, 255:red, 65; green, 117; blue, 5 } ,fill opacity=1 ] (303,73) .. controls (303,61.95) and (311.95,53) .. (323,53) .. controls (334.05,53) and (343,61.95) .. (343,73) .. controls (343,84.05) and (334.05,93) .. (323,93) .. controls (311.95,93) and (303,84.05) .. (303,73) -- cycle ;
%Shape: Circle [id:dp7411890564537515]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (509,179) .. controls (509,167.95) and (517.95,159) .. (529,159) .. controls (540.05,159) and (549,167.95) .. (549,179) .. controls (549,190.05) and (540.05,199) .. (529,199) .. controls (517.95,199) and (509,190.05) .. (509,179) -- cycle ;
%Shape: Circle [id:dp5831222438428452]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (549,179) .. controls (549,167.95) and (557.95,159) .. (569,159) .. controls (580.05,159) and (589,167.95) .. (589,179) .. controls (589,190.05) and (580.05,199) .. (569,199) .. controls (557.95,199) and (549,190.05) .. (549,179) -- cycle ;
%Shape: Circle [id:dp48334519546908683]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (589,179) .. controls (589,167.95) and (597.95,159) .. (609,159) .. controls (620.05,159) and (629,167.95) .. (629,179) .. controls (629,190.05) and (620.05,199) .. (609,199) .. controls (597.95,199) and (589,190.05) .. (589,179) -- cycle ;
%Shape: Circle [id:dp8595438447895325]
\draw [fill={rgb, 255:red, 65; green, 117; blue, 5 } ,fill opacity=1 ] (449,144) .. controls (449,132.95) and (457.95,124) .. (469,124) .. controls (480.05,124) and (489,132.95) .. (489,144) .. controls (489,155.05) and (480.05,164) .. (469,164) .. controls (457.95,164) and (449,155.05) .. (449,144) -- cycle ;
%Shape: Circle [id:dp9426603054606959]
\draw [fill={rgb, 255:red, 65; green, 117; blue, 5 } ,fill opacity=1 ] (489,144) .. controls (489,132.95) and (497.95,124) .. (509,124) .. controls (520.05,124) and (529,132.95) .. (529,144) .. controls (529,155.05) and (520.05,164) .. (509,164) .. controls (497.95,164) and (489,155.05) .. (489,144) -- cycle ;
%Shape: Circle [id:dp17134780824661888]
\draw [fill={rgb, 255:red, 65; green, 117; blue, 5 } ,fill opacity=1 ] (529,144) .. controls (529,132.95) and (537.95,124) .. (549,124) .. controls (560.05,124) and (569,132.95) .. (569,144) .. controls (569,155.05) and (560.05,164) .. (549,164) .. controls (537.95,164) and (529,155.05) .. (529,144) -- cycle ;
%Shape: Circle [id:dp5396973396584686]
\draw [fill={rgb, 255:red, 65; green, 117; blue, 5 } ,fill opacity=1 ] (569,144) .. controls (569,132.95) and (577.95,124) .. (589,124) .. controls (600.05,124) and (609,132.95) .. (609,144) .. controls (609,155.05) and (600.05,164) .. (589,164) .. controls (577.95,164) and (569,155.05) .. (569,144) -- cycle ;
%Shape: Circle [id:dp43202147658512446]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (470,109) .. controls (470,97.95) and (478.95,89) .. (490,89) .. controls (501.05,89) and (510,97.95) .. (510,109) .. controls (510,120.05) and (501.05,129) .. (490,129) .. controls (478.95,129) and (470,120.05) .. (470,109) -- cycle ;
%Shape: Circle [id:dp43841861980416597]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (510,109) .. controls (510,97.95) and (518.95,89) .. (530,89) .. controls (541.05,89) and (550,97.95) .. (550,109) .. controls (550,120.05) and (541.05,129) .. (530,129) .. controls (518.95,129) and (510,120.05) .. (510,109) -- cycle ;
%Shape: Circle [id:dp19855407971187278]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (550,109) .. controls (550,97.95) and (558.95,89) .. (570,89) .. controls (581.05,89) and (590,97.95) .. (590,109) .. controls (590,120.05) and (581.05,129) .. (570,129) .. controls (558.95,129) and (550,120.05) .. (550,109) -- cycle ;
%Shape: Circle [id:dp43806161472499117]
\draw [fill={rgb, 255:red, 65; green, 117; blue, 5 } ,fill opacity=1 ] (490,74) .. controls (490,62.95) and (498.95,54) .. (510,54) .. controls (521.05,54) and (530,62.95) .. (530,74) .. controls (530,85.05) and (521.05,94) .. (510,94) .. controls (498.95,94) and (490,85.05) .. (490,74) -- cycle ;
%Shape: Circle [id:dp956288975204461]
\draw [fill={rgb, 255:red, 65; green, 117; blue, 5 } ,fill opacity=1 ] (530,74) .. controls (530,62.95) and (538.95,54) .. (550,54) .. controls (561.05,54) and (570,62.95) .. (570,74) .. controls (570,85.05) and (561.05,94) .. (550,94) .. controls (538.95,94) and (530,85.05) .. (530,74) -- cycle ;
%Shape: Circle [id:dp08056788384810254]
\draw [fill={rgb, 255:red, 208; green, 2; blue, 27 } ,fill opacity=1 ] (510,39) .. controls (510,27.95) and (518.95,19) .. (530,19) .. controls (541.05,19) and (550,27.95) .. (550,39) .. controls (550,50.05) and (541.05,59) .. (530,59) .. controls (518.95,59) and (510,50.05) .. (510,39) -- cycle ;
\end{tikzpicture}
\caption{Pyramids of circles.}
\end{figure}
\end{document}