number-pairs
Javascript utility for dealing with ranges of numbers and/or dates expressed as length-2 arrays.
contains
tests if a range expressed as a pair of numbers contains some other value
contains([2,6],4)
contains([2,6],7)
A range is considered to contain its lower bound but not its upper:
contains([2,6],2)
contains([2,6],5.99)
contains([2,6],6)
centredOn
Creates a new range of the same size, but centred around a new value (think panning):
centredOn([2,3], 10)
mid
Calculates the midpoint of a pair of numbers
mid(-5, 5)
mid(5, 10)
pairExtent
pairExtent(-1, 10)
interpolateBetweenPair
interpolateBetweenPair([5,10], 0.5)
interpolateBetweenPair([5,10], 0)
interpolateBetweenPair([5,10], 1)
rangesOverlap
rangesOverlap([1,4],[3,8])
rangesOverlap([1,4],[5,8])
overlap
overlap(a,b,c,d)
rangesOverlap([a,b], [c,d])
expandPair
Expand a range so that it the returned value is proptionally larger (or smaller)
than the given value.
it holds that:
extent( expand( r, n ) ) / extent(r) === n
so, to double the extent of a range:
expand( r, 2 )
to half the extent of a range:
expand( r, 0.5 )
max
Enforces the maximum extent of a range, while keeping the centre point the same (think viewport minimum)
max([a,b], maxExtent)
min
Enforces the minimum extent of a range, while keeping the centre point the same (think viewport minimum)
min([a,b], minExtent)