User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
useful_programs [2020/06/03 12:01]
emozolyak [Other handy functions]
useful_programs [2020/07/30 15:07] (current)
atolstov
Line 877: Line 877:
 </​code>​ </​code>​
  
 +===== Curve handler =====
 +Library
 +<code lua - curves.lib>​
 +function GetCurveValue ( curve_register,​ x_to_find_y ​ )
 +    --[[   
 +    --Curve handler ​
 +    INPUT:
 +        Put curve register as first argument and X coordinate as second.
 +    OUTPUT:
 +        Get as result status if it is inside curve range of outside(false) and the Y value as second output argument
 +    ​
 +    EXAMPLE:
 +        curve_status,​ value = GetCurveValue( "​curve_for_current_hour",​ current_hour )
 +    --]]
 +    table = cjson.decode( R ( curve_register ) )
 +    -- inRangeCurve( table, x_to_find_y )
 +    for _, value in ipairs( table ) do --piecewise handler inside range of the curve
 +        if ( value.range[1] <= x_to_find_y and x_to_find_y < value.range[2] ) then 
 +            -- in_range_status,​ index_curve_piece,​ y = true, index, curveLinearCalc( x_to_find_y,​ value.k, value.b )
 +            in_range_status,​ y = true, ( x_to_find_y * value.k + value.b )
 +            return in_range_status,​ y
 +        end
 +    end
 +    if  x_to_find_y < table[1].range[1] then -- behaivior for outside left-sided
 +        -- in_range_status,​ index_curve_piece,​ y = false, 1, curveLinearCalc( table[1].range[1],​ table[1].k*0,​ table[1].b ) --table[1].b
 +        in_range_status,​ index_curve_piece,​ y = false, 1, ( table[1].range[1] * table[1].k + table[1].b ) --table[1].b
 +        return in_range_status,​ y
 +    elseif x_to_find_y > table[#​table].range[2] then -- behaivior for outside right-sided
 +        -- in_range_status,​ index_curve_piece,​ y = false,#​table,​curveLinearCalc( table[#​table].range[2],​ table[#​table].k,​ table[#​table].b ) --table[#​table].b
 +        in_range_status, ​ y = false,#​table,​( table[#​table].range[2] * table[#​table].k + table[#​table].b)
 +        return in_range_status,​ y
 +    end
 +
 +    return in_range_status,​ y 
 +end
 +</​code>​
 +Example of usage:
 +<code lua - Getter>
 +include "​curves.lib"​
 +
 +function main (userId)
 +
 +    local t = os.date("​*t",​ os.time()) ​
 +    current_hour = tonumber(t.hour)
 +    curve_status,​ y = GetCurveValue( "​curve_for_current_hour",​ current_hour )
 +    WriteReg("​value_for_current_hour",​ y) 
 +
 +end
 +</​code>​
  

Page Tools