import settings;
import graph;
pdfviewer="evince";
psviewer="evince";
// returns a pair representation of a relative number of the equivalent class
pair pairRepresentation(int n){
if(n>0){
return(n,0);
}else{
return(0,-n);
}
}
string nullString(real r){
return "";
}
string pairToString(pair point){
return "("+string(point.x)+","+string(point.y)+")";
}
void drawCoordinates(pair point, align align=NoAlign){
label("$"+pairToString(point)+"$",point,align,mediumgray+opacity(0.2)+fontsize(8));
}
string latexBold(real r){
return "$\mathbf{"+string(r)+"}$";
}
string latexBold(string s){
return "$\mathbf{"+s+"}$";
}
// schema representing the addition of N1 + N2
int N1=-1 ;
int N2= 2 ;
int num = 5; // size of the grid, num * num
pen[] Colors=new pen[2*num+1];
Colors.cyclic=true;
int i;
for (i=-1*num ; i<=num ; ++i) {
Colors[i] = blue;
}
Colors[N1]=brown;
Colors[N2]=green;
Colors[N1+N2]=orange;
unitsize(50,50);
// drawing the equivalent classes
for (i=-1*num ; i<=num ; ++i) {
pair point = pairRepresentation(i) ;
dot(point,red);
// choosing the color of the numbers, blue by default
pen numberColor = Colors[i];
// equivalence classes labelled with usual names, in blue
label(latexBold(i),point,5SW,fontsize(17)+numberColor);
int j;
for(j=abs(i);j<num;++j) {
drawCoordinates(point,E);
pair nextpoint = point + (1,1);
draw(point -- nextpoint,numberColor+Dotted+linewidth(2));
dot(point,red+linewidth(5));
point=nextpoint;
}
dot(point,red);
draw(point -- point+(0.5,0.5),numberColor+Dotted+linewidth(2));
dot(point,red+linewidth(5));
drawCoordinates(point,E);
}
void drawAdd(int n1, int n2, pair decalN1, pair decalN2, pen colorN1, pen colorN2, align alignN1=NoAlign,align alignN2=NoAlign){
pair n1pair = pairRepresentation(n1);
pair n2pair = pairRepresentation(n2);
pair n1rep = n1pair + decalN1;
pair n2rep = n2pair + decalN2;
draw (pairToString(n2rep),n1rep -- n1rep+n2rep,alignN1,colorN1,Arrow,Margins);
draw (pairToString(n1rep),n2rep -- n1rep+n2rep,alignN2,colorN2,Arrow,Margins);
}
drawAdd(N1,N2,(2,2),(1,1),green,brown,alignN1=NW,alignN2=SW);
drawAdd(N1,N2,(1,1),(0,0),green,brown,alignN1=NW,alignN2=2SW);
// Vectors
// axes
real decay=-0.2;
ticks tick=RightTicks(N=0,n=1,end=false,nullString);
xaxis("$n_1$",YEquals(decay),decay,num+1.0,tick,Arrow);
yaxis("$n_2$",XEquals(decay),decay,num+1.0,LeftTicks(N=0,n=1,end=false,nullString),Arrow);
path legend = (num * .1, -1) -- (num*.9, -1);
label( Label(latexBold(N1),position=Relative(0.1)), Colors[N1]+fontsize(17),g=legend);
label( Label(latexBold("+"),position=Relative(0.2)) ,fontsize(17),g=legend);
label( Label(latexBold(N2),position=Relative(0.3)),Colors[N2]+fontsize(17),g=legend);
label( shift(0,-3.5)*Label(latexBold("{=}"),position=Relative(0.4)),fontsize(17),g=legend);
label( Label(latexBold(N2),position=Relative(0.5)),Colors[N2]+fontsize(17),g=legend);
label( Label(latexBold("+"),position=Relative(0.6)) ,fontsize(17),g=legend);
label( Label(latexBold(N1),position=Relative(0.7)),Colors[N1]+fontsize(17),g=legend);
label( shift(0,-3.5)*Label(latexBold("="),position=Relative(0.8)),fontsize(17),g=legend);
label( Label(latexBold(N1+N2),position=Relative(0.9)),Colors[N1+N2]+fontsize(17),g=legend);