Range
A class that encompasses a range between two numbers.
Constructor
new Range(from: number, to: number)
Creates a range from the given number to another.
Note that from
does not necessarily need to be smaller than to
.
If it is greater, the range is considered inverted.
Public Properties
from
|
Gets the number that was passed in as the range’s left bound. |
to
|
Gets the number that was passed in as the range’s right bound. |
Public Mutator Methods
Sets the values of from and to to the new values.
|
|
Gets the number that was passed in as the range’s right bound. |
Public Computed Properties
Gets either from or to , depending on which is smaller.
|
|
Gets either from or to , depending on which is greater.
|
|
Gets the number in-between Example: |
|
Gets the total length of the range. This value will always be positive. Example: |
Public Non-Mutator Methods
Clamps Example: |
|
Returns true if the target range is completely contained in this range.
|
|
Checks if the number is contained in this range. A number is considered contained by the range if it lies in-between or on the bounds of this range. This function allows you to specify a tolerance to combat floating point inaccuracies. By default a tolerance of |
|
Checks if the number is in-between the end points of this range. This function differs from It is recommended not to use this function with floating point numbers. Use |
|
Returns true if some part of range overlaps with this range.
|
|
Finds the intersection point closest to this range’s center with the given range. If this range completely envelops the target range, returns this range’s center. If there is no intersection, returns NaN. | |
Gets the intersection between this range and range as a new Range. If there is no intersection, returns an empty Range. For example, a |
|
Gets the union between this range and range as a new Range. If there is no true union (that is, the two ranges do not intersect), the returned Range will "bridge the gap", so to speak, acting as if either of the ranges were large enough to meet the other. For example, a A |
|
If this range intersects with For example, a |
|
Gets the value located at value . For the returned value to be inside this range, value should be between 0.0 and 1.0.
|
|
Gets a relative value between 0.0 and 1.0 to indicate the position of the passed value inside the range. This function is the counter-component to at() .
|
|
Wraps the number into this range. That is, if the number exceeds this range, it will "wrap around" the range's start. For instance, in a range of -3 to 3, the value 4 would be wrapped to -2. |
|
Returns true if the range is empty, that is its length is 0.
|
|
Checks if the given range is identical to this one. | |
Checks if this range has the given from and to values.
|
|
Clones the range. |
Remarks
Note that all computed properties like min()
or max()
are cached after the
first call and only recomputed if the range bounds change. As a result, there is
no need to store their results in temporary variables to avoid recomputation.
There is also a read-only interface of Range
available called ReadonlyRange
.
Due to conflicts with the
Range
from lib.dom.d.ts
, you may wish to create a new range using
Numbers.range()
instead of using the constructor directly.