Skip to content

Instantly share code, notes, and snippets.

@BrianHicks
Created April 29, 2013 20:28
Show Gist options
  • Save BrianHicks/5484517 to your computer and use it in GitHub Desktop.
Save BrianHicks/5484517 to your computer and use it in GitHub Desktop.
function Point(x, y) {
this.x = x;
this.y = y;
}
function Vector(degrees, length) {
this.degrees = degrees; // -180 to 180
this.length = length;
this.radians = degrees * Math.PI / 180;
// assuming y is up
this.x = Math.cos(this.radians) * this.length;
this.y = Math.sin(this.radians) * this.length;
}
function transform(point, vector) {
return new Point(
point.x + vector.x,
point.y + vector.y
);
}
var original = new Point(20, 200),
offset = new Vector(60, 1000),
transformed = transform(original, offset);
console.log(transformed.x); // 520.0000000000001
console.log(transformed.y); // 1066.0254037844386
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment