Created
September 15, 2015 23:20
-
-
Save reportbase/f01cb372131342fbafd5 to your computer and use it in GitHub Desktop.
Principles Of HTML5 Game Design
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
function point2(x, y) | |
{ | |
return {'x': x || 0, 'y': y || 0}; | |
} | |
function vector2(x, y) | |
{ | |
return {'x': x || 0, 'y': y || 0}; | |
} | |
point2.distance = function(a, b) | |
{ | |
var x = a.x - b.x; | |
var y = a.y - b.y; | |
return Math.sqrt(x*x + y*y); | |
}; | |
vector2.length = function(vector) | |
{ | |
return Math.sqrt(vector.x*vector.x + vector.y*vector.y); | |
}; | |
vector2.normalize = function(vector) | |
{ | |
var length = vector2.length(vector); | |
if (length > 0) | |
return vector2(vector.x / length, vector.y / length); | |
else | |
return vector2(); | |
}; | |
point2.direction = function(from, to) | |
{ | |
var x = to.x - from.x; | |
var y = to.y - from.y; | |
var length = Math.sqrt(x*x + y*y); | |
if (length > 0) | |
return vector2(x / length, y / length); | |
else | |
return vector2(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment