Last active
December 19, 2015 11:29
-
-
Save tlimpanont/5947580 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MapArea = function() { | |
}; | |
//pointX, pointY, radius | |
MapArea.prototype.circle = function(circleCords) { | |
var cx = circleCords[0]; | |
var cy = circleCords[1]; | |
var r = circleCords[2]; | |
function intersects(x, y) { | |
var dx = x-cx | |
var dy = y-cy | |
return dx*dx+dy*dy <= r*r | |
} | |
return { | |
collideWithPoint: intersects | |
} | |
} | |
//left, top, right | |
MapArea.prototype.rectangle = function(rectCords) { | |
var rect = { | |
left: rectCords[0], | |
top: rectCords[1], | |
right: rectCords[2], | |
bottom: rectCords[3] | |
}; | |
function between(min, p, max){ | |
result = false; | |
if ( min < max ){ | |
if ( p > min && p < max ){ | |
result = true; | |
} | |
} | |
if ( min > max ){ | |
if ( p > max && p < min){ | |
result = true | |
} | |
} | |
if ( p == min || p == max ){ | |
result = true; | |
} | |
return result; | |
} | |
function point_in_rectangle(x, y){ | |
result = false; | |
if ( between(rect.left,x,rect.right) && between(rect.top,y,rect.bottom) ){ | |
result = true; | |
} | |
return result; | |
} | |
return { | |
collideWithPoint: point_in_rectangle | |
} | |
} | |
// arbitary array of points | |
MapArea.prototype.polygon = function(polyCords) { | |
function getCordsSet(cords) | |
{ | |
if (typeof cords=="string") | |
{ | |
var cords = cords.split(","); | |
var cords_array = new Array(); | |
for(var i = 0 ; i < cords.length; i++) | |
{ | |
cords_array.push(parseInt(cords[i])); | |
} | |
} | |
else | |
{ | |
cords_array = cords; | |
} | |
if((cords_array.length % 2) > 0) | |
{ | |
throw new Error("Not a valid polygon cordinates"); | |
} | |
var array_sets = parseInt(cords_array.length / 2); | |
var pointer = 0; | |
var output = new Array(); | |
for(var j = 0 ; j < array_sets; j++) | |
{ | |
output[j] = new Array(); | |
for(var k = 0; k < 2; k++) | |
{ | |
output[j][k] = cords_array[pointer]; | |
pointer++; | |
} | |
} | |
return output; | |
} | |
var polyCords = getCordsSet(polyCords); | |
function collideWithPoint(pointX, pointY) { | |
var i, j, c = false; | |
for (i = 0, j = polyCords.length - 1; i < polyCords.length; j = i++) | |
{ | |
if (((polyCords[i][1] > pointY) != (polyCords[j][1] > pointY)) | |
&& (pointX < (polyCords[j][0] - polyCords[i][0]) * | |
(pointY - polyCords[i][1]) / (polyCords[j][1] - polyCords[i][1]) + polyCords[i][0])) | |
{ | |
c = !c; | |
} | |
} | |
return c; | |
} | |
return { | |
collideWithPoint: collideWithPoint | |
} | |
}; | |
var maparea = new MapArea(); | |
var collidePolygon = maparea.polygon([531, 361, 650, 281, 1460, 233, 1438, 610]).collideWithPoint(2000, 3000); | |
var collideRectangle = maparea.rectangle([441, 148, 651, 245]).collideWithPoint(2000, 3000); | |
var collideCircle = maparea.circle([1068, 190, 49]).collideWithPoint(2000, 3000); | |
console.log("collideRectangle: " + collideRectangle); | |
console.log("collidePolygon: " + collidePolygon); | |
console.log("collideCircle: " + collideCircle); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment