Interpolate linear

From Sim Innovations Wiki
Jump to navigation Jump to search

Description

value = interpolate_linear(settings, value)
value = interpolate_linear(settings, value, cap) (from AM/AP 4.1)

interpolate_linear is used to interpolate a value with given settings.

Return value

Argument Type Description
value Number Return value, can be nil if given value is outside of settings scope and cap is false.

Arguments

# Argument Type Description
1 settings Table Interpolate settings. Can be manually created (see example), or created from table user property with interpolate_settings_from_user_prop function.
2 value Number Input value.
3 cap Boolean (Optional, default false) When input range is outside of settings and cap is false, nil is returned. Otherwise the min or max output value is returned.

Example (manual settings)

-- Create interpolate settings
local settings = { { 0 , 0   },
                   { 10, 100 },
                   { 20, 200 },
                   { 30, 400 } }

-- Interpolate 25 (between 20-30)
local var = interpolate_linear(settings, 25)

-- Will print 300
print(var)

Example (table user property)

-- Create a table with two columns
local val = user_prop_add_table("Needle calibration", "Fill table with calibration data",
                                  "Speed", "DOUBLE", 0, "Fill in the speed of the flight simulator",
                                  "Needle position", "DOUBLE", 0, "Set needle position in steps")

-- Create interpolate settings from user property
local settings = interpolate_settings_from_user_prop(user_prop_get(val))

-- Interpolate using settings made by the user
local var = interpolate_linear(settings, 25)

--Print result
print(var)

Example (using cap)

-- Create interpolate settings
local settings = { { 0 , 0   },
                   { 10, 100 },
                   { 20, 200 },
                   { 30, 400 } }

-- Interpolate 40 (outside of input range)
var = interpolate_linear(settings, 40)

-- Will print nil
print(var)

-- Interpolate 40 again with cap set to true
var = interpolate_linear(settings, 40, true)

-- Will print 400
print(var)