14.01.2021, 12:53
The adapted script, where in both versions the X is input (outdoor air temperature for example) and the calculated Y is output.
--- [Function] fbe_curve_2pt
--- Curve function with 2 points
--- [Comment]
--- Depending on a straight line defined by 2 points, this module supplies a y-value for an x-value.
--- The first point of the straight line is defined by x1 and y1.
--- The second point of the straight line is defined by x2 and y2.
--- If x is less than x1, then y=y1.
--- If x is greater than x2, then y=y2.
--- Only strictly monotone rising or falling functions can be realised. This means x1 < x2.
--- [Input]
--- Input [value, object, storage]
--- X1 [value, object, storage]
--- Y1 [value, object, storage]
--- X2 [value, object, storage]
--- Y2 [value, object, storage]
--- [Output]
--- out - Output [object, storage]
function fbe_curve_2pt(input, x1, y1, x2, y2)
if input <= x1 then
return y1
elseif input >= x2 then
return y2
end
local n1, n2 = y2 - y1, x1 - x2
if n1 < 0 then
n1 = n1 * -1
n2 = n2 * -1
end
local c = -n1 * x1 - n2 * y1
return (-n1 * input - c) / n2
end
--- [Function] fbe_curve_2pt_offset
--- Curve function with 2 points and offset
--- [Comment]
--- Depending on straight lines defined by 2 points, this module supplies an x-value for a y-value.
--- The first point of the straight line is defined by x1 and y1.
--- The second point of the straight line is defined by x2 and y2.
--- The x and the y-axis can be shifted by the respective offset.
--- If x is less than (x1 offset x), then y=y1.
--- If x is greater than (x2 offset x), then y=y2.
--- [Input]
--- Input [object, storage]
--- X1 [value, object, storage]
--- Y1 [value, object, storage]
--- X2 [value, object, storage]
--- Y2 [value, object, storage]
--- Offset X [value, object, storage]
--- Offset Y [value, object, storage]
--- [Output]
--- out - Output [object, storage]
function fbe_curve_2pt_offset(input, x1, y1, x2, y2, offsetX, offsetY)
if input <= (x1 + offsetX) then
return y1
elseif input >= (x2 + offsetX) then
return y2
end
local n1, n2 = y2 - y1, x1 - x2
if n1 < 0 then
n1 = n1 * -1
n2 = n2 * -1
end
local c = -n1 * (x1 + offsetX) - n2 * (y1 + offsetY)
return (-n1 * input - c) / n2
end
--- [Function] fbe_curve_2pt
--- Curve function with 2 points
--- [Comment]
--- Depending on a straight line defined by 2 points, this module supplies a y-value for an x-value.
--- The first point of the straight line is defined by x1 and y1.
--- The second point of the straight line is defined by x2 and y2.
--- If x is less than x1, then y=y1.
--- If x is greater than x2, then y=y2.
--- Only strictly monotone rising or falling functions can be realised. This means x1 < x2.
--- [Input]
--- Input [value, object, storage]
--- X1 [value, object, storage]
--- Y1 [value, object, storage]
--- X2 [value, object, storage]
--- Y2 [value, object, storage]
--- [Output]
--- out - Output [object, storage]
function fbe_curve_2pt(input, x1, y1, x2, y2)
if input <= x1 then
return y1
elseif input >= x2 then
return y2
end
local n1, n2 = y2 - y1, x1 - x2
if n1 < 0 then
n1 = n1 * -1
n2 = n2 * -1
end
local c = -n1 * x1 - n2 * y1
return (-n1 * input - c) / n2
end
--- [Function] fbe_curve_2pt_offset
--- Curve function with 2 points and offset
--- [Comment]
--- Depending on straight lines defined by 2 points, this module supplies an x-value for a y-value.
--- The first point of the straight line is defined by x1 and y1.
--- The second point of the straight line is defined by x2 and y2.
--- The x and the y-axis can be shifted by the respective offset.
--- If x is less than (x1 offset x), then y=y1.
--- If x is greater than (x2 offset x), then y=y2.
--- [Input]
--- Input [object, storage]
--- X1 [value, object, storage]
--- Y1 [value, object, storage]
--- X2 [value, object, storage]
--- Y2 [value, object, storage]
--- Offset X [value, object, storage]
--- Offset Y [value, object, storage]
--- [Output]
--- out - Output [object, storage]
function fbe_curve_2pt_offset(input, x1, y1, x2, y2, offsetX, offsetY)
if input <= (x1 + offsetX) then
return y1
elseif input >= (x2 + offsetX) then
return y2
end
local n1, n2 = y2 - y1, x1 - x2
if n1 < 0 then
n1 = n1 * -1
n2 = n2 * -1
end
local c = -n1 * (x1 + offsetX) - n2 * (y1 + offsetY)
return (-n1 * input - c) / n2
end