March 05, 2021, 07:12:01 AM

Author Topic: [bb] Box, Box2 (bb & bmax) by CS_TBL [ 1+ years ago ]  (Read 493 times)

Offline BlitzBot

[bb] Box, Box2 (bb & bmax) by CS_TBL [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Box, Box2 (bb & bmax)
Author : CS_TBL
Posted : 1+ years ago

Description : Box is a normal modulo, to keep any value within the 0..modulo range, but including negative values!

Box2 is nearly the same, except that it keeps a value within the given lo..hi range.

hm... example :P

if this is the range for box2: -4,2 then this is what the values -5..5 give:

1
-4
-3
-2
-1
0
1
-4
-3
-2
-1

Box works similary, except that its range is always 0..modulo, so -5..5 at a Box(<value>,3)would look like:

1
2
0
1
2
0
1
2
0
1
2
People who work with banks, pixmaps and other memchunks that can't go beyond the bounds will prolly see most use of all this, esp. when used for drawing graphics in such.


Code :
Code: BlitzBasic
  1. bmax:
  2.  
  3. Function Box:Int(value:Int,modulo:Int)
  4.         If modulo<1 modulo=1
  5.         Return ((value Mod modulo)+modulo) Mod modulo
  6. End Function
  7.  
  8. Function Box2:Int(value:Int,lo:Int,hi:Int)
  9.         Local o:Int
  10.         If lo>hi
  11.                 o=lo
  12.                 lo=hi
  13.                 hi=o
  14.         EndIf
  15.         Local Modulo:Int=hi-lo
  16.         value:-lo
  17.         If modulo<1 modulo=1
  18.         Return lo+((value Mod modulo)+modulo) Mod modulo
  19. End Function
  20.  
  21.  
  22. non-bmax:
  23.  
  24. Function Box(value,modulo)
  25.         If modulo<1 modulo=1
  26.         Return ((value Mod modulo)+modulo) Mod modulo
  27. End Function
  28.  
  29. Function Box2(value,lo,hi)
  30.         If lo>hi
  31.                 o=lo
  32.                 lo=hi
  33.                 hi=o
  34.         EndIf
  35.         Modulo=hi-lo
  36.         value=value-lo
  37.         If modulo<1 modulo=1
  38.         Return lo+((value Mod modulo)+modulo) Mod modulo
  39. End Function


Comments :


N(Posted 1+ years ago)

 I think this is known as 'clamping', actually.Or maybe 'looping' if the term wasn't already reserved for, well, loops.Hm... I still like clamping, since it's clamping the value within a range.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal