当节点包含在矩形内时颜色不同

当节点包含在矩形内时颜色不同

我有:

\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)

在此处输入图片描述

相关内容