我有:
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{parrows, arrows.meta, math, matrix, positioning}
\begin{document}
\begin{tikzpicture}[
vertex/.style={draw, circle, inner sep=0, minimum size=0.12cm}
]
\foreach \x in {0,1,...,6} {
\foreach \y in {0,1,...,7} {
\node at (\x*0.4,\y*0.4) [vertex, color=blue] {};
}
}
\foreach \x in {0,1,...,3} {
\foreach \y in {0,1,...,3} {
\node at (\x*0.5+0.35,\y*0.5+0.55) (O\x\y)[vertex, draw=none, fill=orange] {};
}
}
\node[draw, dashed, color=orange, minimum size=2.35cm, thick] (BB) at (1.1,1.3) {};
\end{tikzpicture}
\end{document}
它繪製了一堆藍色節點和一些橘色節點。橙色節點被橙色矩形 BB 包圍。
- 如何僅更改橙色矩形中包含的藍色節點的顏色?
- 有沒有更聰明的方法將橘色矩形精確定位在橘色節點的中點?
謝謝!
答案1
第二點的答案是使用函式庫fit
就說fit=(O00)(O33)
。對於第一點,您可以使用\x
和上的條件\y
並相應地更改color=
,或者更容易(但不是最有效)繪製矩形clip
路徑並將綠色節點放入其中。
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}[
vertex/.style={draw, circle, inner sep=0, minimum size=0.12cm}
]
\newcommand{\drawcircles}[1]{%
\foreach \x in {0,1,...,6} {
\foreach \y in {0,1,...,7} {
\node at (\x*0.4,\y*0.4) [vertex, color=#1] {};
}
}}
\drawcircles{blue}
\foreach \x in {0,1,...,3} {
\foreach \y in {0,1,...,3} {
\node at (\x*0.5+0.35,\y*0.5+0.55) (O\x\y)[vertex, draw=none, fill=orange] {};
}
}
\node[draw, dashed, color=orange, thick, fit=(O00)(O33)] (BB){};
\path [clip] (BB.north west) rectangle (BB.south east);
\drawcircles{green,thick}
\end{tikzpicture}
\end{document}
以及條件方法:
\documentclass{scrartcl}
\usepackage{tikz,ifthen} % <---
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}[
vertex/.style={draw, circle, inner sep=0, minimum size=0.12cm}
]
\foreach \x in {0,1,...,6} {
\foreach \y in {0,1,...,7} {
\ifthenelse{\x>0\AND\x<6 \AND \y>0\AND\y<6} % condition
{\node at (\x*0.4,\y*0.4) [vertex, color=green] {};} % true
{\node at (\x*0.4,\y*0.4) [vertex, color=blue] {};} % false
}
}
\foreach \x in {0,1,...,3} {
\foreach \y in {0,1,...,3} {
\node at (\x*0.5+0.35,\y*0.5+0.55) (O\x\y)[vertex, draw=none, fill=orange] {};
}
}
\node[draw, dashed, color=orange, thick, fit=(O00)(O33)] (BB){};
\end{tikzpicture}
\end{document}
答案2
與@marmot討論後,我修正了我的想法,現在有8個值需要傳遞。為了提供幫助,我創建了命令
\gridBlue{xmin}{xmax}{ymin}{ymax}
\gridOrange{xmin}{xmax}{ymin}{ymax}
這樣圓圈就形成了格子[xmin,xmax] \times [ymin,ymax]
(藍色和橘色)。
另外,請注意如果圓與矩形接觸,不考慮裡面所以仍然是藍色。
最後,最重要的零件在哪裡\x <= \xmaxOrange + 1
以及\y <= \ymaxOrange + 2
添加的位置應該調整取決於橘色圓圈的擾動。
評論:OP將節點座標乘以一個因子並平移它們;這個過程使得自動計算擾動因子變得困難。
\documentclass[tikz,border=1mm]{standalone}
% grid format
% [#1,#2] \times [#3,#4]
\newcommand{\gridBlue}[4]{
\pgfmathtruncatemacro{\xminBlue}{#1}
\pgfmathtruncatemacro{\xmaxBlue}{#2}
\pgfmathtruncatemacro{\yminBlue}{#3}
\pgfmathtruncatemacro{\ymaxBlue}{#4}
}
\newcommand{\gridOrange}[4]{
\pgfmathtruncatemacro{\xminOrange}{#1}
\pgfmathtruncatemacro{\xmaxOrange}{#2}
\pgfmathtruncatemacro{\yminOrange}{#3}
\pgfmathtruncatemacro{\ymaxOrange}{#4}
}
\begin{document}
% [1,7] \times [1,6]
\gridBlue{1}{7}{1}{6}
% [2,4] \times [1,3]
\gridOrange{2}{4}{1}{3}
\begin{tikzpicture}[
vertex/.style={draw, circle, inner sep=0, minimum size=0.12cm}
]
\foreach \x in {\xminBlue,...,\xmaxBlue} {
\foreach \y in {\yminBlue,...,\ymaxBlue} {
\pgfmathsetmacro{\mycolor}{ifthenelse(\x >= \xminOrange+2 && \x <= \xmaxOrange+1 && \y >= \yminOrange+2 && \y <= \ymaxOrange+2, "green","blue")}
\node at (\x*0.4,\y*0.4) [vertex, color=\mycolor] {};
}
}
\begin{scope}[local bounding box=BB]
\foreach \x in {\xminOrange,...,\xmaxOrange} {
\foreach \y in {\yminOrange,...,\ymaxOrange} {
\node at (\x*0.5+0.35,\y*0.5+0.55) [vertex, draw=none, fill=orange] {};
}
}
\end{scope}
\draw[color=orange, thin]
([xshift=-1pt,yshift=-1pt]BB.south west) rectangle ([xshift=1pt,yshift=1pt]BB.north east);
\end{tikzpicture}
\end{document}
編輯:下圖顯示了平移如何影響橙色網格:黑色箭頭指向(\xminOrange*0.5,\yminOrange*0.5)
到(\xminOrange*0.5 + .35,\yminOrange*0.5 + .55)
。