Skip to content

dart:math

Mathematical constants and functions, plus a random number generator.

To use this library in your code:

dart
import 'dart:math';

Random

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

dart
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.

dart
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.

dart
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).

dart
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).

dart
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

ClassDescription
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.
RandomA generator of random bool, int, or double values.
Rectangle<T extends num>A class for representing two-dimensional rectangles whose properties are immutable.

Functions

FunctionDescription
acosConverts x to a double and returns its arc cosine in radians.
asinConverts x to a double and returns its arc sine in radians.
atanConverts x to a double and returns its arc tangent in radians.
atan2A variant of atan.
cosConverts radians to a double and returns the cosine of the value.
expConverts x to a double and returns the natural exponent, e, to the power x.
logConverts x to a double and returns the natural logarithm of the value.
max<T extends num>Returns the larger of two numbers.
min<T extends num>Returns the lesser of two numbers.
powReturns x to the power of exponent.
sinConverts radians to a double and returns the sine of the value.
sqrtConverts x to a double and returns the positive square root of the value.
tanConverts radians to a double and returns the tangent of the value.

Constants

ConstantDescription
eBase of the natural logarithms.
ln10Natural logarithm of 10.
ln2Natural logarithm of 2.
log10eBase-10 logarithm of e.
log2eBase-2 logarithm of e.
piThe PI constant.
sqrt1_2Square root of 1/2.
sqrt2Square root of 2.