/////////////////////////////////////////////////////////////////////
function _Point(cX, cY)
{
	if(cX==null) 
		cX=0;
		
	if(cY==null)
		cY=0;
		
	this.X=cX;
	this.Y=cY;
}
/////////////////////////////////////////////////////////////////////
function _Line()
{
	this.StartPoint = new _Point(0,0);
	this.EndPoint = new _Point(0,0);
	this.GetLength = _DoLineCalculateLength;
}
function _DoLineCalculateLength()
{
	var dX = this.StartPoint.X - this.EndPoint.X;
	var dY = this.StartPoint.Y - this.EndPoint.Y;
	
	return Math.sqrt( dX*dX + dY*dY );
}
function GetIntersectPoint(Line1, Line2)
{
	var ua_t;
	var ub_t;
	var u_b; 
	var ua;
	var ub;
	var MyPoint = null;

	ua_t = (Line2.EndPoint.X - Line2.StartPoint.X) * (Line1.StartPoint.Y - Line2.StartPoint.Y) - (Line2.EndPoint.Y - Line2.StartPoint.Y) * (Line1.StartPoint.X - Line2.StartPoint.X);
	ub_t = (Line1.EndPoint.X - Line1.StartPoint.X) * (Line1.StartPoint.Y - Line2.StartPoint.Y) - (Line1.EndPoint.Y - Line1.StartPoint.Y) * (Line1.StartPoint.X - Line2.StartPoint.X);
	u_b  = (Line2.EndPoint.Y - Line2.StartPoint.Y) * (Line1.EndPoint.X - Line1.StartPoint.X) - (Line2.EndPoint.X - Line2.StartPoint.X) * (Line1.EndPoint.Y - Line1.StartPoint.Y);

	if (u_b != 0)
	{
		ua = ua_t/u_b;
		ub = ub_t/u_b;

		if (0 <= ua && ua <= 1 && 0 <= ub && ub <= 1)
		{
			MyPoint = new _Point(-1,-1);
			MyPoint.X = Line1.StartPoint.X + ua * (Line1.EndPoint.X - Line1.StartPoint.X);
			MyPoint.Y = Line1.StartPoint.Y + ua * (Line1.EndPoint.Y - Line1.StartPoint.Y);
		}
	}
    return MyPoint;
}
/////////////////////////////////////////////////////////////////////
function _Polyline()
{
	// have the array of points
	this.GetLength = _DoPolyLineCalculateLength;
}
function _DoPolyLineCalculateLength()
{
	return -1;
}
/////////////////////////////////////////////////////////////////////
function _Polygon()
{
	// have the array of points
	// be carefull with these two
	// since you have to repeat first two for the area calculation
	// but not for the length calculation!
	this.m_pointCount = 0;
	this.pointsArray = new Array();

	// implement these two
	this.AddPoint = null;
	this.RemovePoints = null;
	
	this.GetLength = _DoPolygonCalculateLength;
	this.GetArea = _DoPolygonCalculateArea;
	this.IsPointIsnide = _DoPolygonIsPointIsnide;
}
function _DoPolygonCalculateLength()
{
	return -1;
}
function _DoPolygonCalculateArea()
{
	// create new array with n+2 elements and duplicate first two points
	var dblReturnValue;
	var dblArea=0.0;
	var i;

	pointCount = this.m_pointCount;
	//pointsArray = make the copy of this.pointsArray and plus add this.pointsArray[0] and this.pointsArray[1]
	
	for (i=1;i<=pointCount;i++)
	{
		dblArea = dblArea + pointsArray[i].X * (pointsArray[i + 1].Y - pointsArray[i - 1].Y);
	}

	dblReturnValue = dblArea / 2.0 ;

	return dblReturnValue;
}
// pt - should be _Point
function _DoPolygonIsPointInside(pt)
{
	// Is pt.X and pt.Y in this polygon
}
/////////////////////////////////////////////////////////////////////
// delete this function after implementing _Polygon.
function isPointInsideRange(point, left, top, right, bottom)
{
	return (point.X >= left && point.X <= right && point.Y >= bottom && point.Y <= top)
}
// delete this function after implementing _Polygon.
function AreaCalculate(pointCount, pointsArray){

        var dblReturnValue;
        var intAreaSign;
        var dblArea=0.0;
        var i;

		for (i=0;i<pointCount;i++){
			dblArea = dblArea + (pointsArray[i + 1].X - pointsArray[i].X) * (pointsArray[i + 1].Y + pointsArray[i].Y) / 2
		}
				
		dblReturnValue = Math.abs(dblArea);

        return dblReturnValue;
}
	
function roundNumber(number, rlength) {
	var newnumber = Math.round(number*Math.pow(10,rlength))/Math.pow(10,rlength);
	return newnumber;
}

