Skip to content

Instantly share code, notes, and snippets.

@emmanuelstroem
Forked from brandontreb/Vector2D.lua
Created July 30, 2012 19:27
Show Gist options
  • Select an option

  • Save emmanuelstroem/3209375 to your computer and use it in GitHub Desktop.

Select an option

Save emmanuelstroem/3209375 to your computer and use it in GitHub Desktop.

Revisions

  1. @brandontreb brandontreb revised this gist Jun 3, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Vector2D.lua
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    Vector2D = {}

    function Vector2D:new(x, y) -- The constructor
    function Vector2D:new(x, y)
    local object = { x = x, y = y }
    setmetatable(object, { __index = Vector2D }) -- Inheritance
    setmetatable(object, { __index = Vector2D })
    return object
    end

  2. @brandontreb brandontreb created this gist Jun 3, 2011.
    135 changes: 135 additions & 0 deletions Vector2D.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,135 @@
    Vector2D = {}

    function Vector2D:new(x, y) -- The constructor
    local object = { x = x, y = y }
    setmetatable(object, { __index = Vector2D }) -- Inheritance
    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