comparison Libs/DF/math.lua @ 58:0682d738499b v8.0.1.058

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