log( "vector.lua!" )

-- prototype .. watch out for conflicts in game code!
CVector2 = {}

-- constructor (ex: local blah = CVector2:Create( 0, 0 ) )
function CVector2:Create( i_x, i_y )
    local newObj = {}
    if ( i_x ~= nil and i_y ~= nil ) then
        newObj.X = i_x
        newObj.Y = i_y
    else
        newObj.X = 0
        newObj.Y = 0
    end
    
    setmetatable( newObj, self )
    self.__index = self
    
    return newObj
end

-- use the ":" notation when calling to get object context

function CVector2:GetMagnitude()
    return math.sqrt( self.X * self.X + self.Y * self.Y );
end

--void
function CVector2:NormalizeTo( i_magnitude )
	-- !!! VEC_NormalizeTo.NormalizedTo() : Trying to normalize zero magnitude vector"
    assert( self.X ~= 0 or self.Y ~= 0 )
    local magAdj = i_magnitude / self:GetMagnitude();
    
    self.X = self.X * magAdj;
    self.Y = self.Y * magAdj;
end

--void
function CVector2:Clamp( i_magnitude )
    if ( self:GetMagnitude( self ) > i_magnitude ) then
        self:NormalizeTo( i_magnitude );
    end
end


--void
function CVector2:Normalize()
    self:NormalizeTo( 1 );
end

--void
function CVector2:Scale( i_scalar )
    self.X = self.X * i_scalar;
    self.Y = self.Y * i_scalar;
end

--void
function CVector2:Add( i_secondVec )
    self.X = self.X + i_secondVec.X;
    self.Y = self.Y + i_secondVec.Y;
end

--void
function CVector2:Subtract( i_secondVec )
    self.X = self.X - i_secondVec.X;
    self.Y = self.Y - i_secondVec.Y;
end

--void
function CVector2:Reflect( inN )
    local projVonN = CVector2:Create( inN.X, inN.Y )
    local vDotN = self:Dot( inN )
    projVonN:Scale( -2 * vDotN )

    local ret = CVector2:Create( self.X, self.Y )
    ret:Subtract( projVonN )
end

--CVector2
function CVector2:Clone()
    local newVec = CVector2:Create( self.X, self.Y )
    return newVec
end

--bool
function CVector2:IsInRange( i_vec, i_dist )
    return i_dist * i_dist > self:ComputeDistanceSquare( i_vec )
end



--[[number]] function CVector2:ComputeDistanceSquare( i_vec )
    local ret = 0
    ret = ret + ( self.X - inVector.X ) * ( self.X - inVector.X )
    ret = ret + ( self.Y - inVector.Y ) * ( self.Y - inVector.Y )

    return ret;
end

-- return in radians
--number
function CVector2:GetAngle( i_deg ) 
    -- from positive X axis
    local ret = math.atan( self.Y / self.X ) + 3.14 / 2
    if ( self.X < 0 ) then 
        ret = ret + 3.14
    end
    
    if ( i_deg ~= nil and i_deg ) then
        ret = ret / 3.14 * 180
    end
    
    return ret
end

-- set as radians
function CVector2:SetAngle( i_angle ) 
    local oldMag = self:GetMagnitude()

    self.X = math.sin( i_angle )
    self.Y = 0 - math.cos( i_angle )
		
    self:NormalizeTo( oldMag );
end

function CVector2:ToString()
    
    return self.X .. "," .. self.Y
    
end
