point.js

Summary

No overview generated for 'point.js'


Class Summary
Point A class representing a two-dimensional point.

/* 
* $Id: point.js 1389 2007-05-30 12:09:56Z 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

/**
 * @class A class representing a two-dimensional point.
 *
 * @constructor
 * @param {Float} x x value of the {@link Point}
 * @param {Float} y y value of the {@link Point}
 */
 function Point(x, y){
 	/**
 	 * x value of the {@link Point}
 	 *
	 * @type Float
	 */
	this.x = parseFloat(x);
 	/**
 	 * y value of the {@link Point}
	 *
	 * @type Float
	 */
	this.y = parseFloat(y);
}
/**
 * computes the distance between a {@link Point} p and this {@link Point}
 *
 * @param {Point} p the distance between this {@link Point} and the {@link Point} p is computed.
 * @return {Float} the distance between the two {@link Point} objects.
 */
Point.prototype.dist = function(p){
	return Math.sqrt(Math.pow(this.y-p.y,2) + Math.pow(this.x-p.x,2)) ;
}
/**
 * checks if the coordinates of this {@link Point} match the coordinates of a {@link Point} p
 *
 * @param {Point} p 
 * @return {Boolean} true if the two points are equal; elso false
 */
Point.prototype.equals = function(p){
	if (this.x == p.x && this.y == p.y) {return true;}
	return false;
}
/**
 * subtracts a {@link Point} p from this {@link Point}
 *
 * @param {Point} p 
 * @return a new {@link Point} with the difference of the two points
 */
Point.prototype.minus = function(p){
	return new Point(this.x-p.x, this.y-p.y);
}
/**
 * adds this {@link Point} to a {@link Point} p
 *
 * @param {Point} p 
 * @return a new {@link Point} with the sum of the two points
 */
Point.prototype.plus = function(p){
	return new Point(this.x+p.x, this.y+p.y);
}
/**
 * divides this {@link Point} by a scalar c
 *
 * @param {Float} c divisor
 * @return a new {@link Point} divided by c
 */
Point.prototype.dividedBy = function(c){
	if (c != 0) {
		return new Point(this.x/c, this.y/c);
	}
	var e = new Mb_exception("Point.dividedBy: Division by zero");
	return false;
}
/**
 * multiplies this {@link Point} by a scalar c
 *
 * @param {Float} c factor
 * @return a new {@link Point} multiplied by c
 */
Point.prototype.times = function(c){
	return new Point(this.x*c, this.y*c);
}
/**
 * rounds the coordinates to numOfDigits digits
 *
 * @param numOfDigits the coordinate will be rounded to numOfDigits digits
 * @return a new {@link Point} rounded to numOfDigits digits
 * @type Point
 */
Point.prototype.round = function(numOfDigits){
	return new Point(roundToDigits(this.x, numOfDigits), roundToDigits(this.y, numOfDigits));
}
/**
 * @returns a {String} representation of this Point
 * @type String
 */
Point.prototype.toString = function(){
	return "(" + this.x + ", " + this.y + ")";
}


//------------------------------------------------------------------------
// possible improvement: point has flag: map OR real. additional functions: toReal, toMap
/**
 * @ignore
 */
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]);
}
/**
 * @ignore
 */
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 {
		var e = new Mb_exception('Point.realToMap:where am i?');
	}
	return new Point(v[0], v[1]);
}
/**
 * @ignore
 */
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 {
		var e = new Mb_exception('Point.mb_calcExtent: 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;
}
/**
 * @ignore
 */
function roundToDigits(aFloat, numberOfDigits) {
	return Math.round(aFloat*Math.pow(10, parseInt(numberOfDigits)))/Math.pow(10, parseInt(numberOfDigits));
}


Documentation generated by JSDoc on Wed Aug 8 10:35:22 2007