我想画一串交替排列的两种颜色的珠子。整体形状有点像开放的正多边形,但最好在侧面有一些平滑的曲线。多边形的每一边都有相同数量的珠子,其中 k 个红色珠子后面跟着 n 个蓝色珠子。
顶点处的珠子颜色相同。侧面有一些不连续性,以表示开放性。下面的代码是我可以绘制的正方形排列的简单情况,其中每两个红色珠子后面跟着一个蓝色珠子。
\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [gray!66] plot [smooth] coordinates {(-.2,0.1)(.35,-.03)(.65,.03) (1,0) (1.35,-.03)(1.65,0.03) (2,0) (2.35,-0.03) (2.65,0.03) (3,0) (3.4,0)(3.6,0) };
\draw [gray!66] plot [smooth] coordinates { (4.635,0) (5.1,-.023) (5.42,.023) (5.76,-.023) (6.1,0) };
%nodes on the horizontal sides first segment
\draw[fill=blue!66] (0,0.03) circle(3.85pt);
\draw[fill=red!66] (0.35,-.03) circle(3.85pt);
\draw[fill=red!66] (0.65,.03) circle(3.85pt);
\draw[fill=blue!66] (1,0) circle(3.85pt);
\draw[fill=red!66] (1.35,-0.03) circle(3.85pt);
\draw[fill=red!66] (1.65,0.03) circle(3.5pt);
\draw[fill=blue!66] (2,0) circle(3.85pt);
\draw[fill=red!66] (2.36,-0.03) circle(3.85pt);
\draw[fill=red!66] (2.66,0.03) circle(3.85pt);
\draw[fill=blue!66] (3,0) circle(3.85pt);
\draw[fill=red!66] (3.4,0) circle(3.85pt);
%nodes on the horizontal sides second segment
\draw[fill=blue!66] (4.8757,0) circle(3.85pt);
\draw[fill=red!66] (5.219,-.023) circle(3.85pt);
\draw[fill=red!66] (5.69523,.023) circle(3.85pt);
\draw[fill=blue!66] (6.13,-.023) circle(3.85pt);
\draw [gray!66] plot [smooth] coordinates { (6.135,0) (6.135,1) (6.137,2) (6.135,3) (6.135,3.64) };
%nodes on the vertical sides right, first segment
\draw[fill=red!66] (6.135,0.3) circle(3.85pt);
\draw[fill=red!66] (6.135,0.6) circle(3.85pt);
\draw[fill=blue!66] (6.137,.91) circle(3.85pt);
\draw[fill=red!66] (6.137,1.21) circle(3.85pt);
\draw[fill=red!66] (6.137,1.5) circle(3.85pt);
\draw[fill=blue!66] (6.137,1.8) circle(3.85pt);
\draw[fill=red!66] (6.137,2.1) circle(3.85pt);
\draw[fill=red!66] (6.137,2.4) circle(3.85pt);
\draw[fill=blue!66] (6.137,2.7) circle(3.85pt);
\draw[fill=red!66] (6.137,3) circle(3.85pt);
\draw[fill=blue!66] (6.137,3.3) circle(3.85pt);
\draw [gray!66] plot [smooth] coordinates { (6.135,4.257) (6.135,5) (6.137,5.3) (6.135,5.6) (6.135,5.86) };
\draw[fill=blue!66] (6.137,4.7) circle(3.85pt);
\draw[fill=red!66] (6.137,5.1) circle(3.85pt);
\draw[fill=red!66] (6.137,5.52) circle(3.85pt);
\draw[fill=blue!66] (6.137,6) circle(3.85pt);
\draw [gray!66] plot [smooth] coordinates { (6.13,6.03) (6.,6) (5.7,6) (5.4,6) (5.1,6) (4.7,6) (4.4,6) (4,6) (3.7,6) (3.3,6) (2.37,6) };
\draw[fill=blue!66] (6.14,6) circle(3.85pt);
\draw[fill=red!66] (5.7,6) circle(3.85pt);
\draw[fill=red!66] (5.35,6) circle(3.85pt);
\draw[fill=blue!66] (5.,6) circle(3.85pt);
\draw[fill=red!66] (4.695,6) circle(3.85pt);
\draw[fill=red!66] (4.34,6) circle(3.85pt);
\draw[fill=blue!66] (3.96,6) circle(3.85pt);
\draw[fill=red!66] (3.6,6) circle(3.85pt);
\draw[fill=red!66] (3.29,6) circle(3.85pt);
\draw[fill=blue!66] (2.95,6) circle(3.85pt);
\draw[fill=red!66] (2.61,6) circle(3.85pt);
\draw [gray!66] plot [smooth] coordinates { (1.813952,6.03) (1.6012,6) (1.4,6) (1.136,6) (1.13,6) (.57,6) (.24,6) (0,6) };
\draw[fill=blue!66] (1.251791,6) circle(3.85pt);
\draw[fill=red!66] (.813147,6) circle(3.85pt);
\draw[fill=red!66] (.4271904,6) circle(3.85pt);
\draw[fill=blue!66] (0,6) circle(3.85pt);
\draw [gray!66] plot [smooth] coordinates { (0,6.03) (0,5.6) (0,5.3) (0,5) (0,4.6) (0,4) (0,3.4) (.0,2.43) };
\draw[fill=red!66] (0,5.627) circle(3.85pt);
\draw[fill=red!66] (0,5.30748) circle(3.85pt);
\draw[fill=blue!66] (0,4.9748) circle(3.85pt);
\draw[fill=red!66] (0,4.534) circle(3.85pt);
\draw[fill=red!66] (0,4.1) circle(3.85pt);
\draw[fill=blue!66] (0,3.6748) circle(3.85pt);
\draw[fill=red!66] (0,3.24) circle(3.85pt);
\draw[fill=red!66] (0,2.848) circle(3.85pt);
\draw[fill=blue!66] (0,2.52748) circle(3.85pt);
\draw [gray!66] plot [smooth] coordinates { (0,1.8) (0,1.6) (0,1.4) (0,1.2) (0,1) (0,0) };
\draw[fill=red!66] (0,.51848) circle(3.85pt);
\draw[fill=red!66] (0,.851848) circle(3.85pt);
\draw[fill=blue!66] (0,1.2851848) circle(3.85pt);
\end{tikzpicture}
\end{document}
答案1
装饰可能是一个很好的起点:
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.pathreplacing,calc,shapes.geometric,backgrounds}
\def\drawmarker{%
\pgfpointdiff{\pgfpointdecoratedinputsegmentfirst}
{\pgfpointdecoratedinputsegmentlast}
\pgfgetlastxy\x\y\pgfmathveclen{\x}{\y}\let\len=\pgfmathresult
\pgfmathparse{int(floor(\len/(\k+\n)/\sep) * (\k+\n))}\let\m=\pgfmathresult
\pgfmathparse{\len/\m}\let\rsep=\pgfmathresult
\foreach \i [count=\j from -1, evaluate={\a=mod(\i, \k+\n); \b=int(\a >= \k);
\c=int(!(\breakstart > 0 && \i >= \breakstart &&
\breakend > 0 && \i <=\breakend));
\d=int(!(\breakstart > 0 && \i > \breakstart &&
\breakend > 0 && \i <=\breakend));}] in {0,...,\m}{
\ifnum\d=1 \ifnum\i>0
\begin{pgfonlayer}{background}
\path [string/.try]
($(\tikzinputsegmentfirst)!\j*\rsep pt!(\tikzinputsegmentlast)$) --
($(\tikzinputsegmentfirst)!\i*\rsep pt!(\tikzinputsegmentlast)$);
\end{pgfonlayer}
\fi\fi
\ifnum\c=1
\fill [every bead/.try, bead \b/.try]
($(\tikzinputsegmentfirst)!\i*\rsep pt!(\tikzinputsegmentlast)$)
circle [radius=\size/2];
\fi
}
}
\tikzset{beads/.cd,
k/.estore in=\k, n/.estore in=\n, size/.estore in=\size, sep/.estore in=\sep,
break start/.estore in=\breakstart, break end/.estore in=\breakend,
break/.style args={#1-#2}{/tikz/beads/.cd,break start=#1, break end=#2},
k=1, n=2, sep=5pt, size=3pt,
break start=0, break end=0,
/tikz/.cd,
beads/.style={
/tikz/beads/.cd, #1, /tikz/.cd, postaction={
decoration={
reverse path, show path construction,
lineto code=\drawmarker, closepath code=\drawmarker
}, decorate}},
string/.style={draw=gray, thin},
bead 0/.style={fill=blue!66}, bead 1/.style={fill=red!66},
}
\begin{document}
\tikz\foreach \s [count=\q from 1] in {3,...,6}
\node [beads={k=\q, break=3-4}, regular polygon, regular polygon sides=\s, minimum size=3cm]
at (0,-\s*3) {};
\end{document}