Skip to content

Instantly share code, notes, and snippets.

@fillano
Created May 16, 2011 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fillano/974816 to your computer and use it in GitHub Desktop.
Save fillano/974816 to your computer and use it in GitHub Desktop.
simple drawing functions
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;
}
function getCircle (x,y,r) {
var points = [];
for (var i = 0; i> (-Math.round(r/Math.sqrt(2))-1); i--) {
j = Math.sqrt(Math.pow(r,2) - Math.pow(i,2));
if (Math.ceil(j)-j > j-Math.floor(j)) {
j = Math.floor(j);
} else {
j = Math.ceil(j);
}
points.push({"x":i+x,"y":j+y});
points.push({"x":i+x,"y":y-j});
points.push({"x":x-i,"y":j+y});
points.push({"x":x-i,"y":y-j});
points.push({"x":j+x,"y":i+y});
points.push({"x":j+x,"y":y-i});
points.push({"x":x-j,"y":i+y});
points.push({"x":x-j,"y":y-i});
}
return points;
}
function getEclipse(x, y, rx, ry) {
var points = [],
_tx,_ty,_x,_y,_t,i;
if(rx==ry) {
return getCircle(x, y, rx);
}
if(rx > ry) {
i = y;
for(; i>(y-Math.pow(ry,2)/(rx*Math.SQRT2)); i--) {
_tx = x - Math.sqrt((1 - Math.pow(i-y, 2) / Math.pow(ry, 2)) * Math.pow(rx, 2));
_x = (Math.ceil(_tx)-_tx > _tx-Math.floor(_tx))? Math.floor(_tx):Math.ceil(_tx);
_y = i;
points.push({"x":_x,"y":_y});
points.push({"x":_x,"y":2*y-_y});
_t = _x;
points.push({"x":x*2-_x,"y":_y});
points.push({"x":x*2-_x,"y":2*y-_y});
}
i = --_t;
for(; i<(x+1); i++) {
_x = i;
_ty = y - Math.sqrt((1 - Math.pow(i-x, 2) / Math.pow(rx, 2)) * Math.pow(ry, 2));
_y = (Math.ceil(_ty)-_ty > _ty-Math.floor(_ty))? Math.floor(_ty): Math.ceil(_ty);
points.push({"x":_x,"y":_y});
points.push({"x":x*2-_x,"y":_y});
points.push({"x":_x,"y":y*2-_y});
points.push({"x":x*2-_x,"y":y*2-_y});
}
return points;
}
if(rx < ry) {
i = x;
for(; i>(x-Math.pow(rx,2)/(ry*Math.SQRT2)); i--) {
_ty = y - Math.sqrt((1 - Math.pow(i-x, 2) / Math.pow(rx, 2)) * Math.pow(ry, 2));
_y = (Math.ceil(_ty)-_ty > _ty-Math.floor(_ty))? Math.floor(_ty):Math.ceil(_ty);
_x = i;
points.push({"x":_x,"y":_y});
points.push({"x":_x,"y":2*y-_y});
_t = _y;
points.push({"x":x*2-_x,"y":_y});
points.push({"x":x*2-_x,"y":2*y-_y});
}
i = --_t;
for(; i<(y+1); i++) {
_y = i;
_tx = x - Math.sqrt((1 - Math.pow(i-y, 2) / Math.pow(ry, 2)) * Math.pow(rx, 2));
_x = (Math.ceil(_tx)-_tx > _tx-Math.floor(_tx))? Math.floor(_tx): Math.ceil(_tx);
points.push({"x":_x,"y":_y});
points.push({"x":x*2-_x,"y":_y});
points.push({"x":_x,"y":y*2-_y});
points.push({"x":x*2-_x,"y":y*2-_y});
}
return points;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment