function Geometry() {
    
    Geometry.prototype.isInEnvelope = function(x, y, minX, minY, maxX, maxY) {
        return (x >= minX && x <= maxX && y >= minY && y <= maxY);
    }
    
    Geometry.prototype.isSimplePolygon = function(coordList) {
        
        var length = coordList.length;
        var isSimplePolygon = true;
        
        if (length > 3) {
        
            coordList.push(new Array(coordList[0][0], coordList[0][1]));
            length = coordList.length;
            
            for (var i=0; i<length-1; i++) {
				if (this.checkAnyLinesIntersect(coordList, coordList[i][0], coordList[i][1], coordList[i+1][0], coordList[i+1][1]) > -1) {
					isSimplePolygon = false;
					break;
				}
			}
			coordList.pop();
        }
        
        return isSimplePolygon;
    }
    
    Geometry.prototype.getIntersectionIndex = function(coordList, x1, y1, x2, y2) {
        return this.checkAnyLinesIntersect(coordList, x1, y1, x2, y2);
    }
		
    Geometry.prototype.checkAnyLinesIntersect = function(coordList, pntX1,pntY1,pntX2,pntY2) {

	    var pntX3 = 0;
	    var pntY3 = 0;
	    var pntX4 = 0;
	    var pntY4 = 0;
	    
	    var length = coordList.length - 1;

	    for (var i=0; i<length; i++) {
		    pntX3 = coordList[i][0];
		    pntY3 = coordList[i][1];
		    pntX4 = coordList[i+1][0];
		    pntY4 = coordList[i+1][1];
		    if (this.doLinesIntersect(pntX1,pntY1,pntX2,pntY2,pntX3,pntY3,pntX4,pntY4)) {
			    return i; //There is an intersection
		    }
	    }
	    return -1; //No lines intersect.
    }

    Geometry.prototype.doLinesIntersect = function(pntX1,pntY1,pntX2,pntY2,pntX3,pntY3,pntX4,pntY4){
    		
	    var UaNumerator = (pntX4 - pntX3)*(pntY1 - pntY3) - (pntY4 - pntY3)*(pntX1 - pntX3);
	    var UbNumerator = (pntX2 - pntX1)*(pntY1 - pntY3) - (pntY2 - pntY1)*(pntX1 - pntX3);
	    var Demoniator = (pntY4 - pntY3)*(pntX2 - pntX1) - (pntX4 - pntX3)*(pntY2 - pntY1);

	    var Ua = UaNumerator/Demoniator;
	    var Ub = UbNumerator/Demoniator;

	    if ((Ua > 0 && Ua < 1) && (Ub > 0 && Ub < 1)) {
		    //The lines intersect hence not a simple polygon
		    //Extra code to calculate intersection point, but we want to just return message
		    //var theIntersectX = pntX1 + Ua*(pntX2 - pntX1);
		    //var theIntersectY = pntY1 + Ua*(pntY2 - pntY1);
		    return true; //Line does intersect.
	    }		
	    return false; //Line does not intersect.
    }
    
    
    Geometry.prototype.getLineLength = function(coordList) {
        
        var total = 0;
        var length = coordList.length;
        var x1;
        var y1;
        var x2;
        var y2;
        
        if (length > 1 && coordList[0] &&  coordList[1]) {
            for (var i=1; i<length; i++) {
                x1 = coordList[i - 1][0];
                y1 = coordList[i - 1][1];
                x2 = coordList[i][0];
                y2 = coordList[i][1];
                
                total += this.getLineLengthXY(x1, y1, x2, y2);            
            }
        }
        
        return total;
    }
    
    Geometry.prototype.getLineLengthXY = function(x1, y1, x2, y2) {
    
        return Math.sqrt(Math.pow(Math.abs(x2 - x1),2) + Math.pow(Math.abs(y2 - y1),2));
    }
    
    Geometry.prototype.getPolygonArea = function(coordList) {
        
        var area = 0;
        var length = coordList.length;
        var x1;
        var y1;
        var x2;
        var y2;
        
        if (length > 2 && coordList[0] &&  coordList[1] && coordList[2]) {
            coordList.push(new Array(coordList[0][0], coordList[0][1]));
            
            for (var i=0; i<length; i++) {
                x1 = coordList[i][0];
                y1 = coordList[i][1];
                x2 = coordList[i + 1][0];
                y2 = coordList[i + 1][1];
                area += this.calcTrapesiumArea(x1, y1, x2, y2);
            }
            coordList.pop();
        }
        return area;
    }
    
    Geometry.prototype.calcTrapesiumArea = function(x1, y1, x2, y2) {
        
        var area = 0;
        area = ((Math.abs(y2-y1) / 2.0) * Math.abs(x2-x1)) + (Math.abs(x2-x1) * Math.min(y2, y1));
        return x1 > x2 ? -area : area
    }
    
    Geometry.prototype.getPolygonPerimeter = function(coordList) {
        
        var perimeter = 0;
        
        if (coordList.length > 2 && coordList[0] &&  coordList[1] && coordList[2]) {
            coordList.push(new Array(coordList[0][0], coordList[0][1]));
            perimeter = this.getLineLength(coordList);
            coordList.pop();
        }
        return perimeter;
     }
     
     Geometry.prototype.getRectangleArea = function(minX, minY, maxX, maxY) {
        
        var xDiatnace = maxX - minX;
        var yDiatnace = maxY - minY;
        return xDiatnace * yDiatnace;
     }
     
     Geometry.prototype.getRectanglePerimeter = function(minX, minY, maxX, maxY) {
        
        var xDiatnace = maxX - minX;
        var yDiatnace = maxY - minY;
        return (xDiatnace * 2) + (yDiatnace * 2);
     }
     
     Geometry.prototype.getCircleCircumference = function(radius) {
      
        return 2 * Math.PI * radius;
     }
     
     Geometry.prototype.getCircleArea = function(radius) {
        
        return Math.PI * radius * radius;
     }
}