
這個問題與https://math.stackexchange.com/questions/3449433/selecting-cards-to-form-a-fair-game?noredirect=1#comment7090962_3449433。我試著直觀地說明如何從一堆N+M
球中抽出 3 個球,其中N
是藍色的數量,M
是綠色的數量。
我在 tikz 中嘗試這樣做可以在下面看到。在這裡,我首先按順序排列球,然後展示當一個球被移除時它們的樣子。兩個球之間的線表示選擇了這兩個球加上虛線球。
不過,我想做一些改變。
- 所以我們選了 3 個球,兩個由線加上虛線連接。如果這三個球具有相同的顏色,我希望將它們的線連接為 X 樣式(虛線、紅色等)
- 如果三個球有不同的顏色,我希望連接它們的線採用 Y 樣式(實線、橙色等)
- 為什麼我的程式碼在特定情況下會失敗
2
? - 有沒有辦法讓程式碼適用於不同數量的藍色和綠色球?
- 如何選擇中心多邊形的旋轉,使其與外部多邊形的角相匹配?現在我將內部的改為手動安裝。
下面的程式碼不起作用有什麼特殊原因嗎?
\foreach \x in {1,...,\n}{ \begin{scope}[shift={(b.corner \x)}] \missingPentagonTemp{2}{c}; \end{scope} }
這就是我想要的樣子(由油漆提供)。
微量元素
\documentclass[a4paper,11pt,margin=5pt]{standalone}
\usepackage{tikz,xcolor}
\usetikzlibrary{arrows}
\usetikzlibrary{trees}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{decorations}
\usepackage{ifthenx}
\newcommand{\missingPentagon}[2]{
% draw edges
\node[draw=none, minimum size=3cm, regular polygon, regular polygon sides = \n] (#2) {};
\def\n{6}
\pgfmathsetmacro{\start}{int(min(#1+1, \n))};
\pgfmathsetmacro{\stop}{int(max(#1-1, 2))};
\ifthenelse{\equal{#1}{1}}{
\foreach\x in{2,...,\n}{
\foreach\y in{\x,...,\n}{
\draw[color = black, dashed](#2.corner \x)--(#2.corner \y);
}
}
\foreach \x in {1}{
\draw[fill=green!20,dashed](#2.corner \x) circle[radius=1em] node {\x};
}
\foreach \x in {2,...,\n}{
\draw[fill=blue!20](#2.corner \x) circle[radius=1em] node {\x};
}
}{
\foreach\x in {1,...,\stop}{
\foreach\y in {\x,...,\n}{
\ifthenelse{\equal{#1}{\y}}{}{
\draw[color = black, dashed](#2.corner \x) -- (#2.corner \y)};
}
}
\foreach\x in {\start,...,\n}{
\foreach\y in {\x,...,\n}{
\ifthenelse{\equal{#1}{\y}}{}{
\draw[color = black, dashed](#2.corner \x) -- (#2.corner \y)};
}
}
\foreach \x in {1}{
\draw[fill=green!20](#2.corner \x) circle[radius=1em] node {\x};
}
\foreach \x in {2,...,\n}{
\ifthenelse{\equal{#1}{\x}}{
\draw[fill=blue!20,dashed](#2.corner \x) circle[radius=1em] node {\x};
}{
\draw[fill=blue!20](#2.corner \x) circle[radius=1em] node {\x};
};
}
}
}
\begin{document}
\begin{tikzpicture}
\def\n{6}
\node[draw=none, minimum size=3cm, regular polygon, regular polygon sides = \n] (a) {};
\node[draw=none, minimum size=10cm, regular polygon, regular polygon sides = \n] (b) {};
\foreach \x in {4}{
\draw[fill=green!20](a.corner \x) circle[radius=1em] node {1};
}
\foreach \x in {1,...,3}{
\pgfmathparse{int(\x+3)}
\draw[fill=blue!20](a.corner \x) circle[radius=1em] node {\pgfmathresult};
}
\foreach \x in {5,...,6}{
\pgfmathparse{int(\x-3)}
\draw[fill=blue!20](a.corner \x) circle[radius=1em] node {\pgfmathresult};
}
%\foreach \x in {1,...,\n}{
% \begin{scope}[shift={(b.corner \x)}]
% \missingPentagonTemp{2}{c};
% \end{scope}
%}
\begin{scope}[shift={(b.corner 1)}]
\missingPentagon{4}{c};
\end{scope}
\begin{scope}[shift={(b.corner 2)}]
\missingPentagon{5}{c};
\end{scope}
\begin{scope}[shift={(b.corner 3)}]
\missingPentagon{6}{c};
\end{scope}
\begin{scope}[shift={(b.corner 4)}]
\missingPentagon{1}{c};
\end{scope}
\begin{scope}[shift={(b.corner 5)}]
\missingPentagon{2}{c};
\end{scope}
\begin{scope}[shift={(b.corner 6)}]
\missingPentagon{3}{c};
\end{scope}
\end{tikzpicture}
\end{document}
答案1
這是嘗試用 來回答這個問題pic
。此程式碼檢查頂點是否在缺失或突出顯示的頂點列表中(是的,您現在可以使用列表)並相應地繪製邊。
至於你的觀點:
- 完畢。
- 完畢。此時的風格是固定的。一個人可以改變它。
- 老實說,我並沒有嘗試仔細思考程式碼。對不起。
- 是的。現在這些是清單。
- 改變了。也可以只旋轉多邊形,但我喜歡 mod 條件。 ;-)
- 再說一遍,我沒有嘗試。對不起。
完整程式碼:
\documentclass[a4paper,11pt,margin=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\makeatletter
\pgfmathdeclarefunction{memberQ}{2}{%
\begingroup%
\edef\pgfutil@tmpb{0}%memberQ({\lstPast},\inow)
\edef\pgfutil@tmpa{#2}%
\expandafter\pgfmath@member@i#1\pgfmath@token@stop
\edef\pgfmathresult{\pgfutil@tmpb}%
\pgfmath@smuggleone\pgfmathresult%
\endgroup}
\def\pgfmath@member@i#1{%
\ifx\pgfmath@token@stop#1%
\else
\edef\pgfutil@tmpc{#1}%
\ifx\pgfutil@tmpc\pgfutil@tmpa\relax%
\gdef\pgfutil@tmpb{1}%
\fi%
\expandafter\pgfmath@member@i
\fi}
\makeatother
\tikzset{circ/.style={circle,minimum size=2em,draw},
pics/missing polygon/.style={code={
\tikzset{missing polygon/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/missing polygon/##1}}
\node[draw=none, minimum size=\pv{size},
regular polygon, regular polygon sides =\pv{n}] (-poly) {};
% test if the highlighted node is in the missin nodes
\edef\lsthigh{\pv{highlight}}
\foreach \XX in \lsthigh
{\pgfmathtruncatemacro{\ktest}{memberQ(\pv{miss},\XX)}
\xdef\pgfmathresult{\ktest}}
\edef\ktest{\pgfmathresult}
\foreach \XX in {1,...,\the\numexpr\pv{n}-1}
{
\foreach \YY in {2,...,\pv{n}}
{
\pgfmathtruncatemacro{\itest}{memberQ(\pv{miss},\XX)+memberQ(\pv{miss},\YY)}
\ifnum\itest=0
\pgfmathtruncatemacro{\jtest}{memberQ(\pv{highlight},\XX)+memberQ(\pv{highlight},\YY)}
\draw \ifnum\jtest=0 [dashed]\fi \ifnum\ktest=1 [solid]\fi
(-poly.corner \XX) -- (-poly.corner \YY);
\fi
}
}
\foreach \XX in {1,...,\pv{n}}
{\pgfmathtruncatemacro{\itest}{memberQ(\pv{highlight},\XX)}
\ifnum\itest=1
\node[missing polygon/highlighted] (-poly-vertex-\XX) at (-poly.corner \XX){\XX};
\else
\pgfmathtruncatemacro{\jtest}{memberQ(\pv{miss},\XX)}
\ifnum\jtest=1
\node[missing polygon/missing] (-poly-vertex-\XX) at (-poly.corner \XX){\XX};
\else
\node[missing polygon/regular] (-poly-vertex-\XX) at (-poly.corner \XX){\XX};
\fi
\fi
}
}},missing polygon/.cd,size/.initial=3cm,n/.initial=6,
miss/.initial={2},highlight/.initial={1},
highlighted/.style={circ,fill=green!20},
missing/.style={circ,fill=blue!20,dashed},
regular/.style={circ,fill=blue!20}
}
\begin{document}
\begin{tikzpicture}
\def\n{6}
\node[draw=none, minimum size=3cm, regular polygon, regular polygon sides = \n] (a) {};
\node[draw=none, minimum size=10cm, regular polygon, regular polygon sides = \n] (b) {};
\foreach \X in {1,...,\n}{
\pgfmathparse{int(1+Mod(\X-4,6))}
\draw \ifnum\X=4 [fill=green!20] \else [fill=blue!20]\fi
(a.corner \X) circle[radius=1em] node {\pgfmathresult};
}
\path foreach \X in {1,...,\n}
{ [/utils/exec=\pgfmathtruncatemacro{\mymiss}{int(1+Mod(\X-4,6))}]
(b.corner \X) pic(m\X){missing polygon={miss={\mymiss}}}};
\end{tikzpicture}
\end{document}