![当节点包含在矩形内时颜色不同](https://linux22.com/image/390991/%E5%BD%93%E8%8A%82%E7%82%B9%E5%8C%85%E5%90%AB%E5%9C%A8%E7%9F%A9%E5%BD%A2%E5%86%85%E6%97%B6%E9%A2%9C%E8%89%B2%E4%B8%8D%E5%90%8C.png)
我有:
\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)
。