Last active
August 29, 2015 13:57
-
-
Save MattRoelle/9360938 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
Geometry Library | |
All angles are in radian measurements | |
*/ | |
// Point Object | |
function Point(x, y) { | |
this.x = x; | |
this.y = y; | |
}; | |
Point.prototype.distanceBetween = function(p2) { | |
return Math.sqrt(Math.pow(this.x - p2.x, 2) + Math.pow(this.y - p2.y, 2)); | |
}; | |
Point.prototype.angleBetween = function(p2) { | |
return Math.atan((this.y - p2.y) / (this.x - p2.x)); | |
}; | |
Point.prototype.translate = function(dx, dy) { | |
this.x += (dx === undefined) ? 0 : dx; | |
this.y += (dy === undefined) ? 0 : dy; | |
return this; | |
}; | |
Point.prototype.rotate = function(angle, origin) { | |
if (origin === undefined ) { | |
this.x = (this.x*Math.cos(angle)) - (this.y*Math.sin(angle)); | |
this.y = (this.x*Math.sin(angle)) + (this.y*Math.cos(angle)); | |
} else { | |
this.x = (this.x-origin.x)*Math.cos(angle) - (this.y-origin.y)*Math.sin(angle) + origin.x; | |
this.y = (this.x-origin.x)*Math.sin(angle) + (this.y-origin.y)*Math.cos(angle) + origin.y; | |
} | |
return this; | |
}; | |
// Polygon Object | |
function Polygon(points) { | |
if (points[0].length === undefined) { | |
// if points is an array of Point objects | |
this.points = points; | |
} else { | |
// if points is an array of arrays [ [10, 20], [15, 5] ] | |
this.points = points.map(function(point) { | |
return new Point(point[0], point[1]); | |
}); | |
} | |
} | |
Polygon.prototype.translate = function(dx, dy) { | |
this.points.map(function(point) { | |
point.translate(dx, dy); | |
}); | |
}; | |
Polygon.prototype.rotate = function(angle, origin) { | |
this.points.map(function(point) { | |
point.rotate(angle, origin); | |
}); | |
}; | |
Polygon.prototype.containsPoint = function(point) { | |
// based off of http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html | |
var p; | |
if (point.length === undefined) { | |
p = point; | |
} else { | |
p = new Point(point[0], point[1]); | |
} | |
var x = p.x, y = p.y; | |
var inside = false; | |
var j = this.points.length - 1; | |
for(var i = 0; i < this.points.length; j = i++) { | |
var xi = this.points[i].x, yi = this.points[i].y; | |
var xj = this.points[j].x, yj = this.points[j].y; | |
var intersect = ((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); | |
if (intersect) { | |
inside = !inside; | |
} | |
} | |
return inside; | |
}; | |
Polygon.prototype.intersectsPolygon = function(p2) { | |
var intersects = false; | |
p2.points.forEach(function(point) { | |
intersects = this.containsPoint(point); | |
}); | |
this.points.forEach(function(point) { | |
intersects = p2.containsPoint(point); | |
}); | |
return intersects; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment