function Point (_x, _y) {
	this.x = _x;
	this.y = _y;
}

function getLine (_points) {
	if (_points.length != 2) {
		return false;
	}
	var points = new Array();
	if (Math.abs(_points[1].x-_points[0].x) > Math.abs(_points[1].y-_points[0].y)) {
		var accu = _points[0].y;
		if (_points[1].x > _points[0].x) {
			for (var x = _points[0].x; x < _points[1].x+1; x++) {
				y = Math.floor(accu);
				accu += (_points[1].y - _points[0].y) / (_points[1].x - _points[0].x);
				points.push(new Point(x,y));
			}
		} else if (_points[0].x > _points[1].x) {
			for (var x = _points[0].x; x > _points[1].x-1; x--) {
				y = Math.floor(accu);
				accu -= (_points[1].y - _points[0].y) / (_points[1].x - _points[0].x);
				points.push(new Point(x,y));
			}
		} else {
			if (_points[1].y > _points[0].y) {
				for (var y=_points[0].y; y<_points[1].y+1; y++) {
					points.push(new Point(_points[0].x,y))
				}
			} else {
				for (var y=_points[0].y; y>_points[1].y-1; y--) {
					points.push(new Point(_points[0].x,y))
				}
			}
		}
	} else {
		var accu = _points[0].x;
		if (_points[1].y > _points[0].y) {
			for (var y = _points[0].y; y < _points[1].y+1; y++) {
				x = Math.floor(accu);
				accu += (_points[1].x - _points[0].x) / (_points[1].y - _points[0].y);
				points.push(new Point(x,y));
			}
		} else if (_points[0].y > _points[1].y) {
			for (var y = _points[0].y; y > _points[1].y-1; y--) {
				x = Math.floor(accu);
				accu -= (_points[1].x - _points[0].x) / (_points[1].y - _points[0].y);
				points.push(new Point(x,y));
			}
		} else {
			if (_points[1].x > _points[0].x) {
				for (var x=_points[0].x; x<_points[1].x+1; x++) {
					points.push(new Point(x,_points[0].y))
				}
			} else {
				for (var x=_points[0].x; x>_points[1].x-1; x--) {
					points.push(new Point(x,_points[0].y))
				}
			}
		}
	}
	return points;
}

function getPolygon (_points) {
	if (_points.length < 3) {
		return false;
	}
	var ret = new Array();
	for (var i=0; i<_points.length; i++) {
		if ((i+1) == _points.length) {
			ret = ret.concat(getLine(new Array(_points[i], _points[0])));
			ret.pop();
		} else {
			ret = ret.concat(getLine(new Array(_points[i], _points[i+1])));
			ret.pop();
		}
	}
	return ret;
}

function inPolygon (_polygon, _point) {
	var testUp = false;
	var testDn = false;
	var testLt = false;
	var testRt = false;
	for (var i=0; i<_polygon.length; i++) {
		if (_polygon[i].x == _point.x && _polygon[i].y > _point.y)
			testUp = true;
		if (_polygon[i].x == _point.x && _polygon[i].y < _point.y)
			testDn = true;
		if (_polygon[i].y == _point.y && _polygon[i].x > _point.x)
			testLt = true;
		if (_polygon[i].y == _point.y && _polygon[i].x < _point.x)
			testRt = true;
	}
	return (testUp && testDn && testLt && testRt);
}

function inPolygon2 (_points, _point) {
	try {
		if (_points.length < 3) throw "_points must contain at least 3 Point!";
		var sum = new Array();
		for (var i=0; i<_points.length; i++) {
//			sum.push(Math.atan2(_points[i].x-_point.x,_points[i].y-_point.y)/Math.PI*180);
			sum.push(Math.atan2(_points[i].y-_point.y, _points[i].x-_point.x)/Math.PI*180);
		}
		var sum1 = new Array();
		for (var i=0; i<sum.length; i++) {
			if (i<sum.length-1) {
				sum1.push(countAngle(sum[i],sum[i+1]));
			} else {
				sum1.push(countAngle(sum[i],sum[0]));
			}
		}
		var summary = 0;
		for (var i=0; i<sum1.length; i++) {
			summary += sum1[i];
		}
		if (summary > 359 && summary < 361) {
			return true;
		} else {
			return false;
		}
	} catch(e) {alert(e);return false;}
}

function countAngle(a1,a2) {
	try {
		if (a1*a2 < 0) {
			var tmp = Math.abs(a1)+Math.abs(a2);
			if (tmp>180) {tmp = 360-tmp;}
			return tmp;
		} else if (a1*a2 >0) {
			return Math.abs(a1-a2);
		} else {
			return Math.abs(a1+a2);
		}
	} catch (e) {alert(e);}
}

function movePolygon(_polygon, _point) {
	for (var i=0; i<_polygon.length; i++) {
		_polygon[i].x += _point.x;
		_polygon[i].y += _point.y;
	}
//	return _polygon;
}

function drawPixels(_points, _color) {
	for (var j=0;j<_points.length; j++) {
		drawPixel(_points[j], _color);
	}
}

function drawLabeledPixels(_points, _color, _label) {
	for (var j=0;j<_points.length; j++) {
		drawLabeledPixel(_points[j], _color, _label);
	}
}

function drawTargetedPixels(_points, _color, _target, _perv) {
	for (var j=0;j<_points.length; j++) {
		drawTargetedPixel(_points[j], _color, _target, _perv);
	}
}

function drawPixel(_point, _color) {
	var obj = document.createElement("div");
//	obj.style = "position:absolute;clip:rect(0,1,1,0);overflow:hidden";
	obj.style.position = "absolute";
	obj.style.overflow = "hidden";
	obj.style.clip = "rect(0px,1px,1px,0px)";
	obj.style.background = _color;
	obj.innerHTML = "<span style=\"line-height:1px;font-size:1px\">&nbsp;</span>";
	obj.width = 1;
	obj.height = 1;
	obj.style.left = _point.x+"px";
	obj.style.top = _point.y+"px";
	obj.style.zIndex = 50;
	obj.style.margin = "0";
	obj.style.padding = "0";
	obj.style.cursor = "default";
	document.body.appendChild(obj);
}

function drawLabeledPixel(_point, _color, _label) {
	var obj = document.createElement("div");
//	obj.style = "position:absolute;clip:rect(0,1,1,0);overflow:hidden";
	obj.id = _label;
	obj.style.position = "absolute";
	obj.style.overflow = "hidden";
	obj.style.clip = "rect(0px,1px,1px,0px)";
	obj.style.background = _color;
	obj.innerHTML = "<span style=\"line-height:1px;font-size:1px\">&nbsp;</span>";
	obj.width = 1;
	obj.height = 1;
	obj.style.left = _point.x+"px";
	obj.style.top = _point.y+"px";
	obj.style.zIndex = 50;
	obj.style.margin = "0";
	obj.style.padding = "0";
	obj.style.cursor = "default";
	document.body.appendChild(obj);
}

function drawTargetedPixel(_point, _color, _target, _perv) {
	var obj = document.createElement("div");
	obj.style.position = "absolute";
	obj.style.overflow = "hidden";
	obj.style.clip = "rect(0px,1px,1px,0px)";
	obj.style.background = _color;
	obj.innerHTML = "<span style=\"line-height:1px;font-size:1px\">&nbsp;</span>";
	obj.width = 1;
	obj.height = 1;
	obj.style.left = _point.x+"px";
	obj.style.top = _point.y+"px";
	obj.style.zIndex = _perv;
	obj.style.margin = "0";
	obj.style.padding = "0";
	obj.style.cursor = "default";
	_target.appendChild(obj);
}

function realPosition(_obj) {
	var currPos = new Point(_obj.offsetLeft,_obj.offsetTop);
	var workPos = new Point(0,0);
	if (_obj.offsetParent.tagName != "BODY") {
		workPos = realPosition(_obj.offsetParent);
		currPos.x += workPos.x;
		currPos.y += workPos.y;
	}
	return currPos;
}
