# Drawing a square with colored hatched little subsquares

I would like to draw a as shown below. It is a square grid. Some regions are hatched in blue, others contain a red cross. Some edges are blue, others are red, but the red color don't reach the vertices, it stops just short of them, revealing the underlying thin black grid. Some vertices are colored red, others are colored blue.

I think I can draw the underlying grid myself, either by manually entering coordinates and instructions to link vertices by straight edges or using the grid command, and I think I should also be able to draw colored thick nodes that cover up whatever they lie on top of. however, I don't know how to draw the "hatched" region.

I want to include this drawing to lecture notes on homotopy theory I'm typing up. One defines a function by some fixed expression on the blue part of the grid, and inductively extends it to the whole square, first on the red vertices, then on the red edges, and lastly on the faces containing a red cross.

• It seems the pattern library will be useful. – Olivier Bégassat Sep 29 '13 at 22:33
• Could you elaborate on how you want to input the data. This is easily doable with TikZ but I’m guessing you want not to create each diagram from scratch. I see: a grid (is this squared-based or line-based?), blue lines making a filling, blue and red dots on the crossings of the grid, blue lines (without filling), red lines (that do not touch the crossings of the grid). – Qrrbrbirlbel Sep 29 '13 at 23:36
• Could you provide a starting point in the form of your efforts so far? – cmhughes Sep 29 '13 at 23:43

With PSTricks just for fun! The remaining tedious parts can be easily added by yourself.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node}

\begin{document}
\begin{pspicture}[showgrid=top](8,8)
\multips(0,.5)(0,1){8}{%
\multips(.5,0)(1,0){8}{%
\psline[linecolor=red](6pt;-135)(6pt;45)
\psline[linecolor=red](6pt;135)(6pt;-45)}}
\pscustom
[
dimen=middle,
fillstyle=eovlines*,
fillcolor=white,
hatchcolor=blue,
linecolor=blue,
]
{
\psframe(8,8)
\pspolygon
(3,1)
(3,4)
(1,4)
(1,6)
(2,6)
(2,7)
(7,7)
(7,4)
(6,4)
(6,6)
(5,6)
(5,4)
(6,4)
(6,3)
(7,3)
(7,1)
(6,1)
(6,2)
(5,2)
(5,1)
}
\psset{linecolor=blue,nodesep=7pt}
\pscircle*(4,2){2pt}
\pscircle*(4,3){2pt}
\pcline(4,2)(4,3)
\end{pspicture}
\end{document}


## update

here is my response to barbara beeton's comment:

• This is just what I need, thank you! But when I copy and paste this into my existing file, it doesn't work, it doesn't work either when I paste the whole text in a new tex document. I get the error message l.10 ...\psline[linecolor=red](6pt;135)(6pt;-45)}} The control sequence at the end of the top line of your error message was never \def'ed – Olivier Bégassat Sep 30 '13 at 0:31
• @OlivierBégassat: Don't forget to accept this answer by clicking the check mark button on the left. Optionally, upvoting it. :-) – kiss my armpit Sep 30 '13 at 2:08
• I spy some indented code :) Just out of curiosity, did you run it through latexindent? oh, and +1, of course :) – cmhughes Sep 30 '13 at 3:51
• no big deal- I tested latexindent.pl and it worked as expected :) might save you lots of tabbing in the future :) – cmhughes Sep 30 '13 at 4:27
• is my browser sick, or are bits missing: the underlying black grid, the colored vertices, and the shorter rules along segments of the grid? – barbara beeton Sep 30 '13 at 14:21

MWE with Asymptote:

% topo.tex :
%
\documentclass{article}
\usepackage[inline]{asymptote}
\begin{asydef}
import patterns;

struct topoBoxPens{
pen gridPen, borderPen, outlinePen, hatchPen;
pen crossPen, redStrokePen, blueStrokePen;
pen labelPen, bgPen;
void operator init(
pen gridPen=darkblue+0.4bp
,pen borderPen=deepblue+2bp
,pen outlinePen=deepblue+1bp
,pen hatchPen=paleblue+2bp
,pen crossPen=red+1.2bp
,pen redStrokePen=  deepred+3bp+opacity(0.7)
,pen blueStrokePen=deepblue+squarecap+3bp+opacity(0.7)
,pen labelPen=olive+fontsize(10pt)
,pen bgPen=paleyellow //white
){
this.gridPen       = gridPen      ;
this.borderPen     = borderPen    ;
this.outlinePen    = outlinePen   ;
this.hatchPen      = hatchPen     ;
this.crossPen      = crossPen     ;
this.redStrokePen  = redStrokePen ;
this.blueStrokePen = blueStrokePen;
this.labelPen      = labelPen     ;
this.bgPen         = bgPen        ;
}
}

