//$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/apps/igeoportal-standard/branches/2.5_testing/modules/digitizer/geometries.js $
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2009 by:
 - Department of Geography, University of Bonn -
 and
 - lat/lon GmbH -

 This library is free software; you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation; either version 2.1 of the License, or (at your option)
 any later version.
 This library is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 details.
 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

 Contact information:

 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/

 Department of Geography, University of Bonn
 Prof. Dr. Klaus Greve
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/

 e-mail: info@deegree.org
----------------------------------------------------------------------------*/
function Point(x, y) {
	this.x = x;
	this.y = y;
	this.mute = 0.0001;
	this.equals = equals;
	
	function equals(point) {
		return this.x > point.x - this.mute && this.x < point.x + this.mute &&
			   this.y > point.y - this.mute && this.y < point.y + this.mute;
	}
}

function LineString(points) {
	this.points = points;
	this.getSize = getSize;
	this.length = length;
	
	function getSize() {
		return this.points.length;
	}
	
	function length() {
		var d = 0;
		for ( var i = 0; i < this.points.length-1; i++) {
			var x1 = this.points[i].x;
			var y1 = this.points[i].y;
			var x2 = this.points[i+1].x;
			var y2 = this.points[i+1].y;
			d = d + Math.sqrt( (x2 -x1 ) * (x2 -x1) +  (y2 -y1 ) * (y2 -y1) );
    	}
    	return d;
	}
}

function Polygon(points) {
	this.points = points;
	
	this.isValid = isValid;
	this.getSize = getSize;
	this.length = length;
	this.area = area;
	
	function getSize() {
		return this.points.length;
	}
	
	function isValid() {
		return this.points.length > 3 && this.points[0].equals( this.points[this.points.length-1] );
	}
	
	function length() {
		var d = 0;
		for ( var i = 0; i < this.points.length-1; i++) {
			var x1 = this.points[i].x;
			var y1 = this.points[i].y;
			var x2 = this.points[i+1].x;
			var y2 = this.points[i+1].y;
			d = d + Math.sqrt( (x2 -x1 ) * (x2 -x1) +  (y2 -y1 ) * (y2 -y1) );
    	}
    	
		if( this.points.length == 5 ){// when only two points, len must be corrected
			return d/2;
		}
		
    	
    	return d;
	}
	
	function area() {
		var atmp = 0;
		var i = 0;
		var j = 0;
		for ( i = this.points.length - 1, j = 0; j < this.points.length; i = j, j++ ) {
			var xi = this.points[i].x - this.points[0].x;
			var yi = this.points[i].y - this.points[0].y;
			var xj = this.points[j].x - this.points[0].x;
			var yj = this.points[j].y - this.points[0].y;
		    var ai = ( xi * yj ) - ( xj * yi );
		    atmp += ai;
		}

		return Math.abs( atmp / 2 );
	}
	
}
