Difference between revisions of "User:PROXiCiDE/MathUtil"
Jump to navigation
Jump to search
imported>PROXiCiDE m |
imported>PROXiCiDE |
||
Line 1: | Line 1: | ||
<source lang="papyrus"> | <source lang="papyrus"> | ||
; | ;---------------------------------------------------------------------------------[Math] | ||
; Math Functions | ; Math Functions | ||
; | ; | ||
Line 7: | Line 7: | ||
; 0.2 - Optimizations by Cipscis | ; 0.2 - Optimizations by Cipscis | ||
; IsNumberEven ,IsNumberOdd ,InRange | ; IsNumberEven ,IsNumberOdd ,InRange | ||
; | ; 0.3 - Bit Set/Get/Clear | ||
; | ;---------------------------------------------------------------------------------[Min] | ||
; MinInt() / MinFloat() | ; MinInt() / MinFloat() | ||
; Returns the smallest of X and Y | ; Returns the smallest of X and Y | ||
; If X is greater than Y, then Y is returned | ; If X is greater than Y, then Y is returned | ||
; If both are equivalent X is returned | ; If both are equivalent X is returned | ||
Int Function MinInt(Int x,Int y) | Int Function MinInt(Int x,Int y) | ||
If x < y | If x < y | ||
Line 31: | Line 30: | ||
EndFunction | EndFunction | ||
; | ;---------------------------------------------------------------------------------[Max] | ||
; MaxInt() / MaxFloat() | ; MaxInt() / MaxFloat() | ||
; Returns the largest of X and Y | ; Returns the largest of X and Y | ||
; If X is lesser than Y, then Y is returned | ; If X is lesser than Y, then Y is returned | ||
; If both are equivalent X is returned | ; If both are equivalent X is returned | ||
Int Function MaxInt(Int x,Int y) | Int Function MaxInt(Int x,Int y) | ||
If x > y | If x > y | ||
Line 53: | Line 51: | ||
EndFunction | EndFunction | ||
; | ;---------------------------------------------------------------------------------[IsNumberEven] | ||
; Is the Number a Even Number? | ; Is the Number a Even Number? | ||
; 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 | ; 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 | ||
Bool Function IsNumberEven (Int num) | Bool Function IsNumberEven (Int num) | ||
If Math.Floor(num % 2) | If Math.Floor(num % 2) | ||
Line 66: | Line 62: | ||
EndFunction | EndFunction | ||
; | ;---------------------------------------------------------------------------------[IsNumberOdd] | ||
; Is the Number a Odd Number? | ; Is the Number a Odd Number? | ||
; 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 | ; 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 | ||
Bool Function IsNumberOdd (Int num) | Bool Function IsNumberOdd (Int num) | ||
If Math.Floor(num % 2) | If Math.Floor(num % 2) | ||
Line 79: | Line 73: | ||
EndFunction | EndFunction | ||
; | ;---------------------------------------------------------------------------------[Clamp] | ||
; ClampInt() / ClampFloat() | ; ClampInt() / ClampFloat() | ||
; Clamps a Value with-in a given Min and Max Range | ; Clamps a Value with-in a given Min and Max Range | ||
; If X is lesser than Min , Results = Min | ; If X is lesser than Min , Results = Min | ||
; If X is greater than Max , Results = Max | ; If X is greater than Max , Results = Max | ||
; Otherwise Return X | ; Otherwise Return X | ||
Float Function ClampFloat(Float x,Float min,Float max) | Float Function ClampFloat(Float x,Float min,Float max) | ||
Return MinFloat(MaxFloat(x,min),max) | Return MinFloat(MaxFloat(x,min),max) | ||
Line 94: | Line 87: | ||
EndFunction | EndFunction | ||
; | ;---------------------------------------------------------------------------------[InRange] | ||
; Checks to see if Value is with-in a Range of Min and Max | ; Checks to see if Value is with-in a Range of Min and Max | ||
; If X is greater than Max, Returns False | ; If X is greater than Max, Returns False | ||
; If X is lesser than Min, Returns False | ; If X is lesser than Min, Returns False | ||
; Otherwise return True | ; Otherwise return True | ||
Bool Function InRange(Float x,Float min,Float max) | Bool Function InRange(Float x,Float min,Float max) | ||
Return (min <= x && x <= max) | Return (min <= x && x <= max) | ||
EndFunction | EndFunction | ||
; | ;---------------------------------------------------------------------------------[Round] | ||
; Float : afValue - number to round | ; Float : afValue - number to round | ||
; Int : iMult - Multiplier default is 10 | ; Int : iMult - Multiplier default is 10 | ||
Line 113: | Line 103: | ||
; 1 - Up | ; 1 - Up | ||
; 2 - Down | ; 2 - Down | ||
Int Function Round(Float afValue, Int iMult = 10, Int iMode = 0) | Int Function Round(Float afValue, Int iMult = 10, Int iMode = 0) | ||
If iMode == 1 ; Round Up | If iMode == 1 ; Round Up | ||
Line 122: | Line 111: | ||
Return Math.Floor((afValue * iMult) + 0.5) / iMult | Return Math.Floor((afValue * iMult) + 0.5) / iMult | ||
EndIf | EndIf | ||
EndFunction | |||
;---------------------------------------------------------------------------------[Bit Manipulation] | |||
; Bits are range of 1, 30 | |||
; 1............................30 | |||
; 000000000000000000000000000000 | |||
; | |||
; i = Bit_Set(0,2) = 010000000000000000000000000000 | |||
; i = Bit_Set(i,4) = 010100000000000000000000000000 | |||
; i = Bit_Set(i,1) = 110100000000000000000000000000 | |||
; i = Bit_Set(i,30) = 110100000000000000000000000001 | |||
; i = Bit_Xor(i,30) = 110100000000000000000000000000 | |||
; i = Bit_Clear(i,2) = 101111111111111111111111111111 | |||
; i = Bit_Clear(i,30) = 101111111111111111111111111110 | |||
; i = Bit_ClearFrom(i,15) = 101111111111110000000000000000 | |||
; i = Bit_Xor(i,30) = 101111111111110000000000000001 | |||
; i = Bit_ClearFrom(i,4) = 101000000000000000000000000000 | |||
; i = Bit_Set(i,5) = 101010000000000000000000000000 | |||
Int Function Bit_MakeByte(Int aiIndex = 1) | |||
aiIndex = ClampInt(aiIndex, 1, 30) | |||
Int iPos = aiIndex % 30 | |||
return Math.LeftShift(1, iPos) | |||
EndFunction | |||
Int Function Bit_Set(Int aiValue, Int aiIndex = 1) | |||
Return Math.LogicalOr(aiValue,Bit_MakeByte(aiIndex)) | |||
EndFunction | |||
Int Function Bit_Xor(Int aiValue, Int aiIndex = 1) | |||
Return Math.LogicalXor(aiValue,Bit_MakeByte(aiIndex)) | |||
EndFunction | |||
Int Function Bit_SetBool(Int aiValue, Int aiIndex = 1, Bool abValue) | |||
If abValue | |||
Bit_Set(aiValue,aiIndex) | |||
Else | |||
Bit_Clear(aiValue,aiIndex) | |||
EndIf | |||
EndFunction | |||
Bool Function Bit_Get(Int aiValue, Int aiIndex = 1) | |||
Return Math.LogicalAnd(aiValue,Bit_MakeByte(aiIndex)) != 0 | |||
EndFunction | |||
Int Function Bit_Clear(Int aiValue, Int aiIndex = 1) | |||
Return Math.LogicalAnd(aiValue,Math.LogicalNot(Bit_MakeByte(aiIndex))) | |||
EndFunction | |||
Int Function Bit_ClearFrom(Int aiValue, Int aiIndex = 1) | |||
aiIndex = ClampInt(aiIndex, 1, 30) | |||
If aiIndex > 1 && aiIndex < 30 | |||
Int iPos = aiIndex | |||
While (iPos <= 30) | |||
aiValue = Bit_Clear(aiValue,iPos) | |||
iPos += 1 | |||
EndWhile | |||
Return aiValue | |||
Else | |||
Return 0 | |||
EndIf | |||
EndFunction | EndFunction | ||
</source> | </source> |
Revision as of 23:59, 26 April 2014
;---------------------------------------------------------------------------------[Math]
; Math Functions
;
; Version History
; 0.1 - Initial Release
; 0.2 - Optimizations by Cipscis
; IsNumberEven ,IsNumberOdd ,InRange
; 0.3 - Bit Set/Get/Clear
;---------------------------------------------------------------------------------[Min]
; MinInt() / MinFloat()
; Returns the smallest of X and Y
; If X is greater than Y, then Y is returned
; If both are equivalent X is returned
Int Function MinInt(Int x,Int y)
If x < y
Return x
Else
Return y
EndIf
EndFunction
Float Function MinFloat(Float x,Float y)
If x < y
Return x
Else
Return y
EndIf
EndFunction
;---------------------------------------------------------------------------------[Max]
; MaxInt() / MaxFloat()
; Returns the largest of X and Y
; If X is lesser than Y, then Y is returned
; If both are equivalent X is returned
Int Function MaxInt(Int x,Int y)
If x > y
Return x
Else
Return y
EndIf
EndFunction
Float Function MaxFloat(Float x,Float y)
If x > y
Return x
Else
Return y
EndIf
EndFunction
;---------------------------------------------------------------------------------[IsNumberEven]
; Is the Number a Even Number?
; 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30
Bool Function IsNumberEven (Int num)
If Math.Floor(num % 2)
Return False
Else
Return True
EndIf
EndFunction
;---------------------------------------------------------------------------------[IsNumberOdd]
; Is the Number a Odd Number?
; 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31
Bool Function IsNumberOdd (Int num)
If Math.Floor(num % 2)
Return True
Else
Return False
EndIf
EndFunction
;---------------------------------------------------------------------------------[Clamp]
; ClampInt() / ClampFloat()
; Clamps a Value with-in a given Min and Max Range
; If X is lesser than Min , Results = Min
; If X is greater than Max , Results = Max
; Otherwise Return X
Float Function ClampFloat(Float x,Float min,Float max)
Return MinFloat(MaxFloat(x,min),max)
EndFunction
Int Function ClampInt(Int x,Int min,Int max)
Return MinInt(MaxInt(x,min),max)
EndFunction
;---------------------------------------------------------------------------------[InRange]
; Checks to see if Value is with-in a Range of Min and Max
; If X is greater than Max, Returns False
; If X is lesser than Min, Returns False
; Otherwise return True
Bool Function InRange(Float x,Float min,Float max)
Return (min <= x && x <= max)
EndFunction
;---------------------------------------------------------------------------------[Round]
; Float : afValue - number to round
; Int : iMult - Multiplier default is 10
; Int : iMode - Mode type for rounding
; 0 - Default
; 1 - Up
; 2 - Down
Int Function Round(Float afValue, Int iMult = 10, Int iMode = 0)
If iMode == 1 ; Round Up
Return Math.Ceiling(afValue / iMult) * iMult
ElseIf iMode == 2; Round Down
Return Math.Floor(afValue / iMult) * iMult
Else
Return Math.Floor((afValue * iMult) + 0.5) / iMult
EndIf
EndFunction
;---------------------------------------------------------------------------------[Bit Manipulation]
; Bits are range of 1, 30
; 1............................30
; 000000000000000000000000000000
;
; i = Bit_Set(0,2) = 010000000000000000000000000000
; i = Bit_Set(i,4) = 010100000000000000000000000000
; i = Bit_Set(i,1) = 110100000000000000000000000000
; i = Bit_Set(i,30) = 110100000000000000000000000001
; i = Bit_Xor(i,30) = 110100000000000000000000000000
; i = Bit_Clear(i,2) = 101111111111111111111111111111
; i = Bit_Clear(i,30) = 101111111111111111111111111110
; i = Bit_ClearFrom(i,15) = 101111111111110000000000000000
; i = Bit_Xor(i,30) = 101111111111110000000000000001
; i = Bit_ClearFrom(i,4) = 101000000000000000000000000000
; i = Bit_Set(i,5) = 101010000000000000000000000000
Int Function Bit_MakeByte(Int aiIndex = 1)
aiIndex = ClampInt(aiIndex, 1, 30)
Int iPos = aiIndex % 30
return Math.LeftShift(1, iPos)
EndFunction
Int Function Bit_Set(Int aiValue, Int aiIndex = 1)
Return Math.LogicalOr(aiValue,Bit_MakeByte(aiIndex))
EndFunction
Int Function Bit_Xor(Int aiValue, Int aiIndex = 1)
Return Math.LogicalXor(aiValue,Bit_MakeByte(aiIndex))
EndFunction
Int Function Bit_SetBool(Int aiValue, Int aiIndex = 1, Bool abValue)
If abValue
Bit_Set(aiValue,aiIndex)
Else
Bit_Clear(aiValue,aiIndex)
EndIf
EndFunction
Bool Function Bit_Get(Int aiValue, Int aiIndex = 1)
Return Math.LogicalAnd(aiValue,Bit_MakeByte(aiIndex)) != 0
EndFunction
Int Function Bit_Clear(Int aiValue, Int aiIndex = 1)
Return Math.LogicalAnd(aiValue,Math.LogicalNot(Bit_MakeByte(aiIndex)))
EndFunction
Int Function Bit_ClearFrom(Int aiValue, Int aiIndex = 1)
aiIndex = ClampInt(aiIndex, 1, 30)
If aiIndex > 1 && aiIndex < 30
Int iPos = aiIndex
While (iPos <= 30)
aiValue = Bit_Clear(aiValue,iPos)
iPos += 1
EndWhile
Return aiValue
Else
Return 0
EndIf
EndFunction