我想在彭罗斯三角形(代码如下)中添加文字,并
在边上写上“伦敦”、“巴黎”、“柏林”。我该怎么做?
\begin{tikzpicture}[scale=1, line join=bevel]
% \a and \b are two macros defining characteristic
% dimensions of the Penrose triangle.
\pgfmathsetmacro{\a}{1.8}
\pgfmathsetmacro{\b}{0.7}
\tikzset{%
apply style/.code = {\tikzset{#1}},
triangle_edges/.style = {thick,draw=black}
}
\foreach \theta/\facestyle in {%
0/{triangle_edges, fill = gray!50},
120/{triangle_edges, fill = gray!25},
240/{triangle_edges, fill = gray!90}%
}{
\begin{scope}[rotate=\theta]
\draw[apply style/.expand once=\facestyle]
({-sqrt(3)/2*\a},{-0.5*\a}) --
++ (-\b,0) --
({0.5*\b},{\a+3*sqrt(3)/2*\b}) -- % higher point
({sqrt(3)/2*\a+2.5*\b},{-.5*\a-sqrt(3)/2*\b}) -- % rightmost point
++({-.5*\b},-{sqrt(3)/2*\b}) -- % lower point
({0.5*\b},{\a+sqrt(3)/2*\b}) --
cycle;
\end{scope}
}
\end{tikzpicture}
谢谢,马可
答案1
\documentclass[tikz]{standalone}
\usetikzlibrary{calc,positioning,decorations.text}
\begin{document}
\begin{tikzpicture}[scale=1, line join=bevel]
% \a and \b are two macros defining characteristic
% dimensions of the Penrose triangle.
\pgfmathsetmacro{\a}{1.8}
\pgfmathsetmacro{\b}{0.7}
\tikzset{%
apply style/.code = {\tikzset{#1}},
triangle_edges/.style = {thick,draw=black}
}
\foreach \theta/\facestyle/\city in {%
0/{triangle_edges, fill = gray!50}/London,
120/{triangle_edges, fill = gray!25}/Paris,
240/{triangle_edges, fill = gray!90}/Berlin%
}{
\begin{scope}[rotate=\theta]
\draw[apply style/.expand once=\facestyle]
({-sqrt(3)/2*\a},{-0.5*\a})
-- ++ (-\b,0) coordinate (\city1)
-- ({0.5*\b},{\a+3*sqrt(3)/2*\b}) coordinate (\city2) % higher point
-- ({sqrt(3)/2*\a+2.5*\b},{-.5*\a-sqrt(3)/2*\b}) coordinate (\city3)% rightmost point
-- ++({-.5*\b},-{sqrt(3)/2*\b}) coordinate (\city4) % lower point
-- ({0.5*\b},{\a+sqrt(3)/2*\b}) coordinate (\city5)
-- cycle
;
\end{scope}
}
\path [decorate, decoration={text along path, text={Paris}, text align=center, text color=darkgray}]
($(Paris5) - (1/3*\b,0)$) -- ($(Paris4)!1/3!(Paris3)$);
\path [decorate, decoration={text along path, text={London}, text align=center, text color=darkgray}]
($(London5) + (1/3*\b,0)$) -- ($(London3) - (2/3*\b,0)$);
\path [decorate, decoration={text along path, text={Berlin}, text align=center, text color=white}]
($(Berlin3)!1/3!(Berlin3 |- Berlin4)$) -- ($(Berlin5)!2/3!(Berlin3 -| Berlin5)$);
\end{tikzpicture}
\end{document}
答案2
编辑:根据 OP 的要求,标签现在随每个面旋转。
尝试这个:
\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}[scale=1, line join=bevel]
% \a and \b are two macros defining characteristic
% dimensions of the Penrose triangle.
\pgfmathsetmacro{\a}{1.8}
\pgfmathsetmacro{\b}{0.7}
\tikzset{%
apply style/.code = {\tikzset{#1}},
triangle_edges/.style = {thick,draw=black}
}
\foreach \theta/\facestyle/\city/\al\/\ang in {%
0/{triangle_edges, fill = gray!50}/Paris/below/-60,
120/{triangle_edges, fill = gray!25}/Berlin/below/60,
240/{triangle_edges, fill = gray!90}/London/above/0}
{
\begin{scope}[rotate=\theta]
\draw[apply style/.expand once=\facestyle]
({-sqrt(3)/2*\a},{-0.5*\a}) --
++ (-\b,0) --
({0.5*\b},{\a+3*sqrt(3)/2*\b}) --node[\al,rotate=\ang]{\city} % higher point
({sqrt(3)/2*\a+2.5*\b},{-.5*\a-sqrt(3)/2*\b}) -- % rightmost point
++({-.5*\b},-{sqrt(3)/2*\b}) -- % lower point
({0.5*\b},{\a+sqrt(3)/2*\b}) --
cycle;
\end{scope}
}
\end{tikzpicture}
\end{document}
该三角形现在看起来像这样: