//**************************************************************************
//    imapa_coord.js - rutiny pro práci se souřadnicemi (R_Coord)
//--------------------------------------------------------------------------
//    Datum vytvoření   : 2.6.2003
//    Správce           : Michal Bursa
//    Součást projektu  : iMapa
//--------------------------------------------------------------------------
//    Copyright (c) 2007  CDSw - City Data Software s.r.o.
//**************************************************************************
//    $Id: imapa_coord.js,v 1.6 2007-06-19 09:06:59 michal Exp $



//----------------------------------------------------------------------------
// TMapCoord
//----------------------------------------------------------------------------

var MapCoord = Class.create();
MapCoord.prototype = {
  initialize: function(y,x) {
  // konstruktor
    this.y = y;
    this.x = x;
  },

  toString: function() {
// zobrazí informaci o objektu (jako text)
	var tmp, xx, yy;
    tmp = Math.round(this.y*1000).toString();
	yy = tmp.substring(0,tmp.length-3)+"."+tmp.substring(tmp.length-2,tmp.length);
	tmp = Math.round(this.x*1000).toString();
	xx = tmp.substring(0,tmp.length-3)+"."+tmp.substring(tmp.length-2,tmp.length);
	return "["+yy+", "+xx+"]";
  },

  duplicate: function() {
	return new MapCoord(this.y, this.x);
}

};


function makeCoord(y, x) {
  return new MapCoord(y,x);
};


function makeCoordArray(coords) {
  var array = new Array();
  coords = eval(coords);  // should be in JSON format
  for (var i=0; i<coords.length; i++) array.push(new MapCoord(coords[i].y, coords[i].x));
  return array;
};


function makeSegmentLengths(coords) {
// makes an array of line segment lengths (float[])
// first element in the array is 0.0, last element is the total line length
    var lengths = new Array();
    var totallength = 0.0;
    lengths.push(0.0);
    for (var i=0; i<coords.length-1; i++) {
      totallength += Math.sqrt(
        Math.pow(coords[i].y-coords[i+1].y, 2) + 
        Math.pow(coords[i].x-coords[i+1].x, 2)
      );
      lengths.push(totallength);
    };
    for (var i=0; i<lengths.length; i++) lengths[i] /= totallength;
    return lengths;
};


function coordDistance(c1, c2) {
  return Math.sqrt( (c1.y-c2.y)*(c1.y-c2.y) + (c1.x-c2.x)*(c1.x-c2.x) );
};



//----------------------------------------------------------------------------
// TMapCoord
//----------------------------------------------------------------------------


function MapRect($y1, $x1, $y2, $x2, $angle) {
	this.y1 = $y1;
	this.x1 = $x1;
	this.y2 = $y2;
	this.x2 = $x2;
	this.angle = $angle;
}


//********************************************************
// zobrazí informaci o objektu (jako text)
// základní metoda každého objektu
// př.: alert(maprect);

MapRect.prototype.toString=function() {
	var c1 = new MapCoord(this.y1, this.x1);
	var c2 = new MapCoord(this.y2, this.x2);
	return "P1: "+c1+"\n"+"P2: "+c2+"\n"+"angle: "+Math.round(this.angle/Math.PI*180)+" deg";
}


//********************************************************
// vrací kopii TMapRect

MapRect.prototype.duplicate=function() {
	return new MapRect(this.y1, this.x1, this.y2, this.x2, this.angle);
}


//********************************************************
// vrací střed TMapRect

MapRect.prototype.center=function() {
	return new MapCoord( (this.y1+this.y2)/2.0, (this.x1+this.x2)/2.0 );
}


//********************************************************
// přetočí MapRect do novéhlo úhlu

MapRect.prototype.setAngle=function($angle) {
	// střed obdélníku
	var c = this.center();
	// posunout do počátku
	this.y1 -= c.y;
	this.y2 -= c.y;
	this.x1 -= c.x;
	this.x2 -= c.x;

	// přetočit
	if ((-this.angle+$angle) != 0.0) {
        var T, tmp_x, tmp_y;
		T = new Array(2); T[0] = new Array(2); T[1] = new Array(2);
		T[0,0] = +Math.cos(-this.angle+$angle);
		T[0,1] = +Math.sin(-this.angle+$angle);
		T[1,0] = -Math.sin(-this.angle+$angle);
		T[1,1] = +Math.cos(-this.angle+$angle);
		tmp_y = T[0,0]*this.y1 + T[0,1]*this.x1;
		tmp_x = T[1,0]*this.y1 + T[1,1]*this.x1;
		this.y1 = tmp_y;
		this.x1 = tmp_x;
		tmp_y = T[0,0]*this.y2 + T[0,1]*this.x2;
		tmp_x = T[1,0]*this.y2 + T[1,1]*this.x2;
		this.y2 = tmp_y;
		this.x2 = tmp_x;
	}
  
	// posunout zpět do původního středu
	this.y1 += c.y;
	this.y2 += c.y;
	this.x1 += c.x;
	this.x2 += c.x;
	this.angle = $angle;
}


//********************************************************
// vrací šířku TMapRect

MapRect.prototype.width=function() {
	var tmp = this.duplicate();
	tmp.setAngle(0.0);
	return Math.abs(tmp.y1-tmp.y2);
}


//********************************************************
// vrací výšku TMapRect

MapRect.prototype.height=function() {
	var tmp = this.duplicate();
	tmp.setAngle(0.0);
	return Math.abs(tmp.x1-tmp.x2);
}


//********************************************************
// vrací bod 1 (right-top)

MapRect.prototype.P1=function() {
	return new MapCoord(this.y1, this.x1);
}


//********************************************************
// vrací bod 2 (left-bottom)

MapRect.prototype.P2=function() {
	return new MapCoord(this.y2, this.x2);
}


//********************************************************
// vrací úhel v intervalu 0..2*Pi

function baseAngle($angle) {
	$angle = ($angle % (2*Math.PI));
	if ($angle < 0.0) $angle += 2*Math.PI;
	return $angle;
}