struct topoBox{
int n;
real cellWidth;
topoBoxPens Pens;
pair ll, ur;
real dc;
pair[] redDots;
pair[] blueDots;
guide[] redStrokes;
guide[] blueStrokes;

void drawGrid(){
for(int i=0;i<n;++i){
draw((0,i)*cellWidth--(n,i)*cellWidth,Pens.gridPen);
draw((i,0)*cellWidth--(i,n)*cellWidth,Pens.gridPen);
}
}

void drawGridLabels(){
for(int i=0;i<n;++i){
label("$"+string(n-i)+"$",(0,i+0.5)*cellWidth,W,Pens.labelPen);
label("$"+string(n-i)+"$",(n,i+0.5)*cellWidth,E,Pens.labelPen);
label("$"+string(i+1)+"$",(i+0.5,0)*cellWidth,S,Pens.labelPen);
label("$"+string(i+1)+"$",(i+0.5,n)*cellWidth,N,Pens.labelPen);
}
}

void drawBorder(){
draw(box(ll,ur),Pens.borderPen);
}

void drawHatch(){
fill(box(ll,ur),pattern("hatch"));
}

guide[] xcross(transform t){
return t*((-dc,-dc)--(dc,dc))
^^(t*((dc,-dc)--(-dc,dc)));
}

for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
filldraw(
box((j,n-i-1)*cellWidth,(j+1,n-i)*cellWidth)
,Pens.bgPen,Pens.gridPen
);
}
}
}
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
draw(xcross(shift(j+0.5,n-i-0.5)),Pens.crossPen);
}
}
}
}

void drawOutline(){
for(int i=1;i<n-1;++i){
for(int j=1;j<n-1;++j){
draw((j,n-i)--(j+1,n-i),Pens.outlinePen);
}
draw((j,n-i-1)--(j+1,n-i-1),Pens.outlinePen);
}
draw((j,n-i)--(j,n-i-1),Pens.outlinePen);
}
draw((j+1,n-i)--(j+1,n-i-1),Pens.outlinePen);
}
}
}
}

void drawDots(){
for(int i=0;i<redDots.length;++i){
fill(circle((redDots[i].x,n-redDots[i].y),2dc),Pens.redStrokePen);
}
for(int i=0;i<blueDots.length;++i){
fill(circle((blueDots[i].x,n-blueDots[i].y),2dc),Pens.blueStrokePen);
}
}

void drawStrokes(){
for(int i=0;i<redStrokes.length;++i){
draw(reflect((0,n/2),(n,n/2))*subpath(redStrokes[i],0.2,0.8),Pens.redStrokePen);
}
for(int i=0;i<blueStrokes.length;++i){
draw(reflect((0,n/2),(n,n/2))*subpath(blueStrokes[i],0,1),Pens.blueStrokePen);
}
}

void draw(){
drawGrid();
drawGridLabels();
drawHatch();
drawDots();
drawStrokes();
drawOutline();
drawBorder();
}

for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
}
}
}
}
}

void operator init(
int n=8
,pair[] redDots
,pair[] blueDots
,guide[] redStrokes
,guide[] blueStrokes
,real cellWidth=1
,topoBoxPens Pens=topoBoxPens()
){
assert(n>1);
this.n=n;
this.redDots  = copy(redDots );
this.blueDots = copy(blueDots);
this.redStrokes  = copy(redStrokes);
this.blueStrokes = copy(blueStrokes);
this.Pens=Pens;
this.cellWidth=cellWidth;
this.ll=(0,0);
this.ur=(n,n)*cellWidth;
this.dc=0.0618cellWidth;
}
}
\end{asydef}
%
\usepackage{lmodern}
\begin{document}
\begin{figure}
\begin{asy}
settings.outformat="pdf";

size(200);

topoBox tb=topoBox(
array(8,0),
new int[]{0,0,1,1,1,1,1,},
new int[]{0,1,1,1,1,0,1,},
new int[]{0,1,1,1,1,0,1,},
new int[]{0,0,0,1,1,1,},
new int[]{0,0,0,1,1,1,1,},
new int[]{0,0,0,1,1,0,1,},
}
,redDots=new pair[]{(3,2),(4,3),(4,4),(5,5)}
,blueDots=new pair[]{(2,3),(3,3),(4,2),(4,5),(4,6)}
,redStrokes=new guide[]{
(2,2)--(3,2)
,(3,2)--(4,2)
,(4,2)--(5,2)
,(6,2)--(7,2)
,(1,3)--(2,3)
,(3,3)--(4,3)
,(4,3)--(5,3)
,(6,3)--(7,3)
,(3,4)--(4,4)
,(4,4)--(5,4)
,(4,5)--(5,5)
,(5,5)--(6,5)
,(6,6)--(7,6)
,(2,2)--(2,3)
,(2,3)--(2,4)
,(3,1)--(3,2)
,(3,2)--(3,3)
,(3,3)--(3,4)
,(4,2)--(4,3)
,(4,3)--(4,4)
,(4,4)--(4,5)
,(4,6)--(4,7)
,(5,1)--(5,2)
,(5,4)--(5,5)
,(5,5)--(5,6)
,(6,1)--(6,2)
,(6,5)--(6,6)
}
,blueStrokes=new guide[]{
(4,1)--(4,2)
,(4,5)--(4,6)
,(2,3)--(3,3)
,(3,5)--(4,5)
,(3,6)--(4,6)
,(4,6)--(5,6)
}
);
tb.draw();
\end{asy}
\end{figure}
\end{document}
%
% Process:
%
% pdflatex topo.tex
% asy topo-*.asy
% pdflatex topo.tex