A Vector math library for 2D and 3D applications.
position.xwz = color.grb;
.1. Add the following to your project's pubspec.yaml and run pub get
.
dependencies:
vector_math: any
2. Add the correct import for your project.
import 'package:vector_math/vector_math.dart';
Read the docs
1. Using the GLSL getter and setter syntax.
import 'package:vector_math/vector_math.dart';
void main() {
Vector3 x = new Vector3.zero(); // Zero vector
Vector4 y = new Vector4.all(4.0); // Vector with 4.0 in all lanes
x.zyx = y.xzz; // Sets z,y,x the values in x,z,z
}
2. Transforming a vector.
import 'dart:math';
import 'package:vector_math/vector_math.dart';
void main() {
// Rotation of PI/2 degrees around the Y axis followed by a
// translation of (5.0, 2.0, 3.0).
Matrix4 T = new Matrix4.rotationY(PI * 0.5)..translate(5.0, 2.0, 3.0);
// A point.
Vector3 position = new Vector3(1.0, 1.0, 1.0);
// Transform position by T.
T.transform3(position);
}
3. Invert a matrix
import 'dart:math';
import 'package:vector_math/vector_math.dart';
void main() {
// Rotation of 90 degrees around the Y axis followed by a
// translation of (5.0, 2.0, 3.0).
Matrix4 T = new Matrix4.rotationY(PI * 0.5)..translate(5.0, 2.0, 3.0);
// Invert T.
T.invert();
// Invert just the rotation in T.
T.invertRotation();
}
4. Rotate a vector using a quaternion
import 'dart:math';
import 'package:vector_math/vector_math.dart';
void main() {
// The X axis.
Vector3 axis = new Vector3(1.0, 0.0, 0.0);
// 90 degrees.
double angle = PI / 2.0;
// Quaternion encoding a 90 degree rotation along the X axis.
Quaternion q = new Quaternion.axisAngle(axis, angle);
// A point.
Vector3 point = new Vector3(1.0, 1.0, 1.0);
// Rotate point by q.
q.rotate(point);
}
5. Check if two axis aligned bounding boxes intersect
import 'package:vector_math/vector_math.dart';
void main() {
// Define the first box with a minimum and a maximum.
Aabb2 aabbOne = new Aabb2.minMax(new Vector2.zero(), new Vector2(4.0, 4.0));
// Define the second box
Aabb2 aabbTwo =
new Aabb2.minMax(new Vector2(5.0, 5.0), new Vector2(6.0, 6.0));
// Extend the second box to contain a point
aabbTwo.hullPoint(new Vector2(3.0, 3.0));
// Check if the two boxes intersect, returns true in this case.
bool intersect = aabbOne.intersectsWithAabb2(aabbTwo);
}
6. Check where a ray and a sphere intersect
import 'package:vector_math/vector_math.dart';
void main() {
// Define a ray starting at the origin and going into positive xdirection.
Ray ray = new Ray.originDirection(new Vector3.zero(), new Vector3(1.0, 0.0, 0.0));
// Defines a sphere with the center (5.0 0.0 0.0) and a radius of 2.
Sphere sphere = new Sphere.centerRadius(new Vector3(5.0, 0.0, 0.0), 2.0);
// Checks if the ray intersect with the sphere and returns the distance of the
// intersection from the origin of the ray. Would return null if no intersection
// is found.
double distanceFromOrigin = ray.intersectsWithSphere(sphere);
// Evaluate the position of the intersection, in this case (3.0 0.0 0.0).
Vector3 position = ray.at(distanceFromOrigin);
}
7. Work with colors
import 'package:vector_math/vector_math.dart';
void main() {
// Access a buildin color, colors are stored in 4dimensional vectors.
Vector4 red = Colors.red;
Vector4 gray = new Vector4.zero();
// Convert the red color to a grayscaled color.
Colors.toGrayscale(red, gray);
// Parse a blue color from a hex string.
Vector4 blue = new Vector4.zero();
Colors.fromHexString('#0000FF', blue);
// Convert the blue color from RGB to HSL.
Colors.rgbToHsl(blue, blue);
// Reduce the lightness of the color by 50%.
blue.z *= 0.5;
// Convert the HSL color back to RGB.
Colors.hslToRgb(blue, blue);
}
To run test cases:
~/src/vector_math/> pub run test:test
To automatically generate the latest version of vector_math_64
:
~/src/vector_math/> dart tool/generate_vector_math_64.dart
benchmark/
vector_math_64
.Add this to your package's pubspec.yaml file:
dependencies:
vector_math: ^2.0.8
You can install packages from the command line:
with pub:
$ pub get
with Flutter:
$ flutter packages get
Alternatively, your editor might support pub get
or flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:vector_math/vector_math.dart';
Version  Uploaded  Documentation  Archive 

