
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;
int[][] mask;
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(){
add("hatch",hatch(2mm,Pens.hatchPen));
fill(box(ll,ur),pattern("hatch"));
}
guide[] xcross(transform t){
return t*((-dc,-dc)--(dc,dc))
^^(t*((dc,-dc)--(-dc,dc)));
}
void drawMask(){
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(mask[i][j]==1){
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){
if(mask[i][j]==1){
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){
if(mask[i][j]==1 && mask[i-1][j]==0){
draw((j,n-i)--(j+1,n-i),Pens.outlinePen);
}
if(mask[i][j]==1 && mask[i+1][j]==0){
draw((j,n-i-1)--(j+1,n-i-1),Pens.outlinePen);
}
if(mask[i][j]==1 && mask[i][j-1]==0){
draw((j,n-i)--(j,n-i-1),Pens.outlinePen);
}
if(mask[i][j]==1 && mask[i][j+1]==0){
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();
drawMask();
drawDots();
drawStrokes();
drawOutline();
drawBorder();
}
void copyMask(int[][]mask){
for(int i=0;i<n;++i){
if(mask.initialized(i)){
for(int j=0;j<n;++j){
if(mask[i].initialized(j)){
this.mask[i][j]=mask[i][j];
}
}
}
}
}
void operator init(
int n=8
,int[][] mask
,pair[] redDots
,pair[] blueDots
,guide[] redStrokes
,guide[] blueStrokes
,real cellWidth=1
,topoBoxPens Pens=topoBoxPens()
){
assert(n>1);
this.n=n;
this.mask=array(n,array(n,0));
copyMask(mask);
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(
mask=new int[][]{
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