Last active
January 8, 2019 15:38
-
-
Save jackrugile/f12067b819099a2d108fca78cc3aca24 to your computer and use it in GitHub Desktop.
Some common calculation helpers I use in a lot of my demos and games.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Calc { | |
/* | |
------------------------------------------ | |
| rand:float - returns random float | |
| | |
| min:number - minimum value | |
| max:number - maximum value | |
| | |
| Get a random float between two values | |
------------------------------------------ */ | |
static rand(min, max) { | |
if(max === undefined) { | |
max = min; | |
min = 0; | |
} | |
return Math.random() * (max - min) + min; | |
} | |
/* | |
------------------------------------------ | |
| randInt:integer - returns random integer | |
| | |
| min:number - minimum value | |
| max:number - maximum value | |
| | |
| Get a random integer between two values | |
------------------------------------------ */ | |
static randInt(min, max) { | |
if(max === undefined) { | |
max = min; | |
min = 0; | |
} | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
/* | |
------------------------------------------ | |
| randArr:item - returns random item from array | |
| | |
| arr:array - the array to randomly pull from | |
| | |
| Get a random item from an array. | |
------------------------------------------ */ | |
static randArr(arr) { | |
return arr[Math.floor(Math.random() * arr.length)]; | |
} | |
/* | |
------------------------------------------ | |
| map:number - returns a mapped value | |
| | |
| val:number - input value | |
| inputMin:number - minimum of input range | |
| inputMax:number - maximum of input range | |
| outputMin:number - minimum of output range | |
| outputMax:number - maximum of output range | |
| | |
| Get a mapped value from and input min/max | |
| to an output min/max. | |
------------------------------------------ */ | |
static map(val, inputMin, inputMax, outputMin, outputMax) { | |
return ((outputMax - outputMin) * ((val - inputMin) / (inputMax - inputMin))) + outputMin; | |
} | |
/* | |
------------------------------------------ | |
| clamp:number - returns clamped value | |
| | |
| val:number - value to be clamped | |
| min:number - minimum of clamped range | |
| max:number - maximum of clamped range | |
| | |
| Clamp a value to a min/max range. | |
------------------------------------------ */ | |
static clamp(val, min, max) { | |
return Math.max(Math.min(val, max), min); | |
} | |
/* | |
------------------------------------------ | |
| ceilInterval:number - returns rounded up value | |
| | |
| value:number - value to be rounded | |
| interval:number - interval | |
| | |
| Ceil a value to the next highest interval. | |
------------------------------------------ */ | |
static ceilInterval(value, interval) { | |
if(value % interval === 0) { | |
value += 0.0001; | |
} | |
return Math.ceil(value / interval) * interval; | |
} | |
/* | |
------------------------------------------ | |
| floorInterval:number - returns rounded down value | |
| | |
| value:number - value to be rounded | |
| interval:number - interval | |
| | |
| Floor a value to the next lowest interval. | |
------------------------------------------ */ | |
static floorInterval(value, interval) { | |
if(value % interval === 0) { | |
value -= 0.0001; | |
} | |
return Math.floor(value / interval) * interval; | |
} | |
/* | |
------------------------------------------ | |
| roundInterval:number - returns rounded value | |
| | |
| value:number - value to be rounded | |
| interval:number - interval | |
| | |
| Round a value to the nearest interval. | |
------------------------------------------ */ | |
static roundInterval(value, interval) { | |
return Math.round(value / interval) * interval; | |
} | |
/* | |
------------------------------------------ | |
| getIndexFromCoords:number - returns index | |
| | |
| x:number - x value (column) | |
| y:number - y value (row) | |
| w:number - width of grid | |
| | |
| Convert from grid coords to index. | |
------------------------------------------ */ | |
static getIndexFromCoords(x, y, w) { | |
return x + (y * w); | |
} | |
/* | |
------------------------------------------ | |
| getCoordsFromIndex:object - returns coords | |
| | |
| i:number - index | |
| w:number - width of grid | |
| | |
| Convert from index to grid coords. | |
------------------------------------------ */ | |
static getCoordsFromIndex(i, w) { | |
return { | |
x: i % w, | |
y: Math.floor(i / w) | |
} | |
} | |
} |
Nice!
iem => item on row 39 =)
@liamegan Thanks for sharing, I need to look into that!
@DonKarlssonSan Thank you, fixed!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome stuff, the one I use more and more often is hermite interpolation a la: