RedKnight91

wrap(value, min, max)

May 6th, 2020
1,363
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///@func    wrap()
  2. ///@desc    Returns the value wrapped.  If it is above or below the threshold it will wrap around
  3. ///
  4. ///@param value
  5. ///@param min
  6. ///@param max
  7.  
  8. var value   = argument0
  9. var _min    = min(argument1, argument2)
  10. var _max    = max(argument1, argument2)
  11.  
  12. var isInt   = (value mod 1 == 0)
  13. var interval= (isInt) + abs(_max - _min)
  14. var wrapCost= (isInt) ? 1 : 0
  15.  
  16. if (value > _max) {
  17.     var diff = value - _max
  18.     var rest = (diff - wrapCost) % interval
  19.     value = _min + rest
  20. }
  21. else if (value < _min) {
  22.     var diff = _min - value
  23.     var rest = (diff - wrapCost) % interval
  24.     value = _max - rest
  25. }
  26.  
  27. return value
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×