2.0.8  Jul 23, 2018  
2.0.7  Apr 25, 2018  
2.0.6  Mar 20, 2018  
2.0.5  Jul 6, 2017  
2.0.4  Feb 6, 2017  
2.0.3  May 17, 2016  
2.0.2  May 17, 2016  
2.0.1  Apr 25, 2016  
2.0.0  Mar 15, 2016  
1.4.7  Feb 1, 2016 
Popularity:
Describes how popular the package is relative to other packages.
[more]

99

Health:
Code health derived from static analysis.
[more]

90

Maintenance:
Reflects how tidy and uptodate the package is.
[more]

79

Overall:
Weighted score of the above.
[more]

92

We analyzed this package on Feb 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter, web, other
No platform restriction found in primary library
package:vector_math/vector_math.dart
.
Fix lib/src/vector_math_geometry/generators/ring_generator.dart
. (1.99 points)
Analysis of lib/src/vector_math_geometry/generators/ring_generator.dart
reported 4 hints:
line 24 col 19: Use =
to separate a named parameter from its default value.
line 25 col 24: Use =
to separate a named parameter from its default value.
line 26 col 25: Use =
to separate a named parameter from its default value.
line 27 col 35: Use =
to separate a named parameter from its default value.
Fix lib/src/vector_math_geometry/generators/circle_generator.dart
. (1.49 points)
Analysis of lib/src/vector_math_geometry/generators/circle_generator.dart
reported 3 hints:
line 22 col 19: Use =
to separate a named parameter from its default value.
line 23 col 24: Use =
to separate a named parameter from its default value.
line 24 col 25: Use =
to separate a named parameter from its default value.
Fix lib/src/vector_math_geometry/generators/geometry_generator.dart
. (1.49 points)
Analysis of lib/src/vector_math_geometry/generators/geometry_generator.dart
reported 3 hints:
line 13 col 22: Use =
to separate a named parameter from its default value.
line 13 col 42: Use =
to separate a named parameter from its default value.
line 13 col 63: Use =
to separate a named parameter from its default value.
Fix additional 7 files with analysis or formatting issues. (5.99 points)
Additional issues in the following files:
lib/src/vector_math_lists/vector_list.dart
(3 hints)lib/src/vector_math/colors.dart
(2 hints)lib/src/vector_math_64/colors.dart
(2 hints)lib/src/vector_math_geometry/generators/sphere_generator.dart
(2 hints)lib/src/vector_math/aabb3.dart
(1 hint)lib/src/vector_math_64/aabb3.dart
(1 hint)lib/src/vector_math_geometry/generators/cylinder_generator.dart
(1 hint)The package description is too short. (11 points)
Add more detail to the description
field of pubspec.yaml
. Use 60 to 180 characters to describe the package, what it does, and its target use case.
Maintain an example. (10 points)
Create a short demo in the example/
directory to show how to use this package.
Common filename patterns include main.dart
, example.dart
, and vector_math.dart
. Packages with multiple examples should provide example/README.md
.
For more information see the pub package layout conventions.
Package  Constraint  Resolved  Available 

Direct dependencies  
Dart SDK  >=2.0.0dev.49.0 <3.0.0  
Dev dependencies  
benchmark_harness  any  
browser  any  
path  >=1.0.0 <2.0.0  
test  >=0.12.0 <0.13.0 