Skip to content

Instantly share code, notes, and snippets.

@iaincarsberg
Created January 4, 2012 13:32
Show Gist options
  • Save iaincarsberg/1560047 to your computer and use it in GitHub Desktop.
Save iaincarsberg/1560047 to your computer and use it in GitHub Desktop.
Contains some JS to find the angle between shapes
/**
* Used to create a new Vector2
* @param float x Contains the x coord
* @param float y Contains the y coord
*/
function Vector2(x, y) {
this.x = x;
this.y = y;
}
/**
* Used to convert a radient to a degree
* @var Contains 1 radient in degree form
*/
Vector2.prototype.rad2Deg = (180 / Math.PI);
/**
* Used to find the angle between two points
* @param Vector2 v2 Contains a remote v2
* @return float Containing the angle in Radiens
*/
Vector2.prototype.angleTo = function (v2) {
var angle = Math.atan2(
this.x - v2.x,
this.y - v2.y
) * this.rad2Deg;
return (angle < 0) ? (360 + angle) : angle;
};
/**
* Used to define angles to other shapes
* @param Vector2 position Contains the position of the quadrent
*/
function Quadrent(position) {
this.position = position;
this.quadrents = [];
}
/**
* Used to clamp a quadrent
* @param float from Contains a clamped angle
* @param float to Contains a clamped angle
* @param function callback Executed if point within quadrent
* @return void
*/
Quadrent.prototype.addQuadrent = function (from, to, callback) {
this.quadrents.push({
from: from,
to: to,
callback: callback
});
};
/**
* Used to find a quadrent and execute its callback
* @param Vector2 v2 Contains a vector2 were deteting the quadrent of
* @return unknown, depends on return of the quadrents callback
*/
Quadrent.prototype.findQuadrent = function (v2) {
if (! (v2 instanceof Vector2)) {
throw new Error('Expected Vector2, not ', typeof v2);
}
var i, ii,
angle = this.position.angleTo(v2),
quad;
for (i = 0, ii = this.quadrents.length; i < ii; i += 1) {
quad = this.quadrents[i];
if (quad.from < quad.to) {
if (angle >= quad.from && angle < quad.to) {
return quad.callback();
}
} else {
if ((angle >= quad.from && angle <= 360) ||
(angle >= 0 && angle < quad.to)
) {
return quad.callback();
}
}
}
return false;
};
/**
* Used to create a new Shape
* @param double x Contains a x coord
* @param double y Contains a y coord
* @param array quadrents Contains a clockwise list of quadrents starting from
* the up most quadrent.
*/
function Shape(x, y, quadrents) {
this.position = new Vector2(x, y);
this.quadrent = new Quadrent(this.position);
// If no quadrents we're set, default to a simple 4 direction system
if (quadrents === undefined) {
quadrents = [
'above',
'left',
'below',
'right'
];
}
var i, ii,
per = 360 / quadrents.length,
from = 360 - (per / 2),
to;
for (i = 0, ii = quadrents.length; i < ii; i += 1) {
to = from + per;
if (to > 360) {
to -= 360;
}
// Add a new quadrent that will return the detected angle
this.quadrent.addQuadrent(from, to, (function (i) {
return function () {
return quadrents[i];
};
}(i)));
from = to;
}
}
/**
* Used to find the direction to another shape.
* @param Shape shape Contains a shape we're finding the direction to
* @return string Containing the name of the direction
*/
Shape.prototype.getDirectionTo = function (shape) {
if (! (shape instanceof Shape)) {
throw new Error('Expected shape, not ', typeof shape);
}
return this.quadrent.findQuadrent(shape.position);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment