

function Point(x,y,xg,yg){
    this.x=x;
    this.y=y;
    this.x0=x;
    this.y0=y;
    this.xg=xg;
    this.yg=yg; 
    this.id='';  
    return this;
}

function getX(){
    return this.x;
}
Point.prototype.getX=getX;
function getY(){
    return this.y;
}
Point.prototype.getY=getY;

function drawPoint(g){    
    g.fillRect((this.x)-4, this.y-4, 8, 8); 
    g.paint();    
}
Point.prototype.drawPoint=drawPoint;

function getWktPoint(g){
    return 'POINT('+this.xg+' '+this.yg+')';
}


function movePoint(xm,ym){
    //alert(xm+' '+ym);
    this.x=this.x+xm;
    this.y=this.y+ym;
}

function isClose(p){
    //alert((this.getX())+" "+p.getX()+' !! '+(Math.abs(this.getY()-p.getY())));
    if((Math.abs(this.getX()-p.getX())<=6) && (Math.abs(this.getY()-p.getY())<=6)){
        return true;
    }
}

Point.prototype.getWkt=getWktPoint;
Point.prototype.movePoint=movePoint;
Point.prototype.isClose=isClose;


//-----------------------------------------

function Line(){
    this.listPoint=new Array();
    this.updating=false;
    this.id='';
}
function isUpdating(){return this.updating;}
function setUpdating(u){this.updating=u;}
function getID(){return this.id;}
function setID(i){this.id=i;}

function addPoint(p){
    //alert('addp');
    //this.listPoint[this.listPoint.length]=p;
    this.listPoint.push(p);
}
function drawLastLine(g){
    //alert(this.getString());
    var len=this.listPoint.length

    if(len>1){
        var p0=this.listPoint[len-1];
        p0.drawPoint(g);
        var p1=this.listPoint[len-2];
        g.drawLine(p0.x, p0.y, p1.x, p1.y);
    }
    else if(len==1){
        this.listPoint[0].drawPoint(g);
    }
    g.paint();
}


function getWktPointlin(){
    var wkt='';
    var len=this.listPoint.length;
    if(len>0){
        wkt='MULTIPOINT(('+this.listPoint[0].xg+' '+this.listPoint[0].yg;
        wkt+='))';
    }
    return wkt;
}

function getWktLine(){
    var wkt='';
    var len=this.listPoint.length
    if(len>0){
        wkt='MULTILINESTRING(('+this.listPoint[0].xg+' '+this.listPoint[0].yg;
        for(n=1; n<len; n++){
            var p=this.listPoint[n];
            wkt+=','+p.xg+' '+p.yg;
        }
        wkt+='))';
    }
    return wkt;
}


function getWktPolygon(){
    var wkt='';
    var len=this.listPoint.length
    if(len>0){
        var p0=this.listPoint[0];
        wkt='MULTIPOLYGON(((     '+p0.xg+' '+p0.yg;
        for(n=1; n<len; n++){
            var p=this.listPoint[n];
            wkt+=', '+p.xg+' '+p.yg;
        }
        wkt+=',     '+p0.xg+' '+p0.yg;

        wkt+=')))';
    }
    return wkt;
}

function addTemp(g,x,y){
    //alert(this.getString());
    g.clear();
    var len=this.listPoint.length
    if(len>0){
        //g.clear();
        var p0=this.listPoint[len-1];
        g.drawLine(p0.x, p0.y, x, y);
        g.paint();
    }
}

function getString(){
    var ret;
    for(n=0; n<this.listPoint.length; n++){
        var p=this.listPoint[n];
        ret+='|'+n+' '+p.getX()+' '+p.y;
    }
    return ret;
}

function move(xm,ym){
    var len=this.listPoint.length;
    //alert('muovi '+xm+' '+ym);
    for(x=0; x<len; x++){
        //alert('muovi '+x+' ');
        try{
            var p=this.listPoint[x];
            p.movePoint(xm,ym);
        }
        catch(e){alert(e);}
    }
}

function draw(g){
    for(n=0; n<this.listPoint.length; n++){
        var p=this.listPoint[n];
        this.listPoint[n].drawPoint(g);
        if(n>0){
            var p0=this.listPoint[n-1];
            g.drawLine(p.x, p.y, p0.x, p0.y);
        }
    }
    g.paint(); 
}

function drawPolygon(g){
    this.draw(g);
    var pprimo=this.listPoint[0];
    var pultimo=this.listPoint[(this.listPoint.length)-1];    
    //alert(pultimo.x+' '+pultimo.y+' '+pprimo.x+' '+pprimo.y);
    g.drawLine(pultimo.x, pultimo.y, pprimo.x, pprimo.y);  
    g.paint(); 
}

function getNumPoint(){
    return this.listPoint.length;
}

function getVertex(ps){
    for(n=0; n<this.listPoint.length; n++){
        var p=this.listPoint[n];
        if(p.isClose(ps)){
            return ps;
        }
    }
}

function addVertex(ps){

    var mod=false;
    var newListPoint=new Array();
    if(this.listPoint.length>0){
        newListPoint.push(this.listPoint[0]);
        

        for(n=1; n<this.listPoint.length; n++){
            var p1=this.listPoint[n-1];
            var p2=this.listPoint[n];
            if(isBetween(ps,p1,p2)){
                newListPoint.push(ps);
                mod=true;
            }
            newListPoint.push(p2);
        }
        if(mod){
            this.listPoint=newListPoint;
        }
    }
    return mod;
}


function isBetween(p3, p1, p2){
    var u=((p3.getX()-p1.getX())*(p2.getX()-p1.getX())-(p3.getY()-p1.getY())*(p2.getY()-p1.getY()))/distSquare(p1,p2);
    pnx=p1.getX()+u*(p2.getX()-p1.getX());
    pny=p1.getY()+u*(p2.getY()-p1.getY());

    var px=new Point(pnx,pny,0,0);
    if(p3.isClose(px)){
        return true;
    }
    else{
        return false;
    }
}

function distSquare(p1,p2){
    return Math.pow((p1.getX()-p2.getX()),2)-Math.pow((p1.getY()-p2.getY()),2);
}

function moveVertex(rec){
    var moved=false;
    var ps=rec.getStartPoint();
    //alert(this.listPoint.length);
    for(n=0; n<this.listPoint.length; n++){
        var p=this.listPoint[n];
        if(p.isClose(ps)){            
            this.listPoint[n]=rec.getEndPoint();
            moved=true;
            break;
        }
    }
    return moved;
}

Line.prototype.isUpdating=isUpdating;
Line.prototype.setUpdating=setUpdating;
Line.prototype.setID=setID;
Line.prototype.getID=getID;
Line.prototype.getWkt=getWktLine;
Line.prototype.drawLastLine=drawLastLine;
Line.prototype.getNumPoint=getNumPoint;
Line.prototype.addPoint=addPoint;
Line.prototype.getString=getString;
Line.prototype.addTemp=addTemp;
Line.prototype.move=move;
Line.prototype.draw=draw;
Line.prototype.drawLine=draw;
Line.prototype.drawPolygon=drawPolygon;
Line.prototype.getWktPolygon=getWktPolygon;
Line.prototype.getWktPoint=getWktPointlin;
Line.prototype.getVertex=getVertex;
Line.prototype.moveVertex=moveVertex;
Line.prototype.addVertex=addVertex;

//_____________________________________


function Rectangle (inizio, base_top, base_left){
    this.inizio=inizio;
    this.fine=inizio;
    this.base_left=base_left;
    this.base_top=base_top;
    return this;
}

function setFine(fine){
    this.fine=fine;
    this.minx=Math.min(this.inizio.x,this.fine.x);
    this.miny=Math.min(this.inizio.y,this.fine.y);
    this.maxx=Math.max(this.inizio.x,this.fine.x);
    this.maxy=Math.max(this.inizio.y,this.fine.y);    
}

function getStartPoint(){
    return this.inizio;
}
function getEndPoint(){
    return this.fine;
}

function moveDiv(div){    

    div.style.left=this.minx+'px';
    div.style.top=this.miny+'px';
    div.style.width=(this.maxx-this.minx)+'px';
    div.style.height=(this.maxy-this.miny)+'px';
    div.style.display='';
}
function getDifX(){
    return this.fine.x-this.inizio.x;
}
function getDifY(){
    return this.fine.y-this.inizio.y;
}


function getWidthRect(){
    return Math.abs(this.inizio.x-this.fine.x);
}

function getHeightRect(){
    return Math.abs(this.inizio.y-this.fine.y);
}

function stampaRect(){
    return this.inizio.x+' '+this.inizio.y+' '+this.fine.x+' '+this.fine.y;
}

Rectangle.prototype.setFine=setFine;
Rectangle.prototype.getStartPoint=getStartPoint;
Rectangle.prototype.getEndPoint=getEndPoint;
Rectangle.prototype.moveDiv=moveDiv;
Rectangle.prototype.stampa=stampaRect;
Rectangle.prototype.getWidth=getWidthRect;
Rectangle.prototype.getHeight=getHeightRect;
Rectangle.prototype.getDifX=getDifX;
Rectangle.prototype.getDifY=getDifY;






