Skip to content

Instantly share code, notes, and snippets.

@kevinw
Created January 27, 2013 01:15
Show Gist options
  • Select an option

  • Save kevinw/4645667 to your computer and use it in GitHub Desktop.

Select an option

Save kevinw/4645667 to your computer and use it in GitHub Desktop.
local cardinals = {
up = new(0, 1),
down = new(0, -1),
left = new(-1, 0),
right = new(1, 0)
}
function vector:closestCardinal(v)
local maxDistance = 0
local longestVec
for name, cardinalVector in pairs(vector.cardinals) do
local projected = v:projectOn(cardinalVector)
local projectedLength = projected:len2()
if projectedLength > maxDistance then
longestVec = projected
maxDistance = projectedLength
end
end
assert(longestVec)
return longestVec:normalize_inplace()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment