annotate Libs/DF/math.lua @ 63:3552946c0b9a tip

Added tag v8.2.0.062 for changeset d6704922ef5d
author Tercioo
date Fri, 28 Jun 2019 20:06:18 -0300
parents 0682d738499b
children
rev   line source
Tercio@58 1
Tercio@58 2
Tercio@58 3 local DF = _G ["DetailsFramework"]
Tercio@58 4 if (not DF or not DetailsFrameworkCanLoad) then
Tercio@58 5 return
Tercio@58 6 end
Tercio@58 7
Tercio@58 8 local UnitExists = UnitExists
Tercio@58 9 local atan2 = math.atan2
Tercio@58 10 local pi = math.pi
Tercio@58 11 local abs = math.abs
Tercio@58 12
Tercio@58 13 SMALL_FLOAT = 0.000001
Tercio@58 14
Tercio@58 15 --find distance between two players
Tercio@58 16 function DF:GetDistance_Unit (unit1, unit2)
Tercio@58 17 if (UnitExists (unit1) and UnitExists (unit2)) then
Tercio@58 18 local u1X, u1Y = UnitPosition (unit1)
Tercio@58 19 local u2X, u2Y = UnitPosition (unit2)
Tercio@58 20
Tercio@58 21 local dX = u2X - u1X
Tercio@58 22 local dY = u2Y - u1Y
Tercio@58 23
Tercio@58 24 return ((dX*dX) + (dY*dY)) ^ .5
Tercio@58 25 end
Tercio@58 26 return 0
Tercio@58 27 end
Tercio@58 28
Tercio@58 29 --find distance between two points
Tercio@58 30 function DF:GetDistance_Point (x1, y1, x2, y2)
Tercio@58 31 local dx = x2 - x1
Tercio@58 32 local dy = y2 - y1
Tercio@58 33 return ((dx * dx) + (dy * dy)) ^ .5
Tercio@58 34 end
Tercio@58 35
Tercio@58 36 --find a rotation for an object from a point to another point
Tercio@58 37 function DF:FindLookAtRotation (x1, y1, x2, y2)
Tercio@58 38 return atan2 (y2 - y1, x2 - x1) + pi
Tercio@58 39 end
Tercio@58 40
Tercio@58 41 --find the value scale between two given values. e.g: value of 500 in a range 0-100 result in 10 in a scale for 0-10
Tercio@58 42 function DF:MapRangeClamped (inputX, inputY, outputX, outputY, value)
Tercio@58 43 return DF:GetRangeValue (outputX, outputY, Clamp (DF:GetRangePercent (inputX, inputY, value), 0, 1))
Tercio@58 44 end
Tercio@58 45
Tercio@58 46 --find the value scale between two given values. e.g: value of 75 in a range 0-100 result in 7.5 in a scale for 0-10
Tercio@58 47 function DF:MapRangeUnclamped (inputX, inputY, outputX, outputY, value)
Tercio@58 48 return DF:GetRangeValue (outputX, outputY, DF:GetRangePercent (inputX, inputY, value))
Tercio@58 49 end
Tercio@58 50
Tercio@58 51 --find the normalized percent of the value in the range. e.g range of 200-400 and a value of 250 result in 0.25
Tercio@58 52 function DF:GetRangePercent (minValue, maxValue, value)
Tercio@58 53 return (value - minValue) / (maxValue - minValue)
Tercio@58 54 end
Tercio@58 55
Tercio@58 56 --find the value in the range given from a normalized percent. e.g range of 200-400 and a percent of 0.8 result in 360
Tercio@58 57 function DF:GetRangeValue (minValue, maxValue, percent)
Tercio@58 58 return Lerp (minValue, maxValue, percent)
Tercio@58 59 end
Tercio@58 60
Tercio@58 61 --dot product of two 2D Vectors
Tercio@58 62 function DF:GetDotProduct (value1, value2)
Tercio@58 63 return (value1.x * value2.x) + (value1.y * value2.y)
Tercio@58 64 end
Tercio@58 65
Tercio@58 66 --normalized value 0-1 result in the value on the range given, e.g 200-400 range with a value of .5 result in 300
Tercio@58 67 function DF:LerpNorm (minValue, maxValue, value)
Tercio@58 68 return (minValue + value * (maxValue - minValue))
Tercio@58 69 end
Tercio@58 70
Tercio@58 71 --change the color by the deltaTime
Tercio@58 72 function DF:LerpLinearColor (deltaTime, interpSpeed, r1, g1, b1, r2, g2, b2)
Tercio@58 73 deltaTime = deltaTime * interpSpeed
Tercio@58 74 local r = r1 + (r2 - r1) * deltaTime
Tercio@58 75 local g = g1 + (g2 - g1) * deltaTime
Tercio@58 76 local b = b1 + (b2 - b1) * deltaTime
Tercio@58 77 return r, g, b
Tercio@58 78 end
Tercio@58 79
Tercio@58 80 --check if a number is near another number by a tolerance
Tercio@58 81 function DF:IsNearlyEqual (value1, value2, tolerance)
Tercio@58 82 tolerance = tolerance or SMALL_FLOAT
Tercio@58 83 return abs (value1 - value2) <= tolerance
Tercio@58 84 end
Tercio@58 85
Tercio@58 86 --check if a number is near zero
Tercio@58 87 function DF:IsNearlyZero (value, tolerance)
Tercio@58 88 tolerance = tolerance or SMALL_FLOAT
Tercio@58 89 return abs (value) <= tolerance
Tercio@58 90 end
Tercio@58 91
Tercio@58 92 --check if a number is within a two other numbers, if isInclusive is true, it'll include the max value
Tercio@58 93 function DF:IsWithin (minValue, maxValue, value, isInclusive)
Tercio@58 94 if (isInclusive) then
Tercio@58 95 return ((value >= minValue) and (value <= maxValue))
Tercio@58 96 else
Tercio@58 97 return ((value >= minValue) and (value < maxValue))
Tercio@58 98 end
Tercio@58 99 end
Tercio@58 100
Tercio@58 101 --dont allow a number ot be lower or bigger than a certain range
Tercio@58 102 function DF:Clamp (minValue, maxValue, value)
Tercio@58 103 return value < minValue and minValue or value < maxValue and value or maxValue
Tercio@58 104 end
Tercio@58 105
Tercio@58 106 function DF:ScaleBack ()
Tercio@58 107
Tercio@58 108 end