/* 
* $Id: point.js 1138 2007-02-23 11:56:03Z christoph $
* COPYRIGHT: (C) 2001 by ccgis. This program is free software under the GNU General Public
* License (>=v2). Read the file gpl.txt that comes with Mapbender for details. 
*/
//http://www.mapbender.org/index.php/point.js
function Point(x, y){
	this.x = x;
	this.y = y;
}

Point.prototype.dist = function(p){
	return Math.sqrt(Math.pow(this.y-p.y,2) + Math.pow(this.x-p.x,2)) ;
}
Point.prototype.equals = function(p){
	if (this.x == p.x && this.y == p.y) {return true;}
	return false;
}
Point.prototype.minus = function(p){
	return new Point(this.x-p.x, this.y-p.y);
}
Point.prototype.plus = function(p){
	return new Point(this.x+p.x, this.y+p.y);
}
Point.prototype.dividedBy = function(c){
	if (c != 0) {return new Point(this.x/c, this.y/c);}
	alert("Exception: Division by zero");
	return false;
}
Point.prototype.times = function(c){
	return new Point(this.x*c, this.y*c);
}
Point.prototype.round = function(numOfDigits){
	return new Point(roundToDigits(this.x, numOfDigits), roundToDigits(this.y, numOfDigits));
}
Point.prototype.toString = function(){
	return "(" + this.x + ", " + this.y + ")";
}


//------------------------------------------------------------------------
// possible improvement: point has flag: map OR real. additional functions: toReal, toMap

function mapToReal(frameName, aPoint) {
	var v;
	if (typeof(mb_mapObj) == 'object') v = makeClickPos2RealWorldPos(frameName, aPoint.x, aPoint.y);
	else if (typeof(parent.mb_mapObj) == 'object') v = parent.makeClickPos2RealWorldPos(frameName, aPoint.x, aPoint.y);
	else alert('where am i?');
	return new Point(v[0], v[1]);
}
function realToMap(frameName, aPoint) {
	var v;
	if (typeof(mb_mapObj) == 'object') v = makeRealWorld2mapPos(frameName, aPoint.x, aPoint.y);
	else if (typeof(parent.mb_mapObj) == 'object') v = parent.makeRealWorld2mapPos(frameName, aPoint.x, aPoint.y);
	else alert('where am i?');
	return new Point(v[0], v[1]);
}
function mb_calcExtent(frameName, min, max) {
	var ind;
	if (typeof(mb_mapObj) == 'object') ind = getMapObjIndexByName(frameName);
	else if (typeof(parent.mb_mapObj) == 'object') ind = parent.getMapObjIndexByName(frameName);
	else alert('function getMapObjIndexByName not found');
	var extent = max.minus(min);
	var center = extent.dividedBy(2).plus(min);
	
	var relation_px_x = mb_mapObj[ind].width / mb_mapObj[ind].height;
	var relation_px_y = mb_mapObj[ind].height / mb_mapObj[ind].width;
	var relation_bbox_x = extent.x / extent.y;     
	
	var new_min;
	var new_max;
	
	if(relation_bbox_x <= relation_px_x){                
		new_min = new Point(center.x - relation_px_x * extent.y / 2, min.y);
		new_max = new Point(center.x + relation_px_x * extent.y / 2, max.y);
	}
	else if(relation_bbox_x > relation_px_x){                
		new_min = new Point(min.x, center.y - relation_px_y * extent.x / 2);
		new_max = new Point(max.x, center.y + relation_px_y * extent.x / 2);
	}
	mb_mapObj[ind].extent = new_min.x +","+ new_min.y +","+ new_max.x  +","+ new_max.y;
}
function roundToDigits(aFloat, numberOfDigits) {
	return Math.round(aFloat*Math.pow(10, parseInt(numberOfDigits)))/Math.pow(10, parseInt(numberOfDigits));
}
