dart:math library Null safety

Mathematical constants and functions, plus a random number generator.

To use this library in your code:

import 'dart:math';

Random

Random is a generator of bool, int or double values.

var intValue = Random().nextInt(10); // Value is >= 0 and < 10.
var doubleValue = Random().nextDouble(); // Value is >= 0.0 and < 1.0.
var boolValue = Random().nextBool(); // true or false, with equal chance.

Point

Point is a utility class for representing two-dimensional positions.

var leftTop = const Point(0, 0);
var rightBottom = const Point(200, 400);

Rectangle

Rectangle is a class for representing two-dimensional axis-aligned rectangles whose properties are immutable.

Create a rectangle spanned by the points.

var leftTop = const Point(20, 50);
var rightBottom = const Point(300, 600);
var rectangle = Rectangle.fromPoints(leftTop, rightBottom);
print(rectangle.left); // 20
print(rectangle.top); // 50
print(rectangle.right); // 300
print(rectangle.bottom); // 600

Create a rectangle spanned by (left, top) and (left+width, top+height).

var rectangle = const Rectangle(20, 50, 300, 600);
print(rectangle.left); // 20
print(rectangle.top); // 50
print(rectangle.right); // 320
print(rectangle.bottom); // 650

MutableRectangle

MutableRectangle is a class for representing two-dimensional axis-aligned rectangles with mutable properties.

Create a mutable rectangle spanned by (left, top) and (left+width, top+height).

var rectangle = MutableRectangle(20, 50, 300, 600);
print(rectangle); // Rectangle (20, 50) 300 x 600
print(rectangle.left); // 20
print(rectangle.top); // 50
print(rectangle.right); // 320
print(rectangle.bottom); // 650

// Change rectangle width and height.
rectangle.width = 200;
rectangle.height = 100;
print(rectangle); // Rectangle (20, 50) 200 x 100
print(rectangle.left); // 20
print(rectangle.top); // 50
print(rectangle.right); // 220
print(rectangle.bottom); // 150

Classes

MutableRectangle<T extends num>
A class for representing two-dimensional axis-aligned rectangles with mutable properties.
Point<T extends num>
A utility class for representing two-dimensional positions.
Random
A generator of random bool, int, or double values.
Rectangle<T extends num>
A class for representing two-dimensional rectangles whose properties are immutable.

Constants

e → const double
Base of the natural logarithms.
2.718281828459045
ln10 → const double
Natural logarithm of 10.
2.302585092994046
ln2 → const double
Natural logarithm of 2.
0.6931471805599453
log10e → const double
Base-10 logarithm of e.
0.4342944819032518
log2e → const double
Base-2 logarithm of e.
1.4426950408889634
pi → const double
The PI constant.
3.1415926535897932
sqrt1_2 → const double
Square root of 1/2.
0.7071067811865476
sqrt2 → const double
Square root of 2.
1.4142135623730951

Functions

acos(num x) double
Converts x to a double and returns its arc cosine in radians.
asin(num x) double
Converts x to a double and returns its arc sine in radians.
atan(num x) double
Converts x to a double and returns its arc tangent in radians.
atan2(num a, num b) double
A variant of atan.
cos(num radians) double
Converts radians to a double and returns the cosine of the value.
exp(num x) double
Converts x to a double and returns the natural exponent, e, to the power x.
log(num x) double
Converts x to a double and returns the natural logarithm of the value.
max<T extends num>(T a, T b) → T
Returns the larger of two numbers.
min<T extends num>(T a, T b) → T
Returns the lesser of two numbers.
pow(num x, num exponent) num
Returns x to the power of exponent.
sin(num radians) double
Converts radians to a double and returns the sine of the value.
sqrt(num x) double
Converts x to a double and returns the positive square root of the value.
tan(num radians) double
Converts radians to a double and returns the tangent of the value.