function grlRegion(n,s,w,e,cols,rows) {
 this.n=n; this.s=s; this.w=w; this.e=e;
 this.N=n; this.S=s; this.W=w; this.E=e;
 this.cols=cols; this.rows=rows;
 this.side=1;
 this.scale=1;
 this.getScale();
 this.getSide();
 this.max_scale=10000000000;
 this.min_scale=0;
 this.max_side=10000000000;
 this.min_side=0;
}

grlRegion.prototype.setRegion=function(n, s, e, w) {
 this.n=Number(n); this.s=Number(s); this.e=Number(e); this.w=Number(w);
 if(this.n == this.s) this.n += 1;
 if(this.e == this.w) this.w += 1;
 this.getScale();
 this.getSide();
}

grlRegion.prototype.setCanvas=function(width, height) {
 this.cols = Number(width); this.rows = Number(height);
 this.getScale();
 this.getSide(); 
}

grlRegion.prototype.setRange=function(min, max) {
 this.min_scale=Number(min);
 this.max_scale=Number(max);
}

grlRegion.prototype.setLimits=function(min, max) {
 this.min_side=Number(min);
 this.max_side=Number(max);
}

grlRegion.prototype.setRatio=function() {
 n=this.n; s=this.s; e=this.e; w=this.w;
 cols=this.cols; rows=this.rows;
 if(rows <2 || cols <2) return;

 if(this.n == this.s) this.n++;
 if(this.e == this.w) this.e++;



 if((n - s) / (e - w)>rows / cols) {
  margin=0.5 * ((cols * (n - s)) / rows - (e - w));
  this.w-=margin; this.e+=margin;
 } else {
  margin=0.5 * ((rows * (e - w)) / cols - (n - s));
  this.n+=margin; this.s-=margin;
 }
 this.getScale();
 this.getSide();
 if(this.scale < this.min_scale) this.setScale(this.min_scale);
 if(this.scale > this.max_scale) this.setScale(this.max_scale);
 if(this.side < this.min_side) this.setScale(this.min_side);
 if(this.side > this.max_side) this.setScale(this.max_side);
 this.getScale();
 this.getSide();
}

grlRegion.prototype.getScale=function() {
  this.scale=Math.round((pt_x_cm*(this.n-this.s)*100)/this.rows);
  return this.scale;
}
grlRegion.prototype.getSide=function() {
  var ns = this.n-this.s;
  var ew = this.e-this.w;
  if(ns>ew) this.side=ew; else this.side=ns;
  return this.side;
}


grlRegion.prototype.setScale=function(r) {
 var T = (this.scale/r - 1)/(2*(this.scale/r));
 var ew=this.e - this.w;
 var ns=this.n - this.s;
 this.n-=ns*T; this.s+=ns*T; this.e-=ew*T; this.w+=ew*T;
 this.getScale();
 this.getSide();
}

grlRegion.prototype.setSide=function(l) {
  this.zoom(l/this.side);
}

grlRegion.prototype.zoom=function(t) {
 this.setScale(this.scale*t);
}

grlRegion.prototype.pan=function(dx, dy) {
 var dN=0,dS=0,dE=0,dW=0;
 dN=dS=-dy*(this.n-this.s)/this.rows;
 dE=dW=dx*(this.e-this.w)/this.cols;
 this.n+=dN; this.s+=dS; this.e+=dE; this.w+=dW;
}

grlRegion.prototype.center=function(x, y) {
 var dN=0,dS=0,dE=0,dW=0;
 dN=dS=(y-this.rows/2)*(this.n-this.s)/this.rows;
 dE=dW=(x-this.cols/2)*(this.e-this.w)/this.cols;
 this.n+=dN; this.s+=dS; this.e+=dE; this.w+=dW;
}

grlRegion.prototype.setCenter=function(e, n) {
 var dN=0,dS=0,dE=0,dW=0;
 dN=dS=n-(this.n+this.s)/2;
 dE=dW=e-(this.e+this.w)/2;
 this.n+=dN; this.s+=dS; this.e+=dE; this.w+=dW;
}

grlRegion.prototype.toE=function(x) {
 return this.w+x*(this.e-this.w)/this.cols;
}
grlRegion.prototype.toN=function(y) {
 return this.s+y*(this.n-this.s)/this.rows;
}

grlRegion.prototype.toX=function(e) {
 return (e - this.w)*this.cols/(this.e-this.w);
}
grlRegion.prototype.toY=function(n) {
 return (n - this.s)*this.rows/(this.n-this.s);
}

