Difference between revisions of "User:PROXiCiDE/MathUtil"

From the CreationKit Wiki
Jump to navigation Jump to search
imported>PROXiCiDE
m (added function: Round)
imported>PROXiCiDE
m
Line 115: Line 115:
;=================================================================================
;=================================================================================
Int Function Round(Float afValue, Int iMult = 10, Int iMode = 0)
Int Function Round(Float afValue, Int iMult = 10, Int iMode = 0)
    Int iResults = 0
     If iMode == 1 ; Round Up
     If iMode == 1 ; Round Up
         Return Math.Ceiling(afValue / iMult) * iMult
         Return Math.Ceiling(afValue / iMult) * iMult

Revision as of 17:59, 1 May 2013

;=================================================================================
; Math Functions
; 
; Version History
; 0.1 - Initial Release
; 0.2 - Optimizations by Cipscis
;	IsNumberEven ,IsNumberOdd ,InRange
;=================================================================================

;=================================================================================
; 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

;=================================================================================
; 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

;=================================================================================
; 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