Instantly share code, notes, and snippets.

brandontreb/Vector2D.lua

Created Jun 3, 2011
A simple Vector2D class used in Corona SDK games.
 Vector2D = {} function Vector2D:new(x, y) local object = { x = x, y = y } setmetatable(object, { __index = Vector2D }) return object end function Vector2D:copy() return Vector2D:new(self.x, self.y) end function Vector2D:magnitude() return math.sqrt(self.x^2 + self.y^2) end function Vector2D:normalize() local temp temp = self:magnitude() if temp > 0 then self.x = self.x / temp self.y = self.y / temp end end function Vector2D:limit(l) if self.x > l then self.x = l end if self.y > l then self.y = l end end function Vector2D:equals(vec) if self.x == vec.x and self.y == vec.y then return true else return false end end function Vector2D:add(vec) self.x = self.x + vec.x self.y = self.y + vec.y end function Vector2D:sub(vec) self.x = self.x - vec.x self.y = self.y - vec.y end function Vector2D:mult(s) self.x = self.x * s self.y = self.y * s end function Vector2D:div(s) self.x = self.x / s self.y = self.y / s end function Vector2D:dot(vec) return self.x * vec.x + self.y * vec.y end function Vector2D:dist(vec2) return math.sqrt( (vec2.x - self.x) + (vec2.y - self.y) ) end -- Class Methods function Vector2D:Normalize(vec) local tempVec = Vector2D:new(vec.x,vec.y) local temp temp = tempVec:magnitude() if temp > 0 then tempVec.x = tempVec.x / temp tempVec.y = tempVec.y / temp end return tempVec end function Vector2D:Limit(vec,l) local tempVec = Vector2D:new(vec.x,vec.y) if tempVec.x > l then tempVec.x = l end if tempVec.y > l then tempVec.y = l end return tempVec end function Vector2D:Add(vec1, vec2) local vec = Vector2D:new(0,0) vec.x = vec1.x + vec2.x vec.y = vec1.y + vec2.y return vec end function Vector2D:Sub(vec1, vec2) local vec = Vector2D:new(0,0) vec.x = vec1.x - vec2.x vec.y = vec1.y - vec2.y return vec end function Vector2D:Mult(vec, s) local tempVec = Vector2D:new(0,0) tempVec.x = vec.x * s tempVec.y = vec.y * s return tempVec end function Vector2D:Div(vec, s) local tempVec = Vector2D:new(0,0) tempVec.x = vec.x / s tempVec.y = vec.y / s return tempVec end function Vector2D:Dist(vec1, vec2) return math.sqrt( (vec2.x - vec1.x) + (vec2.y - vec1.y) ) end return Vector2D

ghost commented Feb 11, 2014

 Vector2D:dist and Vector2D:Dist are wrong, here's the correct implementation of Euclidian distance: function Vector2D:Dist(vec1, vec2) dx = (vec2.x - vec1.x) dy = (vec2.y - vec1.y) return math.sqrt( dx_dx + dy_dy ) end function Vector2D:dist(vec2) dx = (vec2.x - self.x) dy = (vec2.y - self.y) return math.sqrt( dx_dx + dy_dy ) end Nice work anyway :)

poisa commented Jul 10, 2014

 Here's how to limit the actual magnitude of a vector (the limit function in the code above only deals with individual components, not magnitude). I ported this from the openFrameworks ofVec3f class (https://github.com/openframeworks/openFrameworks/blob/master/libs/openFrameworks/math/ofVec3f.h#L931) ```function Vector2D:LimitMagnitude(vec, max) local tempVec = Vector2D:new(vec.x, vec.y) local lengthSquared = tempVec.x * tempVec.x + tempVec.y * tempVec.y if lengthSquared > max * max and lengthSquared > 0 then local ratio = max / math.sqrt(lengthSquared) tempVec.x = tempVec.x * ratio tempVec.y = tempVec.y * ratio end return tempVec end```