我正在尝试寻找绘制一系列工业图案的最佳方法。在大家的帮助下,我能够绘制规则图案。但是,我现在需要绘制具有不同腔体分布和大小的图案。我还被迫将原点移到信封的中心,这种配置不如以前方便。我尝试了一种与以前不同的方法,制作出我想要的精确形状,但是它非常硬编码,因为腔体编号模式不遵循自然顺序。我尝试过\foreach \x / \n [count=\xi]
看起来很有希望的选项,但我还没有找到对每一行都使用它的方法。有没有办法优化这里缺少的代码,尤其是腔体计数器?
\documentclass{standalone}
\usepackage{tikz}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}
\begin{tikzpicture}[scale=2.54, thick,
twenty/.style={circle, draw, very thin, fill,text=white, minimum size=.097in , text centered,font=\tiny, inner sep=0.1pt},
sixteen/.style={circle, draw, very thin, fill,text=white, minimum size=.150in, text centered,font=\tiny, inner sep=0.1pt},
]
\newlength{\hspacing} %Vertical spacing
\newlength{\vspacing} %Horiz spacing
\setlength{\hspacing}{.108cm}
%Begin envelope
\coordinate (O) at (0,0) ;
\coordinate (A) at ( - 0.7475,-0.435) ;
\coordinate (B) at ( 0.7475,-0.435) ;
\coordinate (C) at (0.7475, 0.249) ;
\coordinate (D) at (0.516 ,0.435) ;
\coordinate (E) at (-0.516 ,0.435) ;
\coordinate (F) at (-0.7475, 0.249) ;
\draw (A) --(B)--(C)--(D)-- (E)--(F)-- cycle ;
%End envelope
\node[red, very thin] at (O) {+}; %origin
%--------------------------------
% Begin top row -mixed cavities- uneven spacing
\setlength{\vspacing}{.28cm}
%small cavity right side
\foreach \x / \n [count=\xi] in {4.5 / \xi, 3.5 / \xi} {
\node [twenty] at (\hspacing*\x, \vspacing){\xi};
}
%Begin large cavity
\foreach \x / \n in {2 / 3, 0 / 4} {
\node [sixteen] at (\hspacing*\x, \vspacing){\n};
}
\foreach \x / \n in {2 / 5} {
\node [sixteen] at (-\hspacing*\x, \vspacing){\n};
}
%small cavity left side
\foreach \x / \n in {4.5/ 6, 3.5/ 7} {
\node [twenty] at (-\hspacing*\x, \vspacing){\n};
}
% end top row
%---------------------------
% Begin row 2 small cavity uneven spacing
\setlength{\vspacing}{.168cm}
%---------------------------
\foreach \x / \n in {6/ 8, 5 / 9, 4 / 10, 3/ 11, 1/12 } {
\node [twenty] at (\hspacing*\x, \vspacing){\n};
}
\foreach \x / \n in {6/ 17, 5/ 16, 4 / 15, 3/ 14, 1/ 13 } {
\node [twenty] at (-\hspacing*\x, \vspacing){\n};
}
%End small cavity
% end row 2
%---------------------------
% Begin row 3 - small cavity
\setlength{\vspacing}{.056cm}
%Begin small cavity
\foreach \x / \n in {5.5/18, 4.5/19, 3.5/20, 2.5/21, 1.5/22, 0.5/23} {
\node [twenty] at (\hspacing*\x, \vspacing){\n};
}
\foreach \x / \n in {5.5/29, 4.5/28, 3.5/27, 2.5/26, 1.5/25, 0.5/24} {
\node [twenty] at (-\hspacing*\x, \vspacing){\n};
}
%End small cavity
% end row 3
%---------------------------
% Begin row 4 - small cavity
\setlength{\vspacing}{-.056cm}
\foreach \x / \n in {6/30, 5/31, 4/32, 3/33, 2/34, 1/35, 0/36} {
\node [twenty] at (\hspacing*\x,\vspacing){\n};
}
\foreach \x / \n in {6 /42, 5/41, 4/40, 3/39, 2/38, 1/37} {
\node [twenty] at (-\hspacing*\x, \vspacing){\n};
}
%End small cavity
% end row 5
%---------------------------
% Begin row 5 - small cavity
\setlength{\vspacing}{-0.162cm}
%Begin small cavity
\foreach \x / \n in {5.5/43, 4.5/44, 3.5/45, 2.5/46, 1.5/47, 0.5/48} {
\node [twenty] at (\hspacing*\x, \vspacing){\n};
}
\foreach \x / \n in {5.5/54, 4.5/53, 3.5/52, 2.5/51, 1.5/50, 0.5/49} {
\node [twenty] at (-\hspacing*\x, \vspacing){\n};
}
%---------------------------
% end row 5
% Begin row 6 -small cavity
\setlength{\vspacing}{-0.27cm}
\foreach \x / \n in {6 /55, 5/56, 4/57, 3/58, 2/59, 1/60, 0/61} {
\node [twenty] at (\hspacing*\x,\vspacing){\n};
}
\foreach \x / \n in {6 /67, 5/65, 4/65, 3/64, 2/63, 1/62} {
\node [twenty] at (-\hspacing*\x, \vspacing){\n};
}
% end row 6
\end{tikzpicture}
\end{document}
答案1
HTH!整个来源的评论。
结果:
代码如下:
\documentclass{standalone}
\usepackage{tikz}
% e-TeX power =)
\usepackage{etoolbox}
\newcounter{cavities}
% associates each cavity with its style
% #1: row number
% #2: column number
% #3: small or big (styles)
\newcommand\setcavitystyle[3]{\csgdef{cavitystyler#1c#2}{#3}}
\newcommand\getcavitystyle[2]{\csuse{cavitystyler#1c#2}}
% determines whether or not the cavity is drawn
% #1: row number
% #2: column number
% #3: 0 (disabled) or 1 (enabled)
\newcommand\setpointenabled[3]{\csnumgdef{styler#1c#2}{#3}}
\newcommand\getpointenabled[2]{\csuse{styler#1c#2}}
% builds the possible locations of the cavities
% #1: max absolute x
% #2: max absolute y
\newcommand{\makebase}[2]{
\pgfmathtruncatemacro{\x}{#1}
\pgfmathtruncatemacro{\y}{#2}
\foreach \i in { -\x, ..., \x} {
\foreach \j in { -\y, ..., \y} {
\coordinate (base-\i-\j) at (\i, \j);
}
}
\drawenvelope
}
% draw envelope and defines useful area
% from (-\xuseful, -\yuseful) to (\xuseful, \yuseful)
\newcommand{\drawenvelope} {
\pgfmathtruncatemacro{\xuseful}{\x - 1}
\pgfmathtruncatemacro{\yuseful}{\y - 1}
\draw [
line width = 1mm
] (-\x, -\y) -- (-\x, \yuseful) -- (-\xuseful, \y) -- (\xuseful, \y) -- (\x, \yuseful) -- (\x, -\y) -- cycle;
}
% defines all possible cavities to be used with a basic style.
% uses only useful area
\newcommand{\makebasicpoints}{
\foreach \j in {\yuseful, ..., -\yuseful} {
\foreach \i in {\xuseful, ..., -\xuseful} {
\ifboolexpr{
(test{\ifnumequal{\j}{\yuseful}} and test{\ifnumequal{\i}{\xuseful}})
or
(test{\ifnumequal{\j}{\yuseful}} and test{\ifnumequal{\i}{-\xuseful}})
}{
\setpointenabled{\i}{\j}{0}
}{
\ifboolexpr{
(test{\ifnumodd{\i}} and test{\ifnumodd{\j}})
or
(not(test{\ifnumodd{\i}}) and not(test{\ifnumodd{\j}}))
}{
\setpointenabled{\i}{\j}{0}
}{
\setpointenabled{\i}{\j}{1}
\setcavitystyle{\i}{\j}{small}
}
}
}
}
}
% defines a specific cavities with "big" style.
% disables drawing near cavities (above, below, right and left)
% #1: x
% #2: y
\newcommand{\makebiggerpoint}[2]{
\setpointenabled{#1}{#2}{1}
\setcavitystyle{#1}{#2}{big}
\pgfmathtruncatemacro{\ytemp}{#2}
\pgfmathtruncatemacro{\xtemp}{#1 - 1}
\setpointenabled{\xtemp}{\ytemp}{0}
\pgfmathtruncatemacro{\ytemp}{#2}
\pgfmathtruncatemacro{\xtemp}{#1 + 1}
\setpointenabled{\xtemp}{\ytemp}{0}
\pgfmathtruncatemacro{\ytemp}{#2 + 1}
\pgfmathtruncatemacro{\xtemp}{#1}
\setpointenabled{\xtemp}{\ytemp}{0}
\pgfmathtruncatemacro{\ytemp}{#2 - 1}
\pgfmathtruncatemacro{\xtemp}{#1}
\setpointenabled{\xtemp}{\ytemp}{0}
}
% draw each cavity if it is enabled.
\newcommand{\makecavities}{
\foreach \j in {\yuseful, ..., -\yuseful} {
\foreach \i in {\xuseful, ..., -\xuseful} {
\ifnumequal{\getpointenabled{\i}{\j}}{1}{
\stepcounter{cavities}
\coordinate (cavity-\thecavities) at (\i, \j);
\node [\getcavitystyle{\i}{\j}] at (cavity-\thecavities) {\thecavities};
}{
}
}
}
}
% styles
\tikzset{
small/.style = {
circle
, fill
, minimum size = 10mm
, inner sep = 0mm
, font = \small\bf
, text = white
, inner sep = 0pt
}
, big/.style = {
circle
, fill
, minimum size = 15mm
, inner sep = 0mm
, text = white
, font = \bf
, inner sep = 0pt
}
}
\begin{document}
\begin{tikzpicture}
\makebase{10}{5}
\draw [red, ultra thick] (0,0) +(0, 4mm) -- +(0, -4mm) +(4mm, 0) -- +(-4mm, 0);
\makebasicpoints
\foreach \k in {-6, -3, ..., 6} {\makebiggerpoint{\k}{3}}
\foreach \k in {-7, -4, ..., 8} {\makebiggerpoint{\k}{-2}}
\makecavities
\end{tikzpicture}
\end{document}