修改折纸图代码

修改折纸图代码

使用以下修改后的代码这个帖子,在包含其他模型的文档中绘制截头二十面体的一部分(在仍在进行的项目中);从而生成第一张图纸。

是否可以修改代码(用于此特定模型,而不影响用于文档中其他模型的默认代码),以便五边形可以涂成红色,而长方形可以涂成黄色,如第二张图所示!

\documentclass{article}
\usepackage{verbatim}
\usepackage{tikz,xparse}
\usetikzlibrary{calc}
\begin{document}
\tikzset{%
patron/.style={%
 line join=round, rounded corners=.05pt, draw, thin},
patron side/.style={patron},
patron languette/.style={patron},%
}
\newcounter{NodePat}
\NewDocumentCommand{\PolygReg}{%
O{3}    %
m       %
O{A}    %
}
{%
\foreach \a/\b in {#2} 
{%
\path[patron side] let
\p1 = ($(\a)!.5!(\b)$) ,
\n1 = {veclen(\x1,\y1)} ,
\p2 = ($(\p1)!1/tan(180/#1)!90:(\b)$)
in
(\a)--(\b)
\foreach \i [%
  evaluate=\i as \j using (\i-1)*360/#1] in {3,...,#1} {%
  -- ($(\p2)!1!\j:(\a)$) coordinate (#3\theNodePat)
  \pgfextra{\stepcounter{NodePat}}
  }
-- cycle ;
}
}
\NewDocumentCommand{\Languette}{%
O{.15}  %
D<>{30} %
m       %
D<>{30} %
O{A}
}
{%
\foreach \b/\a in {#3} {%
\path[patron languette] let
\p1 = ($(#5\b)!#1/sin(#2)!-#2:(#5\a)$),
\p2 = ($(#5\a)!#1/sin(#4)!#4:(#5\b)$)
in
(#5\a) -- (#5\b) -- (\p1) -- (\p2) -- cycle ; }
}
\makeatletter
\newcommand{\AffNodesPatron}[1][A]{%
 \newcount\X
 \X=1
 \loop
 \expandafter\ifx\csname pgf@sh@pi@A\the\X\endcsname\pgfpictureid
 \node[font={\footnotesize},red] at (A\the\X) {A\the\X} ;
 \advance \X by 1
 \else
 \X=0
 \fi
 \unless\ifnum \X=0
 \repeat
}
\makeatother
\hskip -1.8cm
\begin{tikzpicture}[scale=2.5]
\coordinate (A1) at (0,0) ;
\coordinate (A2) at (0,-1) ;
\setcounter{NodePat}{3}
\PolygReg[5]{A1/A2}
\PolygReg[6]{A2/A1,A1/A5,A5/A4,A4/A3,A3/A2}
\PolygReg[5]{A7/A6,A11/A10,A15/A14,A19/A18,A23/A22}
\Languette<12>{1/6,5/10,4/14,3/18,2/22,6/26,10/29,14/32,18/35,22/38}
\AffNodesPatron
\end{tikzpicture}
\end{document}

在此处输入图片描述

在此处输入图片描述

答案1

定义\filltrue获取下面的图像或\fillfalse仅获取轮廓。由于\iffill可以动态更改为 true 或 false,因此当您想要填充的版本时,您可以将标志设置为 true,然后为所有其他版本将其重置为 false。

我首先创建一个新的 if,\newif\iffill然后在宏定义的开头\PolygReg添加以下行:

\iffill\ifnum#1=5\def\tmp{fill=red}\else\def\tmp{}\fi\else\def\tmp{}\fi

仅当且(即五边形)时,这才会定义\tmp为,否则将被设置为空。然后,在调用路径时,我使用fill=red\filltrue\ifnum#1=5

\expandafter\path\expandafter[\tmp, patron side] let

因此\tmp在执行之前会进行扩展\path

我对 的定义做了一些类似的事情\Languette,但颜色是金色。

\documentclass{article}
\usepackage{verbatim}
\usepackage{tikz,xparse}
\usetikzlibrary{calc}
\newif\iffill
\filltrue
\begin{document}
\tikzset{%
patron/.style={%
 line join=round, rounded corners=.05pt, draw, thin},
patron side/.style={patron},
patron languette/.style={patron},%
}
\newcounter{NodePat}
\NewDocumentCommand{\PolygReg}{%
O{3}    %
m       %
O{A}    %
}
{%
\iffill\ifnum#1=5\def\tmp{fill=red}\else\def\tmp{}\fi\else\def\tmp{}\fi
\foreach \a/\b in {#2} 
{%
\expandafter\path\expandafter[\tmp, patron side] let
\p1 = ($(\a)!.5!(\b)$) ,
\n1 = {veclen(\x1,\y1)} ,
\p2 = ($(\p1)!1/tan(180/#1)!90:(\b)$)
in
(\a)--(\b)
\foreach \i [%
  evaluate=\i as \j using (\i-1)*360/#1] in {3,...,#1} {%
  -- ($(\p2)!1!\j:(\a)$) coordinate (#3\theNodePat)
  \pgfextra{\stepcounter{NodePat}}
  }
-- cycle ;
}
}
\NewDocumentCommand{\Languette}{%
O{.15}  %
D<>{30} %
m       %
D<>{30} %
O{A}
}
{%
\iffill\def\tmp{fill=yellow!70!orange}\else\def\tmp{}\fi
\foreach \b/\a in {#3} {%
\expandafter\path\expandafter[\tmp, patron languette] let
\p1 = ($(#5\b)!#1/sin(#2)!-#2:(#5\a)$),
\p2 = ($(#5\a)!#1/sin(#4)!#4:(#5\b)$)
in
(#5\a) -- (#5\b) -- (\p1) -- (\p2) -- cycle ; }
}
\makeatletter
\newcommand{\AffNodesPatron}[1][A]{%
 \newcount\X
 \X=1
 \loop
 \expandafter\ifx\csname pgf@sh@pi@A\the\X\endcsname\pgfpictureid
 \node[font={\footnotesize},red] at (A\the\X) {A\the\X} ;
 \advance \X by 1
 \else
 \X=0
 \fi
 \unless\ifnum \X=0
 \repeat
}
\makeatother
\hskip -1.8cm
\begin{tikzpicture}[scale=2.5]
\coordinate (A1) at (0,0) ;
\coordinate (A2) at (0,-1) ;
\setcounter{NodePat}{3}
\PolygReg[5]{A1/A2}
\PolygReg[6]{A2/A1,A1/A5,A5/A4,A4/A3,A3/A2}
\PolygReg[5]{A7/A6,A11/A10,A15/A14,A19/A18,A23/A22}
\Languette<12>{1/6,5/10,4/14,3/18,2/22,6/26,10/29,14/32,18/35,22/38}
\AffNodesPatron
\end{tikzpicture}
\end{document}

在此处输入图片描述


补充

OP 通过评论请求更多功能:

  1. 定义多个默认多边形颜色,

  2. 允许单个 n 边形的颜色覆盖。

楼主尝试解决第 1 点,但忽略了一个重要细节。如果#1匹配没有任何在预先指定的 n 边形默认值中,必须取消填充,否则将使用最后已知的填充。

对于第 2 点,我添加了第二个可选参数\PolygReg,其中fill =可以指定实际选项。

因此,修订后的测试现在

\ifx\relax#2\relax
  \iffill
    \ifnum#1=3 \def\tmp{fill=violet}\else
    \ifnum#1=4 \def\tmp{fill=blue}\else
    \ifnum#1=5 \def\tmp{fill=red}\else
    \def\tmp{}\fi\fi\fi
  \else
    \def\tmp{}%
  \fi 
\else
  \def\tmp{#2}
\fi

这是 MWE。

\documentclass{article}
\usepackage{verbatim}
\usepackage{tikz,xparse}
\usetikzlibrary{calc}
\newif\iffill
\filltrue
\begin{document}
\tikzset{%
patron/.style={%
 line join=round, rounded corners=.05pt, draw, thin},
patron side/.style={patron},
patron languette/.style={patron},%
}
\newcounter{NodePat}
\NewDocumentCommand{\PolygReg}{%
O{3}    %
O{}
m       %
O{A}    %
}
{%
\ifx\relax#2\relax
  \iffill
    \ifnum#1=3 \def\tmp{fill=violet}\else
    \ifnum#1=4 \def\tmp{fill=blue}\else
    \ifnum#1=5 \def\tmp{fill=red}\else
    \def\tmp{}\fi\fi\fi
  \else
    \def\tmp{}%
  \fi 
\else
  \def\tmp{#2}
\fi
\foreach \a/\b in {#3} 
{%
\expandafter\path\expandafter[\tmp, patron side] let
\p1 = ($(\a)!.5!(\b)$) ,
\n1 = {veclen(\x1,\y1)} ,
\p2 = ($(\p1)!1/tan(180/#1)!90:(\b)$)
in
(\a)--(\b)
\foreach \i [%
  evaluate=\i as \j using (\i-1)*360/#1] in {3,...,#1} {%
  -- ($(\p2)!1!\j:(\a)$) coordinate (#4\theNodePat)
  \pgfextra{\stepcounter{NodePat}}
  }
-- cycle ;
}
}
\NewDocumentCommand{\Languette}{%
O{.15}  %
D<>{30} %
m       %
D<>{30} %
O{A}
}
{%
\iffill\def\tmp{fill=yellow!70!orange}\else\def\tmp{}\fi
\foreach \b/\a in {#3} {%
\expandafter\path\expandafter[\tmp, patron languette] let
\p1 = ($(#5\b)!#1/sin(#2)!-#2:(#5\a)$),
\p2 = ($(#5\a)!#1/sin(#4)!#4:(#5\b)$)
in
(#5\a) -- (#5\b) -- (\p1) -- (\p2) -- cycle ; }
}
\makeatletter
\newcommand{\AffNodesPatron}[1][A]{%
 \newcount\X
 \X=1
 \loop
 \expandafter\ifx\csname pgf@sh@pi@A\the\X\endcsname\pgfpictureid
 \node[font={\footnotesize},red] at (A\the\X) {A\the\X} ;
 \advance \X by 1
 \else
 \X=0
 \fi
 \unless\ifnum \X=0
 \repeat
}
\makeatother
\hskip -1.8cm
\begin{tikzpicture}[scale=2.5]
\coordinate (A1) at (0,0) ;
\coordinate (A2) at (0,-1) ;
\setcounter{NodePat}{3}
\PolygReg[5]{A1/A2}
\PolygReg[6]{A2/A1,A1/A5,A5/A4,A4/A3,A3/A2}
\PolygReg[5]{A7/A6,A11/A10,A15/A14,A19/A18}
\PolygReg[5][fill=cyan]{A23/A22}
\PolygReg[3]{A40/A39}
\Languette<12>{1/6,5/10,4/14,3/18,2/22,6/26,10/29,14/32,18/35,22/38}
\AffNodesPatron
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容