API / geotoolkit / util / MathUtil / MathUtil
The class Math contains methods for performing basic numeric operations and basic algorithms.
Constructors
- [new MathUtil()](/solutions/geotoolkit/GeoToolkit_JS/apis/classes/geotoolkit.util.mathutil.mathutil.md#new mathutil())
Methods
Accessors
• Static get epsilon(): number
constant for epsilon equal to 1E-10
number
• Static get logMin(): number
constant for minimum logarithmic value to be display equal to 1E-10
number
• Static get quickSort(): (array: any[] | number[], lo: number, hi: number, comparator: Comparator, swap: Swap) => void
Quick sort (in-place).
fn
▸ (array, lo, hi, comparator, swap): void
Quick sort (in-place).
| Name | Type | Description |
|---|---|---|
array | any[] | number[] | array of numbers or objects |
lo | number | starting index |
hi | number | ending index (including) |
comparator | Comparator | optional a function that defines an alternative sort order. The function should return a negative, zero, or positive value, depending on the arguments, like: function(a, b){return a-b} When the sort() method compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value. |
swap | Swap | optional swap function to swap two elements of collection, like: function(array, i, j) { } |
void
• Static get sortPointsClockwise(): (xArray: number[], yArray: number[], isClockwise?: boolean) => void
Sort given array clockwise around their center point.
fn
▸ (xArray, yArray, isClockwise?): void
Sort given array clockwise around their center point.
| Name | Type | Description |
|---|---|---|
xArray | number[] | x-coordinate of points |
yArray | number[] | y-coordinate of points |
Optional isClockwise | boolean |
void
Methods
▸ Static calcCubicBezier(first, p1, p2, last, flatness?): Point[]
Bezier curves are often used to model smooth curves. calcCubicBezier Performs cubic Bezier approximation. The control points P1 and P2 are only used to provide directional information.The arc or circle is divided into four equal sections and each section fit to a cubic Bézier curve. When points are spaced far apart approximation is performed based on the precision(flatness) and control points on the same side of vector.
| Name | Type | Description |
|---|---|---|
first | Point | first point |
p1 | Point | control point #1 |
p2 | Point | control point #2 |
last | Point | last point |
Optional flatness | number | precision |
Point[]
array of interpolated points if "param" is undefined or is an Array instance
▸ Static calculateNeatLimits(min, max, logScale?, centerOnZeroOnNegativeMin?, splitOnZero?): Range
This function computes human readable limits so that the returned interval is easily apprehended by the user.<br> To do so, it will compute a minimum and maximum that can be easily divided by 2.<br> Which means the user will immediately guess the mid and quarter values.<br> <br> The purpose of this function is not to provide accurately rounded limits but limits that will be easily apprehended.<br> Using the given limits can virtually remove the necessity of displaying the intermediates labels on a axis.<br> <br> For example it will return [0, 4000] for the values [790, 3050].<br> The user will easily guess the mid value to be 2000.<br>
| Name | Type | Description |
|---|---|---|
min | number | The minimum value |
max | number | The maximum value |
Optional logScale | boolean | If this should use a logarithmic scale instead of linear |
Optional centerOnZeroOnNegativeMin | boolean | If this should center the limits around 0 if minimum is a negative value |
Optional splitOnZero | boolean | If this should split neat limits result around 0, applicable only when Math.sign(minimum) != Math.sign(maximum) |
The computed neat limits
▸ Static calculateNiceLimits(min, max, desiredNumberOfTicks, minPadding?, maxPadding?): Limits
Create a "nice" range based on "Algorithm for Optimal Scaling on a Chart Axis".
| Name | Type | Description |
|---|---|---|
min | number | minimum value |
max | number | maximum value |
desiredNumberOfTicks | number | desired number of ticks |
Optional minPadding | boolean | add step padding at first or not |
Optional maxPadding | boolean | add step padding at the end or not |
calculated limits
▸ Static calculateRoundedLimits(min, max, marginPercentage?, marginPercentageTolerance?): Range
Computes rounded limits using margin percentages as constraints.<br> <br> This function tries to compute a 'smart' range that includes the given min/max.<br> To do so it will try to simplify the decimal part of the given values to the closest value.<br> The original range is always contained in the returned range, meaning that the returned 'low' is lesser than the given min and the returned 'high' is greater than the given max.<br> <br> The given margin parameters will be used to constrain rounding process:<br> <ul> <li>Margin:<br> The margin percentage determines how much margin the algorithm will try to introduce.<br> If the given margin percentage is 10%, then the returned 'high'' will be around:<br> <i>high = max + 10% * (max - min)</i>.<br> </li> <br> <li>Margin tolerance:<br> The margin tolerance percentage determines how much the 'smart rounding' process can change the value computed above.<br> If the given margin percentage is 4%, then the returned 'high' will be in the range:<br> <i>[high - <b>0.02</b> * (max - min), high + <b>0.04</b> * (max - min)]</i>.<br> Note that margin tolerance is applied differently for inner tolerance.<br> <pre> ------------------|-----------<----|--------> max +10% (margin)
-2% +4% (margin tolerance) ------------------|-----------<----|--------> high (before rounding) </pre>
Example
import {MathUtil} from '@int/geotoolkit/util/MathUtil';
// With default margin :
MathUtil.calculateRoundedLimits(347.242, 372.110); // returns Range(345, 374);
// With bigger margin (10%) :
MathUtil.calculateRoundedLimits(347.242, 372.110, 0.10); // returns Range(344, 375);
// With values close to zero :
MathUtil.calculateRoundedLimits(-0.0041, 0.012); // returns Range(-0.005, 0.013);| Name | Type | Description |
|---|---|---|
min | number | The minimum value to be rounded |
max | number | The maximum value to be rounded |
Optional marginPercentage | number | Percentage of margin desired |
Optional marginPercentageTolerance | number | Percentage of margin tolerance desired |
The computed range
▸ Static clamp(value, min, max): number
Clamp the value to the range
| Name | Type | Description |
|---|---|---|
value | number | current value |
min | number | minimum value |
max | number | maximum value |
number
clamped value
▸ Static computeStatistics(array): Statistics
Compute Statistics for a collection
| Name | Type | Description |
|---|---|---|
array | number[] | raw number array |
▸ Static degToRad(deg): number
Convert degree to radians
| Name | Type | Description |
|---|---|---|
deg | number | The angle in degrees |
number
The angle in radians
▸ Static equals(v1, v2, epsilon?): boolean
Compare two values with specified precision
| Name | Type | Description |
|---|---|---|
v1 | number | 1st number to compare |
v2 | number | 2nd number to compare |
Optional epsilon | number | difference bet 2 numbers to compare |
boolean
▸ Static findIndex<T>(value, values, fromIndex?, toIndex?, compareFunction?): number
Uses binary search to find an index of given value. If given value is found, it returns the exact index. If given value is not found it returns ~[next closest index].
| Name |
|---|
T |
| Name | Type | Description |
|---|---|---|
value | T | object to find |
values | T[] | sorted array on objects |
Optional fromIndex | number | index to start the search |
Optional toIndex | number | index to end search scope |
Optional compareFunction | (x: T, y: T) => number | Order | compare function that defines the sort order. If omitted, the object type must be number. The second parameter passed to this compareFunction is the value passed to findIndex(). OR the data order. Ascending by default. |
number
▸ Static findIndex<T>(value, values, fromIndex?, toIndex?, compareFunction?): number
| Name |
|---|
T |
| Name | Type |
|---|---|
value | number |
values | T[] |
Optional fromIndex | number |
Optional toIndex | number |
Optional compareFunction | Order | (x: T, y: number) => number |
number
▸ Static findNiceLimits(min, max, modelStep, minPadding?, maxPadding?): Limits
Find a "nice" settings based on "Algorithm for Optimal Scaling on a Chart Axis".
| Name | Type | Description |
|---|---|---|
min | number | minimum value |
max | number | maximum value |
modelStep | number | desired model space between ticks |
Optional minPadding | boolean | add step padding at first or not |
Optional maxPadding | boolean | add step padding at the end or not |
calculated limits
▸ Static getCentile(values, centileNum): number
Get centile
| Name | Type | Description |
|---|---|---|
values | number[] | the percent value |
centileNum | number | percentile |
number
▸ Static getLimits(arr, init?, nullvalue?): number[]
Return the min and max values of a given array.<br> This function ignores NaN and null values.
| Name | Type | Description |
|---|---|---|
arr | number[] | represents the array |
Optional init | number[] | represents the initials min and max values: [min, max] |
Optional nullvalue | number | A nullvalue to ignore, note that null and NaN are ignored |
number[]
An array containing the [min, max]
▸ Static getMax(arr, init?, nullvalue?): number
Return the max values of a given array.<br> This function ignores NaN and null values.
| Name | Type | Description |
|---|---|---|
arr | number[] | represents the array. |
Optional init | number | represents the initial max value |
Optional nullvalue | number | A nullvalue to ignore, note that null and NaN are ignored |
number
the minimum valid value in the given array.
▸ Static getMin(arr, init?, nullvalue?): number
Return the min value of a given array. it internally filter NaN and null values.
| Name | Type | Description |
|---|---|---|
arr | number[] | represents the array. |
Optional init | number | represents the initial min value |
Optional nullvalue | number | A nullvalue to ignore, note that null and NaN are ignored |
number
the minimum valid value in the given array.
▸ Static getSeededRandom(min?, max?, seed?): number
Used for tutorials to create a reproducible random value set.
| Name | Type | Description |
|---|---|---|
Optional min | number | value of random |
Optional max | number | value of random |
Optional seed | number | seed |
number
▸ Static getWeightedArithmeticMean(intervals, weights, power?): number
Gets weighted arithmetic mean (or weighted average)
Throws
Error if input arrays are not valid or sum of weights is zero
| Name | Type | Description |
|---|---|---|
intervals | number[] | array of intervals |
weights | number[] | array of their respective weights |
Optional power | number | weight power |
number
weighted arithmetic mean
▸ Static isNumber(value): value is number
Checks whether value is a finite number.
| Name | Type | Description |
|---|---|---|
value | any | value to test |
value is number
▸ Static isNumeric(value): value is number
Checks whether value is a finite number.
| Name | Type | Description |
|---|---|---|
value | any | value to test |
value is number
▸ Static isPlatformLittleEndian(): boolean
Checks if current platform is little endian
boolean
▸ Static log10(val, precision?, handlenegative?): number
Calculates log base 10
Example
import {MathUtil} from '@int/geotoolkit/util/MathUtil';
MathUtil.log10(-10, null, true); // will give -1 result and MathUtil.log10(-10) will be NaN| Name | Type | Description |
|---|---|---|
val | number | value to calculate log base 10 |
Optional precision | number | the number of digits after the decimal point |
Optional handlenegative | boolean | set true if handling of negative value and 0 is required when mapping |
number
▸ Static niceNumber(value, round?): number
Returns a "nice" number approximately equal to range Rounds the number if round = true takes the ceiling if round = false.
| Name | Type | Description |
|---|---|---|
value | number | input number |
Optional round | boolean | round value |
number
output number
▸ Static normalizeAngle(angle): number
Normalizes the provided angle to be in range [0, 2*PI]
| Name | Type | Description |
|---|---|---|
angle | number | Angle to normalize |
number
▸ Static radToDeg(rad): number
Convert radians to degree
| Name | Type | Description |
|---|---|---|
rad | number | The angle in radians |
number
The angle in degrees
▸ Static round(value, base?): number
Rounds value with the provided accuracy base
Example
import {MathUtil} from '@int/geotoolkit/util/MathUtil';
MathUtil.round(12.3456, 1000); // 12.346, rounds to 1/1000 accuracy
MathUtil.round(12.3456, 100); // 12.35, rounds to 1/100 accuracy
MathUtil.round(12.3456, 10); // 12.3, rounds to 1/10 accuracy
MathUtil.round(12.3456, 4); // 12.25, rounds to 1/4 accuracy| Name | Type | Description |
|---|---|---|
value | number | value to round |
Optional base | number | accuracy base to round |
number
▸ Static roundTo(src, precision): number
Rounds "src" to relative (see example below) "precision"
Example
import {MathUtil} from '@int/geotoolkit/util/MathUtil';
const a = 0.0000056789;
const resA = MathUtil.roundTo(a, 2); // resA = 0.00000568
const b = 56789.56789;
const resB = MathUtil.roundTo(b, 2); // resB = 56800| Name | Type | Description |
|---|---|---|
src | number | source value to be rounded |
precision | number | relative precision |
number
rounded value
▸ Static sign(a): number
Get sign of specified value
| Name | Type | Description |
|---|---|---|
a | number | current value |
number
-1 for negative | 0 for zero | +1 for positive