我想修改马克·维布罗的三角分数图这样就可以绘制黑色部分,而不是在三角形之间留下空白。由于我不明白代码是如何工作的,所以我想向社区寻求帮助:
\documentclass[tikz, border=5]{standalone}
\newcount\tikzfractiondenominator
\newcount\tikzfractionnumerator
\def\tikzfractionempty{}
\let\tikzfractionstyle=\tikzfractionempty
\newif\iftikzfractionfill
\tikzset{pics/.cd,
fraction/.style={%
code={%
\tikzset{pics/fraction/.cd, #1}%
\pgfmathparse{int(ceil(\tikzfractionnumerator/\tikzfractiondenominator))}%
\let\tikzfractionshapetotal=\pgfmathresult
\ifx\tikzfractionstyle\tikzfractionempty
\else%
\pgfmathloop
\ifnum\tikzfractionnumerator<1
\else
\pgfmathsetmacro\tikzfractionproper{int(\tikzfractionnumerator?\tikzfractionnumerator:\tikzfractiondenominator)}%
\foreach \tikzfractionsegmentnumber in {1,...,\tikzfractiondenominator}{%
\ifnum\tikzfractionsegmentnumber>\tikzfractionproper\relax%
\tikzfractionfillfalse%
\else%
\tikzfractionfilltrue%
\fi%
\let\tikzfractionshapenumber=\pgfmathcounter%
\begin{scope}
\tikzset{pics/fraction/\tikzfractionstyle/shape position/.try}%
\tikzset{pics/fraction/\tikzfractionstyle/segment position/.try}%
\tikzset{pics/fraction/\tikzfractionstyle/segment draw/.try}%
\end{scope}
}%
\advance\tikzfractionnumerator by-\tikzfractiondenominator%
\repeatpgfmathloop%
\fi%
}
},
fraction/.cd,
style/.store in=\tikzfractionstyle,
numerator/.code=\pgfmathsetcount\tikzfractionnumerator{#1},
denominator/.code=\pgfmathsetcount\tikzfractiondenominator{#1},
fraction/.style args={#1/#2}{%
/tikz/pics/fraction/.cd,
numerator={#1}, denominator={#2}
}
}
\tikzset{%
/tikz/pics/fraction/triangles/.cd,
shape position/.code={
\pgfmathsetmacro\y{sqrt(\tikzfractiondenominator)}
\tikzset{
shift=(0:{(\tikzfractionshapenumber-1)*\y}),
shift={(0,\y/4)},
}
},
segment position/.code={
\let\i=\tikzfractionsegmentnumber
\pgfmathsetmacro\z{int(sqrt(\i-1))}
\pgfmathsetmacro\q{\i-(\z)^2}
\tikzset{
shift={({sin(60) * (\q-\z) / 2}, {-\z*0.75 -mod(\q,2)*cos(60)/2})},
rotate={mod(\q-1,2)*180}
}
},
segment draw/.code={
\iftikzfractionfill
\tikzset{triangle fill/.style={blue!50!cyan!50}}
\else
\tikzset{triangle fill/.style={gray!20}}
\fi
\fill [triangle fill] (90:0.45) -- (210:0.45) -- (330:0.45) -- cycle;
}
}
\begin{document}
\begin{tikzpicture}
\foreach \numerator/\denominator [count=\y] in {1/1, 2/4, 13/9}{
\tikzset{shift=(270:\y*2)}
\pic {fraction={style=triangles, fraction={\numerator/\denominator}}};
\node at (-1,0) {$\frac{\numerator}{\denominator}$};
}
\end{tikzpicture}
\end{document}
这里有一个解决方案可以绘制出与我所寻找的相似的漂亮的三角形分数图,但我希望找到一个允许我继续使用 Mark Wibrow 的简单\begin{tikzpicture}\pic{fraction={style=triangles, fraction={2/4}}};\end{tikzpicture}
宏的解决方案。提前谢谢您!
答案1
最小手术,即更换
segment draw/.code={
\iftikzfractionfill
\tikzset{triangle fill/.style={blue!50!cyan!50}}
\else
\tikzset{triangle fill/.style={gray!20}}
\fi
\fill [triangle fill] (90:0.45) -- (210:0.45) -- (330:0.45) -- cycle;
}
经过
segment draw/.code={
\iftikzfractionfill
\tikzset{triangle fill/.style={fill=blue!50!cyan!50}}
\else
\tikzset{triangle fill/.style={fill=gray!20}}
\fi
\draw [triangle fill] (90:0.500) -- (210:0.500) -- (330:0.500) -- cycle;
}
产量
\documentclass[tikz, border=5]{standalone}
\newcount\tikzfractiondenominator
\newcount\tikzfractionnumerator
\def\tikzfractionempty{}
\let\tikzfractionstyle=\tikzfractionempty
\newif\iftikzfractionfill
\tikzset{pics/.cd,
fraction/.style={%
code={%
\tikzset{pics/fraction/.cd, #1}%
\pgfmathparse{int(ceil(\tikzfractionnumerator/\tikzfractiondenominator))}%
\let\tikzfractionshapetotal=\pgfmathresult
\ifx\tikzfractionstyle\tikzfractionempty
\else%
\pgfmathloop
\ifnum\tikzfractionnumerator<1
\else
\pgfmathsetmacro\tikzfractionproper{int(\tikzfractionnumerator?\tikzfractionnumerator:\tikzfractiondenominator)}%
\foreach \tikzfractionsegmentnumber in {1,...,\tikzfractiondenominator}{%
\ifnum\tikzfractionsegmentnumber>\tikzfractionproper\relax%
\tikzfractionfillfalse%
\else%
\tikzfractionfilltrue%
\fi%
\let\tikzfractionshapenumber=\pgfmathcounter%
\begin{scope}
\tikzset{pics/fraction/\tikzfractionstyle/shape position/.try}%
\tikzset{pics/fraction/\tikzfractionstyle/segment position/.try}%
\tikzset{pics/fraction/\tikzfractionstyle/segment draw/.try}%
\end{scope}
}%
\advance\tikzfractionnumerator by-\tikzfractiondenominator%
\repeatpgfmathloop%
\fi%
}
},
fraction/.cd,
style/.store in=\tikzfractionstyle,
numerator/.code=\pgfmathsetcount\tikzfractionnumerator{#1},
denominator/.code=\pgfmathsetcount\tikzfractiondenominator{#1},
fraction/.style args={#1/#2}{%
/tikz/pics/fraction/.cd,
numerator={#1}, denominator={#2}
}
}
\tikzset{%
/tikz/pics/fraction/triangles/.cd,
shape position/.code={
\pgfmathsetmacro\y{sqrt(\tikzfractiondenominator)}
\tikzset{
shift=(0:{(\tikzfractionshapenumber-1)*\y}),
shift={(0,\y/4)},
}
},
segment position/.code={
\let\i=\tikzfractionsegmentnumber
\pgfmathsetmacro\z{int(sqrt(\i-1))}
\pgfmathsetmacro\q{\i-(\z)^2}
\tikzset{
shift={({sin(60) * (\q-\z) / 2}, {-\z*0.75 -mod(\q,2)*cos(60)/2})},
rotate={mod(\q-1,2)*180}
}
},
segment draw/.code={
\iftikzfractionfill
\tikzset{triangle fill/.style={fill=blue!50!cyan!50}}
\else
\tikzset{triangle fill/.style={fill=gray!20}}
\fi
\draw [triangle fill] (90:0.500) -- (210:0.500) -- (330:0.500) -- cycle;
}
}
\begin{document}
\begin{tikzpicture}
\foreach \numerator/\denominator [count=\y] in {1/1, 2/4, 13/9}{
\tikzset{shift=(270:\y*2)}
\pic {fraction={style=triangles, fraction={\numerator/\denominator}}};
\node at (-1,0) {$\frac{\numerator}{\denominator}$};
}
\end{tikzpicture}
\end{document}