RiptideRiptide
Download Riptide SDKInstall Riptorch
Menu
Raya docs · Geometry, physics and speed

Vectors, quaternions and matrices

Allocation-free vector, quaternion and matrix types over SIMD and typed data, spherical harmonics and low-discrepancy sequences.

import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'package:riptide_math/ratio_laws.ray';
77 classes697 functions and methods326 of 897 documented623 runnable examples

Overview

Engine math: vectors, quaternions and matrices that don't allocate in hot loops, spherical harmonics for diffuse lighting, and low-discrepancy sequences for sampling.

What's in it

FileContents
vec.rayVec2, Vec3 (dot, cross by SIMD shuffles, reflect, lerp), Vec4 (perspective divide)
quat.rayQuat: axis-angle, Hamilton product, fast rotate (v + 2w(u \times v) + 2u \times (u \times v)), shortest-arc slerp
mat.rayMat4: TRS, look-at, perspective (z in [0,1] for Metal/Vulkan/WebGPU), multiply, transpose, inverse; Mat3; Mat4Buffer
sh.rayOrder-2 real spherical harmonics (9 coefficients), RGB projection and cosine-convolved irradiance (Ramamoorthi and Hanrahan 2001)
sequences.rayRadical inverse, van der Corput, Hammersley, Halton, Roberts R2, uniform sphere mapping, GGX importance sampling (Karis 2013)

Ratio geometry

The one geometry API for Riptide: quadrance, spread, cross, twist, solid and dihedral spreads, reflections, and rotations from rational Cayley parameters or two reflections (Turn, RatioQuat), against a metric (Metric2, Metric3), with radians only through RatioBoundary. RatioF holds every float formula once (inlined: the machine code is the formula's); RatioBackend is the contract for f64, f32, exact int64 (Int64Ratio) and, in riptide_rational_geometry, exact fields (FieldRatio); RatioBatch / RatioMsl are the batched CPU and Metal kernels. package:riptide_math/ratio_laws.ray checks any backend against the laws.

Exact numbers

  • Decimal: exact (BigInt unscaled × 10^{-\mathrm{scale}}) for money. fromMinorUnits / toMinorUnits match riptide_finance_core's integer minor units (USD 2, JPY 0, BHD 3), with 8 rounding modes (half-even by default). Compare with equals/compareTo (numeric), not ==, which compares the representation.
  • U8, U16, U32, U64 (extension types over int) and U128, I128 (over a pair of ints): wrapping operators, checked* (throw IntegerOverflow), saturating*, bit ops, shifts and parse. Tested against BigInt on random values.

AOT, 2M iterations (benchmark/int_bench.ray): U128 multiply-add 168.8 ns against 754.8 ns for BigInt mod 2^{128} (same result); a Decimal add is 57 ns.

Functions and constants

Functions

gjkfunction
List<Vec3>? gjk(Convex a, Convex b, {int maxIterations = 64})

GJK intersection test (Gilbert, Johnson and Keerthi 1988): A and B overlap iff their Minkowski difference A - B contains the origin. Returns the final simplex (4 points when intersecting) for epa, or null when separated.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = SphereShape(Vec3(0, 0, 0), 1), b = BoxShape(Vec3(1.5, 0, 0), Vec3(0.75, 0.5, 0.5));
  final c = BoxShape(Vec3(3, 0, 0), Vec3(0.5, 0.5, 0.5));
  print('sphere vs near box: ${gjk(a, b) != null ? 'overlap' : 'apart'}');
  print('sphere vs far box: ${gjk(a, c) != null ? 'overlap' : 'apart'}');
}
Output
sphere vs near box: overlap
sphere vs far box: apart
epafunction
({Vec3 normal, double depth})? epa(Convex a, Convex b, List<Vec3> simplex, {int maxIterations = 128, double tolerance = 1e-4})

EPA (Expanding Polytope Algorithm, van den Bergen 2001): from GJK's enclosing simplex, expands toward the boundary of A - B to find the penetration depth and normal: moving B by normal × depth separates the shapes.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = SphereShape(Vec3(0, 0, 0), 1), b = SphereShape(Vec3(1.5, 0, 0), 1);
  final s = gjk(a, b)!;
  final p = epa(a, b, s)!;
  print('penetration depth ${f(p.depth)} along ${v3(p.normal)} (exact 0.5)');
}
Output from the Raya VM
penetration depth 0.5000 along (1.0000, 0.0000, 0.0093) (exact 0.5)
obbOverlapfunction
bool obbOverlap(Obb a, Obb b)

Separating axis theorem for two oriented boxes: they're disjoint iff some axis among the 3 + 3 face normals and the 9 edge cross products separates their projections (Gottschalk et al. 1996).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final r = Quat.axisAngle(Vec3.unitZ, math.pi / 4);
  Obb box(double x, Quat q) => Obb(Vec3(x, 0, 0), [q.rotate(Vec3.unitX), q.rotate(Vec3.unitY), Vec3.unitZ], Vec3(1, 1, 1));
  print('axis-aligned at 0, turned 45° at 2.3: ${obbOverlap(box(0, Quat.identity), box(2.3, r))}');
  print('turned at 2.5: ${obbOverlap(box(0, Quat.identity), box(2.5, r))}');
}
Output
axis-aligned at 0, turned 45° at 2.3: true
turned at 2.5: false
sphereDepthfunction
double sphereDepth(SphereShape a, SphereShape b)

Distance helper used by tests and physics: penetration depth of two spheres (negative when separated).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = SphereShape(Vec3(0, 0, 0), 1);
  for (final x in [1.5, 2.0, 2.5]) {
    print('centres $x apart: depth ${sphereDepth(a, SphereShape(Vec3(x, 0, 0), 1))}');
  }
}
Output
centres 1.5 apart: depth 0.5
centres 2.0 apart: depth 0.0
centres 2.5 apart: depth -0.5
bezierfunction
Vec3 bezier(List<Vec3> ctrl, double t)

Bézier curve of any degree by de Casteljau's algorithm: repeated linear interpolation of the control polygon (numerically stable, gives the point and the subdivision).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final ctrl = [Vec3(0, 0, 0), Vec3(1, 3, 0), Vec3(3, 3, 0), Vec3(4, 0, 0)];
  final c = [for (var i = 0; i <= 40; i++) bezier(ctrl, i / 40)];
  print('B(0.5) = ${v3(bezier(ctrl, 0.5))}');
}
Output from the Raya VM
B(0.5) = (2.0000, 2.2500, 0.0000)
catmullRomfunction
Vec3 catmullRom(Vec3 p0, Vec3 p1, Vec3 p2, Vec3 p3, double t, {double alpha = 0.5})

Uniform cubic Catmull–Rom through p_{1} \to p_{2} with neighbours p0, p3, centripetal parameterisation (\alpha = 0.5) by default, which avoids cusps and self-intersections (Yuksel et al. 2011).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final ctrl = [Vec3(0, 0, 0), Vec3(1, 3, 0), Vec3(3, 3, 0), Vec3(4, 0, 0)];
  final c = [for (var i = 0; i <= 30; i++) catmullRom(ctrl[0], ctrl[1], ctrl[2], ctrl[3], i / 30)];
  print('passes through P1 at t = 0: ${v3(c.first)}, P2 at t = 1: ${v3(c.last)}');
}
Output from the Raya VM
passes through P1 at t = 0: (1.0000, 3.0000, 0.0000), P2 at t = 1: (3.0000, 3.0000, 0.0000)
deBoorfunction
Vec3 deBoor(int degree, List<double> knots, List<Vec3> ctrl, double u, {List<double>? weights})

B-spline / NURBS evaluation by de Boor's algorithm. knots has ctrl.length + degree + 1 entries; weights (optional) makes it a NURBS curve (rational: evaluate in homogeneous coordinates, then divide by w).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final ctrl = [Vec3(0, 0, 0), Vec3(1, 2, 0), Vec3(2, -1, 0), Vec3(3, 2, 0), Vec3(4, 0, 0)];
  final knots = clampedKnots(ctrl.length, 3);
  final c = [for (var i = 0; i <= 50; i++) deBoor(3, knots, ctrl, i / 50)];
  print('knots $knots');
}
Output from the Raya VM
knots [0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 1.0, 1.0, 1.0]
clampedKnotsfunction
List<double> clampedKnots(int count, int degree)

A clamped uniform knot vector for count control points of degree (curve starts/ends at the end points).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('4 points, degree 3: ${clampedKnots(4, 3)}');
  print('6 points, degree 2: ${clampedKnots(6, 2)}');
}
Output
4 points, degree 3: [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0]
6 points, degree 2: [0.0, 0.0, 0.0, 0.25, 0.5, 0.75, 1.0, 1.0, 1.0]
rayTrianglefunction
({double t, double u, double v})? rayTriangle(Ray ray, Vec3 a, Vec3 b, Vec3 c, {bool cullBack = false, double tMin = 1e-7})

Ray–triangle intersection (Möller and Trumbore 1997): solves o + t d = (1-u-v) a + u b + v c by Cramer's rule. Returns (t, u, v) for the closest hit with t > tMin, or null. Hits from both sides count unless cullBack.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final ray = Ray(Vec3(0, 0, 5), Vec3(0, 0, -1));
  final h = rayTriangle(ray, Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(0, 1, 0));
  print('t = ${f(h!.t)}, barycentric u = ${f(h.u)}, v = ${f(h.v)}');
  print('miss: ${rayTriangle(Ray(Vec3(5, 5, 5), Vec3(0, 0, -1)), Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(0, 1, 0))}');
}
Output
t = 5.0000, barycentric u = 0.2500, v = 0.5000
miss: null
rayAabbfunction
({double tNear, double tFar})? rayAabb(Ray ray, Vec3 min, Vec3 max, {double tMax = double.infinity})

Ray–AABB slab test (Kay and Kajiya 1986; branchless form after Williams et al. 2005). Returns the entry/exit t-interval clipped to [0, tMax], or null.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final ray = Ray(Vec3(-3, 0.5, 0), Vec3(1, 0.2, 0).normalized());
  final h = rayAabb(ray, Vec3(-1, -1, -1), Vec3(1, 1, 1))!;
  final a = ray.at(h.tNear), b = ray.at(h.tFar);
  print('enters at t = ${f(h.tNear)}, leaves at t = ${f(h.tFar)}');
}
Output
enters at t = 2.0396, leaves at t = 2.5495
raySpherefunction
double? raySphere(Ray ray, Vec3 center, double radius)

Ray–sphere: the smallest t \ge 0 where |o + t d - c| = r, or null. Uses the numerically stable half-b form.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final ray = Ray(Vec3(-3, 0.5, 0), Vec3(1, 0, 0));
  final t = raySphere(ray, Vec3.zero, 1)!;
  final p = ray.at(t);
  print('t = ${f(t)}, hit ${v3(p)}');
}
Output
t = 2.1340, hit (-0.8660, 0.5000, 0.0000)
rayPlanefunction
double? rayPlane(Ray ray, Plane plane)

Ray–plane: t where the ray meets the plane, or null when parallel or behind.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final ground = Plane.fromPointNormal(Vec3.zero, Vec3.unitY);
  final ray = Ray(Vec3(0, 3, 0), Vec3(1, -1, 0).normalized());
  final t = rayPlane(ray, ground)!;
  print('hits y = 0 at ${v3(ray.at(t))}');
}
Output from the Raya VM
hits y = 0 at (3.0000, 0.0000, 0.0000)
Vec3 closestPointOnSegment(Vec3 p, Vec3 a, Vec3 b)

The point on segment [a, b] closest to p.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(0, 0, 0), b = Vec3(4, 2, 0);
  final ps = [Vec3(1, 3, 0), Vec3(-2, 1, 0), Vec3(5, 4, 0)];
  for (final p in ps) {
    print('${v3(p)} -> ${v3(closestPointOnSegment(p, a, b))}');
  }
}
Output
(1.0000, 3.0000, 0.0000) -> (2.0000, 1.0000, 0.0000)
(-2.0000, 1.0000, 0.0000) -> (0.0000, 0.0000, 0.0000)
(5.0000, 4.0000, 0.0000) -> (4.0000, 2.0000, 0.0000)
Vec3 closestPointOnTriangle(Vec3 p, Vec3 a, Vec3 b, Vec3 c)

The point on triangle abc closest to p, by Voronoi regions (Ericson, "Real-Time Collision Detection", 5.1.5).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(0, 0, 0), b = Vec3(4, 0, 0), c = Vec3(1, 3, 0);
  final ps = [Vec3(2, 1, 2), Vec3(5, 2, 0), Vec3(-1, -1, 0)];
  for (final p in ps) {
    print('${v3(p)} -> ${v3(closestPointOnTriangle(p, a, b, c))}');
  }
}
Output
(2.0000, 1.0000, 2.0000) -> (2.0000, 1.0000, 0.0000)
(5.0000, 2.0000, 0.0000) -> (3.5000, 0.5000, 0.0000)
(-1.0000, -1.0000, 0.0000) -> (0.0000, 0.0000, 0.0000)
({Float64List values, Mat3d vectors}) symmetricEigen3(Mat3d a, {int maxSweeps = 32})

Eigen-decomposition of a symmetric 3 \times 3 matrix by cyclic Jacobi rotations: A = V \operatorname{diag}(\lambda) V^{T}. Eigenvalues are sorted descending; V's columns are the matching unit eigenvectors (a right-handed rotation).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Mat3d.of([4, 1, 0, 1, 3, 1, 0, 1, 2]);
  final e = symmetricEigen3(a);
  print('eigenvalues ${e.values.map(f).toList()}');
}
Output
eigenvalues [4.7321, 3.0000, 1.2679]
svd3function
({Mat3d u, Float64List sigma, Mat3d v}) svd3(Mat3d a)

Singular value decomposition A = U \sum V^{T} of a 3 \times 3 matrix (\sigma descending, ≥ 0), via the eigen-decomposition of A^{T}A. U and V are rotations; if \det(A) < 0 the smallest \sigma is negated so U, V stay proper (the "signed SVD" used for polar decomposition and shape matching).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Mat3d.of([2, 0, 0, 0, 0, 3, 0, -1, 0]);
  final s = svd3(a);
  print('singular values ${s.sigma.map(f).toList()}');
}
Output
singular values [3.0000, 2.0000, 1.0000]
polar3function
({Mat3d r, Mat3d s}) polar3(Mat3d a)

Polar decomposition A = R S with R the closest rotation to A and S symmetric positive semi-definite.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // A rotation times a stretch: the polar decomposition recovers both.
  final a = Mat3d.of([0, -2, 0, 1, 0, 0, 0, 0, 1]);
  final p = polar3(a);
  print('R = ${[for (var i = 0; i < 9; i++) f(p.r.at(i ~/ 3, i % 3))]}');
  print('S = ${[for (var i = 0; i < 9; i++) f(p.s.at(i ~/ 3, i % 3))]}');
}
Output
R = [0.0000, -1.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000]
S = [1.0000, 0.0000, 0.0000, 0.0000, 2.0000, 0.0000, 0.0000, 0.0000, 1.0000]
solveLinearfunction
bool solveLinear(Float64List m, Float64List b, int n)

Solves the n \times n system M x = b in place by Gaussian elimination with partial pivoting (row-major m). Returns false when singular.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Float64List.fromList([2, 1, 1, 1, 3, 2, 1, 0, 0]);
  final b = Float64List.fromList([4, 5, 6]);
  final ok = solveLinear(m, b, 3);
  print('solved: $ok, x = $b');
  print('singular: ${solveLinear(Float64List.fromList([1, 2, 2, 4]), Float64List.fromList([1, 1]), 2)}');
}
Output
solved: true, x = [6.0, 15.000000000000004, -23.000000000000004]
singular: false
convexHull3function
List<(int, int, int)> convexHull3(Float32x4List pts)

Convex hull of a 3D point set by the incremental method with conflict-free face visibility (the same horizon-stitching step QuickHull uses; this version visits points in input order instead of farthest-first, so it is O(n^{2}) worst case). Returns triangles as index triples, wound counter-clockwise seen from outside.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final rng = math.Random(1);
  final pts = Float32x4List.fromList([for (var i = 0; i < 30; i++) Float32x4(rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), 0)]);
  final faces = convexHull3(pts);
  print('${faces.length} triangles on the hull of 30 points');
}
Output from the Raya VM
95 triangles on the hull of 30 points
earClipfunction
List<(int, int, int)> earClip(List<Vec2> poly)

Ear-clipping triangulation of a simple polygon (2D, either winding). Returns index triples into poly.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final poly = [Vec2(0, 0), Vec2(4, 0), Vec2(4, 3), Vec2(2, 1), Vec2(0, 3)];
  final tris = earClip(poly);
  print('${tris.length} triangles: $tris');
}
Output
3 triangles: [(1, 2, 3), (0, 1, 3), (0, 3, 4)]
delaunay2function
List<(int, int, int)> delaunay2(List<Vec2> pts)

Delaunay triangulation of 2D points by Bowyer–Watson: insert points one by one, remove triangles whose circumcircle contains the point, re-triangulate the hole. O(n^{2}) in this simple form.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final rng = math.Random(4);
  final pts = [for (var i = 0; i < 25; i++) Vec2(rng.nextDouble() * 10, rng.nextDouble() * 10)];
  final tris = delaunay2(pts);
  print('${tris.length} triangles');
}
Output
40 triangles
Float32x4List computeTangents(Float32x4List positions, Float32x4List normals, Float32List uvs, Uint32List indices)

Per-vertex tangent frames for normal mapping (Lengyel's method: solve for the UV-gradient directions per triangle, accumulate, Gram–Schmidt against the normal, store handedness in w). MikkTSpace adds specific accumulation and splitting rules for bit-exact agreement with bakers; this follows the same math but not its exact ordering.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // A unit square of two triangles, with positions, normals and uvs.
  final pos = Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(1, 0, 0, 0), Float32x4(1, 1, 0, 0), Float32x4(0, 1, 0, 0)]);
  final idx = Uint32List.fromList([0, 1, 2, 0, 2, 3]);
  final nrm = Float32x4List.fromList(List.filled(4, Float32x4(0, 0, 1, 0)));
  final uv = Float32List.fromList([0, 0, 1, 0, 1, 1, 0, 1]);
  final t = computeTangents(pos, nrm, uv, idx);
  print('tangent at vertex 0: (${t[0].x}, ${t[0].y}, ${t[0].z}), handedness ${t[0].w}');
}
Output
tangent at vertex 0: (1.0, 0.0, 0.0), handedness 1.0
simplifyQemfunction
({Float32x4List positions, Uint32List indices}) simplifyQem(Float32x4List positions, Uint32List indices, int targetTriangles)

Quadric error metric simplification (Garland and Heckbert 1997): each vertex accumulates the sum of squared distances to its faces' planes as a 4 \times 4 quadric Q; collapsing edge (i, j) to v costs v^{T}(Q_{i} + Q_{j})v. Edges are collapsed cheapest-first until targetTriangles remain. Returns new positions and indices.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // A 6 × 6 flat grid: the quadric error metric can merge it down to few triangles.
  const n = 6;
  final pos = Float32x4List.fromList([for (var i = 0; i < n; i++) for (var j = 0; j < n; j++) Float32x4(i.toDouble(), j.toDouble(), 0, 0)]);
  final idx = Uint32List.fromList([for (var i = 0; i < n - 1; i++) for (var j = 0; j < n - 1; j++) ...[i * n + j, (i + 1) * n + j, (i + 1) * n + j + 1, i * n + j, (i + 1) * n + j + 1, i * n + j + 1]]);
  final s = simplifyQem(pos, idx, 8);
  print('${idx.length ~/ 3} -> ${s.indices.length ~/ 3} triangles');
}
Output
50 -> 8 triangles
kahanSumfunction
double kahanSum(Iterable<double> values)

Compensated summation: Neumaier's variant of Kahan summation keeps the low-order bits lost by each addition, so summing many floats of different magnitude stays accurate.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final xs = List<double>.filled(1000000, 0.1);
  var naive = 0.0;
  for (final x in xs) {
    naive += x;
  }
  print('naive  $naive');
  print('Kahan  ${kahanSum(xs)}');
}
Output
naive  100000.00000133288
Kahan  100000.0
double contactImpulse({required Float32x4List vel, required Float32List invMass, required int a, required int b, required Vec3 normal, required double accumulated, double restitution = 0})

One sequential-impulse contact solve along normal n (Catto 2005): computes the impulse that removes approaching velocity (with restitution), clamps the accumulated impulse to ≥ 0 so contacts only push, and applies it to the two bodies' linear velocities. Returns the new accumulated impulse.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // Two bodies approaching along x at 1 m/s each; one contact normal (1, 0, 0) from a to b.
  final vel = Float32x4List.fromList([Float32x4(1, 0, 0, 0), Float32x4(-1, 0, 0, 0)]);
  final inv = Float32List.fromList([1, 1]);
  final j = contactImpulse(vel: vel, invMass: inv, a: 0, b: 1, normal: Vec3.unitX, accumulated: 0, restitution: 1);
  print('impulse $j; velocities now ${vel[0].x}, ${vel[1].x} (elastic bounce)');
}
Output
impulse 2.0; velocities now -1.0, 1.0 (elastic bounce)
springDampfunction
({double value, double velocity}) springDamp(double value, double velocity, double target, double omega, double dt)

Critically damped spring smoothing toward a target (exact solution of x'' = -2\omega x' - \omega^{2} (x − target), no overshoot, frame-rate independent). omega = 2 / smoothTime is a good default.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  var v = 0.0, x = 0.0;
  final path = <List<double>>[];
  for (var i = 0; i < 60; i++) {
    final s = springDamp(x, v, 1, 8, 1 / 30);
    x = s.value;
    v = s.velocity;
    path.add([i / 30, x]);
  }
  print('after 2 s: ${f(x)} (target 1, no overshoot)');
}
Output
after 2 s: 1.0000 (target 1, no overshoot)
poissonDiskfunction
List<Vec2> poissonDisk(double width, double height, double r, {int k = 30, int seed = 1})

Poisson-disk (blue noise) points in the width × height rectangle with minimum distance r (Bridson 2007).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final pts = poissonDisk(10, 10, 1, seed: 3);
  var minD = 1e9;
  for (var i = 0; i < pts.length; i++) {
    for (var j = i + 1; j < pts.length; j++) {
      minD = math.min(minD, (pts[i] - pts[j]).length);
    }
  }
  print('${pts.length} points, closest pair ${f(minD)} (≥ 1)');
}
Output
67 points, closest pair 1.0040 (≥ 1)
rotateSh9function
Float32List rotateSh9(Float32List coeffs, Vec3 Function(Vec3) inverseRotate)

Rotation of order-2 SH coefficients (9 per channel) by re-projection: SH bands are closed under rotation, so evaluating the function at 9 fixed directions rotated by R^{-1} and solving the 9 \times 9 basis system gives the rotated coefficients exactly (up to round-off). Slower than the Ivanic–Ruedenberg recurrence but simple and exact.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // Light from +x projected to SH, then rotated 90° about z: it should come from +y.
  final sh = ShRgb()..addSample(Vec3.unitX, Vec3(1, 1, 1), 1);
  final red = Float32List.fromList([for (var i = 0; i < 9; i++) sh.coeffs[3 * i]]);
  final q = Quat.axisAngle(Vec3.unitZ, math.pi / 2);
  final r = rotateSh9(red, (d) => q.conjugate.rotate(d));
  final y = Float32List(9);
  Sh9.basis(Vec3.unitY, y);
  var atY = 0.0;
  for (var i = 0; i < 9; i++) {
    atY += r[i] * y[i];
  }
  print('rotated light seen from +y: ${f(atY)}');
}
Output
rotated light seen from +y: -0.0439

Classes

class

Bvh

final class Bvh

A bounding volume hierarchy over triangles, built top-down with the binned surface area heuristic (SAH; Wald 2007): split where C = C_{\mathrm{trav}} + (\frac{A_{L}}{A}) N_{L} C_{\mathrm{isect}} + (\frac{A_{R}}{A}) N_{R} C_{\mathrm{isect}} is lowest. Nodes are flat arrays (structure of arrays), so traversal allocates nothing per node.

Constructors

Bvh.buildfactory
factory Bvh.build(Float32x4List positions, Uint32List indices, {int maxLeaf = 4, int bins = 12})

Builds over positions (one Float32x4 per vertex, w ignored) and indices (3 per triangle).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // A unit square of two triangles, with positions, normals and uvs.
  final pos = Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(1, 0, 0, 0), Float32x4(1, 1, 0, 0), Float32x4(0, 1, 0, 0)]);
  final idx = Uint32List.fromList([0, 1, 2, 0, 2, 3]);
  final bvh = Bvh.build(pos, idx);
  final h = bvh.raycast(Ray(Vec3(0.75, 0.25, 1), Vec3(0, 0, -1)));
  print('hit triangle ${h!.triangle} at t = ${f(h.t)}');
}
Output
hit triangle 0 at t = 1.0000

Properties

final Float32x4List positions
indicesfield
final Uint32List indices
orderfield
final Uint32List order
final Float32x4List boundsMin
final Float32x4List boundsMax
leftfield
final Int32List left

Child node indices; -1 for a leaf.

rightfield
final Int32List right
firstfield
final Int32List first
countfield
final Int32List count
nodeCountgetter
int get nodeCount

Methods

raycastmethod
({int triangle, double t, double u, double v})? raycast(Ray ray)

The closest triangle hit by ray: (triangle, t, u, v) or null. Children are visited near-first.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // A unit square of two triangles, with positions, normals and uvs.
  final pos = Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(1, 0, 0, 0), Float32x4(1, 1, 0, 0), Float32x4(0, 1, 0, 0)]);
  final idx = Uint32List.fromList([0, 1, 2, 0, 2, 3]);
  final bvh = Bvh.build(pos, idx);
  for (final (x, y) in [(0.2, 0.7), (0.9, 0.1), (2.0, 2.0)]) {
    final h = bvh.raycast(Ray(Vec3(x, y, 1), Vec3(0, 0, -1)));
    print('($x, $y): ${h == null ? 'miss' : 'triangle ${h.triangle}, u ${f(h.u)}, v ${f(h.v)}'}');
  }
}
Output
(0.2, 0.7): triangle 1, u 0.2000, v 0.5000
(0.9, 0.1): triangle 0, u 0.8000, v 0.1000
(2.0, 2.0): miss
class

Convex

abstract interface class Convex

A convex shape described only by its support function: the farthest point in direction d. GJK and EPA work on any such shape (spheres, boxes, capsules, convex hulls, and their Minkowski sums).

Methods

supportmethod
Vec3 support(Vec3 d)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // Every convex shape answers one question: the farthest point in a direction.
  final List<Convex> shapes = [SphereShape(Vec3.zero, 1), BoxShape(Vec3.zero, Vec3(2, 1, 1)), CapsuleShape(Vec3(-1, 0, 0), Vec3(1, 0, 0), 0.5)];
  for (final s in shapes) {
    print('${s.runtimeType}: support(1, 1, 0) = ${v3(s.support(Vec3(1, 1, 0)))}');
  }
}
Output
SphereShape: support(1, 1, 0) = (0.7071, 0.7071, 0.0000)
BoxShape: support(1, 1, 0) = (2.0000, 1.0000, 1.0000)
CapsuleShape: support(1, 1, 0) = (1.3536, 0.3536, 0.0000)
final class SphereShape implements Convex

Constructors

SphereShapeconstructor
const SphereShape(this.center, this.radius)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final s = SphereShape(Vec3(1, 2, 0), 1.5);
  final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
  print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}
Output
support(+x) = (2.5000, 2.0000, 0.0000)

Properties

centerfield
final Vec3 center
radiusfield
final double radius

Methods

supportmethod
Vec3 support(Vec3 d)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final s = SphereShape(Vec3.zero, 1);
  final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
  print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}
Output
support(+x) = (1.0000, 0.0000, 0.0000)
final class BoxShape implements Convex

Constructors

BoxShapeconstructor
const BoxShape(this.center, this.halfExtents)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final s = BoxShape(Vec3.zero, Vec3(2, 1, 0.5));
  final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
  print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}
Output
support(+x) = (2.0000, 1.0000, 0.5000)

Properties

centerfield
final Vec3 center
final Vec3 halfExtents

Methods

supportmethod
Vec3 support(Vec3 d)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final s = BoxShape(Vec3(1, 1, 0), Vec3(1, 0.5, 0.5));
  final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
  print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}
Output
support(+x) = (2.0000, 1.5000, 0.5000)
final class PointCloudShape implements Convex

Constructors

PointCloudShapeconstructor
PointCloudShape(this.points)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final s = PointCloudShape(Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(2, 0, 0, 0), Float32x4(1, 2, 0, 0), Float32x4(1, 0.5, 0, 0)]));
  final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
  print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}
Output
support(+x) = (2.0000, 0.0000, 0.0000)

Properties

pointsfield
final Float32x4List points

Methods

supportmethod
Vec3 support(Vec3 d)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final s = PointCloudShape(Float32x4List.fromList([for (var k = 0; k < 7; k++) Float32x4(math.cos(k * 0.9) * 2, math.sin(k * 0.9), 0, 0)]));
  final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
  print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}
Output
support(+x) = (2.0000, 0.0000, 0.0000)
final class CapsuleShape implements Convex

A capsule: the segment a–b swept by a sphere of radius.

Constructors

CapsuleShapeconstructor
const CapsuleShape(this.a, this.b, this.radius)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final s = CapsuleShape(Vec3(-1, 0, 0), Vec3(1, 0, 0), 0.5);
  final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
  print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}
Output
support(+x) = (1.5000, 0.0000, 0.0000)

Properties

afield
final Vec3 a
bfield
final Vec3 b
radiusfield
final double radius

Methods

supportmethod
Vec3 support(Vec3 d)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final s = CapsuleShape(Vec3(0, -1, 0), Vec3(0, 1, 0), 0.3);
  final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
  print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}
Output from the Raya VM
support(+x) = (0.3000, 1.0000, 0.0000)
final class TransformedConvex implements Convex

A convex shape in local coordinates moved by a rotation and translation: \operatorname{support}(d) = p + R \cdot s(R^{T}d). Lets one local shape (a hull, a box) be tested at any pose without copying its points.

Constructors

const TransformedConvex(this.shape, this.rotation, this.translation)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final s = TransformedConvex(BoxShape(Vec3.zero, Vec3(1, 0.3, 0.3)), Quat.axisAngle(Vec3.unitZ, math.pi / 6), Vec3(2, 1, 0));
  final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
  print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}
Output
support(+x) = (3.0160, 1.2402, 0.3000)

Properties

shapefield
final Convex shape
final Quat rotation
final Vec3 translation

Methods

supportmethod
Vec3 support(Vec3 d)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final s = TransformedConvex(SphereShape(Vec3.zero, 1), Quat.identity, Vec3(3, 0, 0));
  final pts = [for (var k = 0; k < 24; k++) s.support(Vec3(math.cos(k * math.pi / 12), math.sin(k * math.pi / 12), 0))];
  print('support(+x) = ${v3(s.support(Vec3.unitX))}');
}
Output
support(+x) = (4.0000, 0.0000, 0.0000)
class

Obb

final class Obb

Oriented box for separating-axis tests: center, 3 unit axes, half extents.

Constructors

Obbconstructor
const Obb(this.center, this.axes, this.halfExtents)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q = Quat.axisAngle(Vec3.unitZ, 0.5);
  final o = Obb(Vec3(1, 1, 0), [q.rotate(Vec3.unitX), q.rotate(Vec3.unitY), Vec3.unitZ], Vec3(2, 1, 0.5));
  final ax = o.axes, h = o.halfExtents;
  Vec3 corner(double sx, double sy) => o.center + ax[0].scale(sx * h.x) + ax[1].scale(sy * h.y);
  print('projected radius on x: ${f(o.radiusOn(Vec3.unitX))}');
}
Output
projected radius on x: 2.2346

Properties

centerfield
final Vec3 center
axesfield
final List<Vec3> axes
final Vec3 halfExtents

Methods

radiusOnmethod
double radiusOn(Vec3 l)

Projection radius of this box onto axis l.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final o = Obb(Vec3.zero, [Vec3.unitX, Vec3.unitY, Vec3.unitZ], Vec3(2, 1, 0.5));
  for (final l in [Vec3.unitX, Vec3.unitY, Vec3(1, 1, 0).normalized()]) {
    print('radius on ${v3(l)}: ${f(o.radiusOn(l))}');
  }
}
Output
radius on (1.0000, 0.0000, 0.0000): 2.0000
radius on (0.0000, 1.0000, 0.0000): 1.0000
radius on (0.7071, 0.7071, 0.0000): 2.1213
class

Color

abstract final class Color

Colour spaces and transfer functions. Renderers light in linear space; displays expect sRGB-encoded values; film pipelines grade in ACEScg; OKLab is perceptually uniform for gradients and palettes (Ottosson 2020).

Static

srgbToLinearstaticmethod
static double srgbToLinear(double c)

sRGB electro-optical transfer: encoded → linear (IEC 61966-2-1).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  for (final c in [0.0, 0.04045, 0.5, 1.0]) {
    print('sRGB $c -> linear ${f(Color.srgbToLinear(c))}');
  }
}
Output
sRGB 0.0 -> linear 0.0000
sRGB 0.04045 -> linear 0.0031
sRGB 0.5 -> linear 0.2140
sRGB 1.0 -> linear 1.0000
linearToSrgbstaticmethod
static double linearToSrgb(double c)

Linear → sRGB encoded.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('linear 0.214 -> sRGB ${f(Color.linearToSrgb(0.214))} (about 0.5)');
  print('round trip: ${f(Color.linearToSrgb(Color.srgbToLinear(0.3)))}');
}
Output
linear 0.214 -> sRGB 0.5000 (about 0.5)
round trip: 0.3000
luminancestaticmethod
static double luminance(Vec3 rgb)

Relative luminance of linear Rec.709/sRGB (BT.709 weights).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('white ${f(Color.luminance(Vec3(1, 1, 1)))}, red ${f(Color.luminance(Vec3(1, 0, 0)))}, green ${f(Color.luminance(Vec3(0, 1, 0)))}, blue ${f(Color.luminance(Vec3(0, 0, 1)))}');
}
Output
white 1.0000, red 0.2126, green 0.7152, blue 0.0722
linearSrgbToAcesCgstaticmethod
static Vec3 linearSrgbToAcesCg(Vec3 c)

Linear sRGB (D65) → ACEScg (AP1, D60), including the Bradford adaptation (matrix from the ACES reference).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final c = Color.linearSrgbToAcesCg(Vec3(1, 0, 0));
  print('sRGB red in ACEScg: ${v3(c)}');
  print('white stays white: ${v3(Color.linearSrgbToAcesCg(Vec3(1, 1, 1)))}');
}
Output
sRGB red in ACEScg: (0.6131, 0.0701, 0.0206)
white stays white: (1.0001, 1.0000, 0.9999)
acesCgToLinearSrgbstaticmethod
static Vec3 acesCgToLinearSrgb(Vec3 c)

ACEScg → linear sRGB (inverse of the above).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final c = Color.acesCgToLinearSrgb(Color.linearSrgbToAcesCg(Vec3(0.2, 0.5, 0.8)));
  print('round trip ${v3(c)}');
}
Output
round trip (0.2000, 0.5000, 0.8000)
linearSrgbToOklabstaticmethod
static Vec3 linearSrgbToOklab(Vec3 c)

Linear sRGB → OKLab (L, a, b).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  for (final (n, c) in [('white', Vec3(1, 1, 1)), ('red', Vec3(1, 0, 0)), ('green', Vec3(0, 1, 0)), ('blue', Vec3(0, 0, 1))]) {
    print('${n.padRight(5)} -> Lab ${v3(Color.linearSrgbToOklab(c))}');
  }
  final hues = [for (var k = 0; k < 12; k++) Color.linearSrgbToOklab(Vec3(0.5 + 0.5 * math.cos(k * math.pi / 6), 0.5 + 0.5 * math.cos(k * math.pi / 6 - 2.094), 0.5 + 0.5 * math.cos(k * math.pi / 6 + 2.094)))];
}
Output
white -> Lab (1.0000, 0.0000, 0.0000)
red   -> Lab (0.6280, 0.2249, 0.1258)
green -> Lab (0.8664, -0.2339, 0.1795)
blue  -> Lab (0.4520, -0.0325, -0.3115)
oklabToLinearSrgbstaticmethod
static Vec3 oklabToLinearSrgb(Vec3 lab)

OKLab → linear sRGB.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // Mix two colours in Oklab for a perceptually even gradient.
  final a = Color.linearSrgbToOklab(Vec3(1, 0, 0)), b = Color.linearSrgbToOklab(Vec3(0, 0, 1));
  final mid = Color.oklabToLinearSrgb(a.lerp(b, 0.5));
  print('red–blue midpoint in Oklab: ${v3(mid)}');
}
Output
red–blue midpoint in Oklab: (0.2637, 0.0866, 0.3628)
acesFilmstaticmethod
static double acesFilm(double x)

ACES filmic tone-mapping curve fit (Narkowicz 2015), applied per channel to linear HDR, result in [0, 1].

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print([for (final x in [0.0, 0.18, 1.0, 4.0, 16.0]) f(Color.acesFilm(x))]);
}
Output
[0.0000, 0.2669, 0.8038, 0.9734, 1.0000]
class

Half

abstract final class Half

IEEE 754 binary16 ("half") packing for GPU buffers and HDR textures: 1 sign, 5 exponent, 10 mantissa bits.

Static

packstaticmethod
static int pack(double value)

Float → half bits, round-to-nearest-even, with overflow to infinity, NaN kept, and subnormals.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  for (final v in [1.0, 0.1, 65504.0, 1e-8]) {
    final h = Half.pack(v);
    print('$v -> 0x${h.toRadixString(16).padLeft(4, '0')} -> ${Half.unpack(h)}');
  }
}
Output
1.0 -> 0x3c00 -> 1.0
0.1 -> 0x2e66 -> 0.0999755859375
65504.0 -> 0x7bff -> 65504.0
1e-8 -> 0x0000 -> 0.0
unpackstaticmethod
static double unpack(int h)

Half bits → float.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print([for (final h in [0x3c00, 0xc000, 0x7bff, 0x0400]) Half.unpack(h)]);
}
Output
[1.0, -2.0, 65504.0, 0.00006103515625]
final class ArcLengthTable

Arc-length table for a parametric curve: sample samples segments, accumulate chord lengths, then map an arc length s back to t by binary search + linear interpolation (constant-speed motion along curves).

Constructors

ArcLengthTableconstructor
ArcLengthTable(Vec3 Function(double t) curve, {int samples = 256})
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // Move at constant speed along y = x²: tAt(s) maps distance to the parameter.
  Vec3 curve(double t) => Vec3(2 * t, 4 * t * t, 0);
  final tab = ArcLengthTable(curve);
  final even = [for (var k = 0; k <= 10; k++) curve(tab.tAt(k / 10))];
  final naive = [for (var k = 0; k <= 10; k++) curve(k / 10)];
  print('parameter at half the length: ${f(tab.tAt(0.5))}');
}
Output
parameter at half the length: 0.2232

Properties

lengthsfield
final Float64List lengths
totalgetter
double get total

Methods

tAtmethod
double tAt(double s)

The parameter t at arc length s (clamped to [0, total]).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final tab = ArcLengthTable((t) => Vec3(math.cos(math.pi * t), math.sin(math.pi * t), 0));
  print([for (final s in [0.0, 0.25, 0.5, 0.75, 1.0]) f(tab.tAt(s))]);
}
Output
[0.0000, 0.0796, 0.1592, 0.2387, 0.3183]
enum RoundingMode

How to round when a result needs fewer digits than it has.

Values

halfEven

Toward the nearest; ties to the even neighbour (banker's rounding — no statistical bias over many roundings).

halfUpvalue
halfUp

Toward the nearest; ties away from zero (the usual school rounding).

halfDown

Toward the nearest; ties toward zero.

downvalue
down

Toward zero (truncate).

upvalue
up

Away from zero.

floorvalue
floor

Toward -\infty.

ceilingvalue
ceiling

Toward +\infty.

unnecessary

Throw if any non-zero digit would be dropped.

extension type

Decimal

extension type const Decimal._((BigInt, int) _r) implements Object

An exact decimal. Immutable; equality is numeric (1.50 = 1.5).

Constructors

factory Decimal.raw(BigInt unscaled, int scale)

unscaled × 10^(−scale); scale ≥ 0.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final d = Decimal.raw(BigInt.from(123456), 3);
  print('${d.toPlainString()} (unscaled 123456, scale 3)');
}
Output
123.456 (unscaled 123456, scale 3)
factory Decimal.fromInt(int v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print((Decimal.fromInt(7) * Decimal.parse('0.1')).toPlainString());
}
Output
0.7
factory Decimal.fromMinorUnits(int minor, int digits)

Integer minor units with digits minor digits: fromMinorUnits(1999, 2) == 19.99.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final d = Decimal.fromMinorUnits(1999, 2); // cents
  print('${d.toPlainString()} dollars');
}
Output
19.99 dollars
factory Decimal.parse(String s)

Parses "-12.3400", "1e-3", "+7". Throws FormatException for anything else.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Decimal.parse('0.1'), b = Decimal.parse('0.2');
  print('0.1 + 0.2 = ${(a + b).toPlainString()} (doubles give ${0.1 + 0.2})');
}
Output
0.1 + 0.2 = 0.3 (doubles give 0.30000000000000004)

Static

zerostaticfield
static final zero
onestaticfield
static final one

Properties

unscaledgetter
BigInt get unscaled
scalegetter
int get scale
signgetter
int get sign

Methods

dividemethod
Decimal divide(Decimal o, {required int scale, RoundingMode mode = RoundingMode.halfEven})

Quotient rounded to scale digits after the point with mode.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final total = Decimal.parse('100.00');
  final share = total.divide(Decimal.fromInt(3), scale: 2);
  print('100 / 3 to cents: ${share.toPlainString()}');
  print('halfUp at 4 places: ${Decimal.fromInt(2).divide(Decimal.fromInt(3), scale: 4, mode: RoundingMode.halfUp).toPlainString()}');
}
Output
100 / 3 to cents: 33.33
halfUp at 4 places: 0.6667
setScalemethod
Decimal setScale(int newScale, {RoundingMode mode = RoundingMode.halfEven})

This rounded (or padded) to newScale digits after the point.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final d = Decimal.parse('2.345');
  for (final m in RoundingMode.values.where((m) => m.name != "unnecessary")) {
    print('${m.name.padRight(9)} ${d.setScale(2, mode: m).toPlainString()}');
  }
}
Output
halfEven  2.34
halfUp    2.35
halfDown  2.34
down      2.34
up        2.35
floor     2.34
ceiling   2.35
int toMinorUnits(int digits, {RoundingMode mode = RoundingMode.halfEven})

Integer minor units for a currency with digits minor digits, rounding with mode; throws if the result doesn't fit a 64-bit int.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
  final t = (price * qty * (Decimal.fromInt(1) + tax));
  print('total ${t.toPlainString()} -> ${t.toMinorUnits(2)} cents');
}
Output
total 64.917525 -> 6492 cents
compareTomethod
int compareTo(Decimal o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
  print(price.compareTo(Decimal.parse('19.990')));
  print(price.compareTo(Decimal.parse('20')));
}
Output
0
-1
equalsmethod
bool equals(Decimal o)

Numeric equality (scale-insensitive). Use this rather than ==, which on extension types compares the representation (so 1.50 and 1.5 would differ).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
  print('19.99 equals 19.990: ${price.equals(Decimal.parse('19.990'))}');
}
Output
19.99 equals 19.990: true
absmethod
Decimal abs()
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(Decimal.parse('-3.50').abs().toPlainString());
}
Output
3.50
Decimal normalized()

Removes trailing zeros (1.500 \to 1.5).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(Decimal.parse('12.5000').normalized().toPlainString());
}
Output
12.5
String toPlainString()

Plain notation with exactly scale digits after the point ("-0.050").

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(Decimal.raw(BigInt.from(-5), 4).toPlainString());
}
Output
-0.0005
toDoublemethod
double toDouble()

Nearest double (for display or charts only — never for money arithmetic).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
  print(price.toDouble());
}
Output
19.99

Operators

operator +operator
Decimal operator +(Decimal o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
  print('price + tax = ${(price + tax).toPlainString()}');
}
Output
price + tax = 20.0725
operator -operator
Decimal operator -(Decimal o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
  print('price - tax = ${(price - tax).toPlainString()}');
}
Output
price - tax = 19.9075
operator -operator
Decimal operator -()
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
  print((-price).toPlainString());
}
Output
-19.99
operator *operator
Decimal operator *(Decimal o)

Exact product (scales add).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
  print('price * tax = ${(price * tax).toPlainString()}');
}
Output
price * tax = 1.649175
operator <operator
bool operator <(Decimal o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
  print('price < 20: ${price < Decimal.fromInt(20)}');
}
Output
price < 20: true
operator <=operator
bool operator <=(Decimal o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
  print('price <= 20: ${price <= Decimal.fromInt(20)}');
}
Output
price <= 20: true
operator >operator
bool operator >(Decimal o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
  print('price > 20: ${price > Decimal.fromInt(20)}');
}
Output
price > 20: false
operator >=operator
bool operator >=(Decimal o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final price = Decimal.parse('19.99'), qty = Decimal.fromInt(3), tax = Decimal.parse('0.0825');
  print('price >= 20: ${price >= Decimal.fromInt(20)}');
}
Output
price >= 20: false
final class IntegerOverflow implements Exception

Thrown by checked arithmetic when the exact result doesn't fit the type.

Constructors

IntegerOverflowconstructor
IntegerOverflow(this.op)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  try {
    U8(200) + U8(100);
    U8(200).checkedAdd(U8(100));
  } on IntegerOverflow catch (e) {
    print('caught: ${e.op}');
  }
}
Output
caught: 200 + 100

Properties

opfield
final String op

Methods

toStringmethod
String toString()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print(IntegerOverflow('U16 4000 * 20'));
}
Output
IntegerOverflow: U16 4000 * 20
extension type

U8

extension type const U8._(int v) implements Object

Constructors

U8factory
factory U8(int v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print('U8(200) = ${U8(200).value}');
  try {
    U8(256);
  } catch (e) {
    print('U8(256) is out of range: ${e.runtimeType}');
  }
  print('max = ${U8.max.value}');
}
Output
U8(200) = 200
U8(256) is out of range: IntegerOverflow
max = 255
U8.wrapfactory
factory U8.wrap(int v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print('wrap(261) = ${U8.wrap(261).value}');
  print('wrap(−1) = ${U8.wrap(-1).value}');
}
Output
wrap(261) = 5
wrap(−1) = 255

Static

maxconstant
static const max = U8._(255)
parsestaticmethod
static U8 parse(String s, {int radix = 10})
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print(U8.parse('200').value);
  print(U8.parse('ff', radix: 16).value);
}
Output
200
255

Properties

valuegetter
int get value

Methods

U8 checkedAdd(U8 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U8(200), b = U8(100);
  print('small: ${attempt(() => U8(3).checkedAdd(U8(2)).value)}');
  print('a + b: ${attempt(() => a.checkedAdd(b).value)}');
}
Output
small: 5
a + b: IntegerOverflow: 200 + 100
U8 checkedSub(U8 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U8(200), b = U8(100);
  print('small: ${attempt(() => U8(3).checkedSub(U8(2)).value)}');
  print('b - a: ${attempt(() => b.checkedSub(a).value)}');
}
Output
small: 1
b - a: IntegerOverflow: 100 - 200
U8 checkedMul(U8 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U8(200), b = U8(100);
  print('small: ${attempt(() => U8(3).checkedMul(U8(2)).value)}');
  print('a * b: ${attempt(() => a.checkedMul(b).value)}');
}
Output
small: 6
a * b: IntegerOverflow: 200 * 100
U8 saturatingAdd(U8 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U8(200), b = U8(100);
  print('a + b clamps to ${a.saturatingAdd(b).value} (max)');
  print('small: ${U8(3).saturatingAdd(U8(4)).value}');
}
Output
a + b clamps to 255 (max)
small: 7
U8 saturatingSub(U8 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U8(200), b = U8(100);
  print('b − a clamps to ${b.saturatingSub(a).value}');
  print('a − b = ${a.saturatingSub(b).value}');
}
Output
b − a clamps to 0
a − b = 100

Operators

operator +operator
U8 operator +(U8 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U8(200), b = U8(100);
  print('a + b wraps to ${(a + b).value}');
}
Output
a + b wraps to 44
operator -operator
U8 operator -(U8 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U8(200), b = U8(100);
  print('b − a wraps to ${(b - a).value}');
}
Output
b − a wraps to 156
operator *operator
U8 operator *(U8 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U8(200), b = U8(100);
  print('a × b wraps to ${(a * b).value}');
}
Output
a × b wraps to 32
operator &operator
U8 operator &(U8 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U8(0xf0), y = U8(0x3c);
  print('x & y = 0x${(x & y).value.toRadixString(16)}');
}
Output
x & y = 0x30
operator |operator
U8 operator |(U8 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U8(0xf0), y = U8(0x3c);
  print('x | y = 0x${(x | y).value.toRadixString(16)}');
}
Output
x | y = 0xfc
operator ^operator
U8 operator ^(U8 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U8(0xf0), y = U8(0x3c);
  print('x ^ y = 0x${(x ^ y).value.toRadixString(16)}');
}
Output
x ^ y = 0xcc
operator ~operator
U8 operator ~()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U8(0xf0), y = U8(0x3c);
  print('~x = 0x${(~x).value.toRadixString(16)}');
}
Output
~x = 0xf
operator <<operator
U8 operator <<(int s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U8(0xf0), y = U8(0x3c);
  print('x << 4 = 0x${(x << 4).value.toRadixString(16)} (high bits fall off)');
}
Output
x << 4 = 0x0 (high bits fall off)
operator >>operator
U8 operator >>(int s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U8(0xf0), y = U8(0x3c);
  print('x >> 4 = 0x${(x >> 4).value.toRadixString(16)}');
}
Output
x >> 4 = 0xf
operator <operator
bool operator <(U8 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U8(200), b = U8(100);
  print('a < b: ${a < b}, b < a: ${b < a}');
}
Output
a < b: false, b < a: true
operator >operator
bool operator >(U8 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U8(200), b = U8(100);
  print('a > b: ${a > b}');
}
Output
a > b: true
extension type

U16

extension type const U16._(int v) implements Object

Constructors

U16factory
factory U16(int v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print('U16(60000) = ${U16(60000).value}');
  try {
    U16(65536);
  } catch (e) {
    print('U16(65536) is out of range: ${e.runtimeType}');
  }
  print('max = ${U16.max.value}');
}
Output
U16(60000) = 60000
U16(65536) is out of range: IntegerOverflow
max = 65535
U16.wrapfactory
factory U16.wrap(int v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print('wrap(65541) = ${U16.wrap(65541).value}');
  print('wrap(−1) = ${U16.wrap(-1).value}');
}
Output
wrap(65541) = 5
wrap(−1) = 65535

Static

maxconstant
static const max = U16._(0xffff)
parsestaticmethod
static U16 parse(String s, {int radix = 10})
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print(U16.parse('60000').value);
  print(U16.parse('ff', radix: 16).value);
}
Output
60000
255

Properties

valuegetter
int get value

Methods

U16 checkedAdd(U16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U16(60000), b = U16(7000);
  print('small: ${attempt(() => U16(3).checkedAdd(U16(2)).value)}');
  print('a + b: ${attempt(() => a.checkedAdd(b).value)}');
}
Output
small: 5
a + b: IntegerOverflow: 60000 + 7000
U16 checkedSub(U16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U16(60000), b = U16(7000);
  print('small: ${attempt(() => U16(3).checkedSub(U16(2)).value)}');
  print('b - a: ${attempt(() => b.checkedSub(a).value)}');
}
Output
small: 1
b - a: IntegerOverflow: 7000 - 60000
U16 checkedMul(U16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U16(60000), b = U16(7000);
  print('small: ${attempt(() => U16(3).checkedMul(U16(2)).value)}');
  print('a * b: ${attempt(() => a.checkedMul(b).value)}');
}
Output
small: 6
a * b: IntegerOverflow: 60000 * 7000
U16 saturatingAdd(U16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U16(60000), b = U16(7000);
  print('a + b clamps to ${a.saturatingAdd(b).value} (max)');
  print('small: ${U16(3).saturatingAdd(U16(4)).value}');
}
Output
a + b clamps to 65535 (max)
small: 7
U16 saturatingSub(U16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U16(60000), b = U16(7000);
  print('b − a clamps to ${b.saturatingSub(a).value}');
  print('a − b = ${a.saturatingSub(b).value}');
}
Output
b − a clamps to 0
a − b = 53000

Operators

operator +operator
U16 operator +(U16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U16(60000), b = U16(7000);
  print('a + b wraps to ${(a + b).value}');
}
Output
a + b wraps to 1464
operator -operator
U16 operator -(U16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U16(60000), b = U16(7000);
  print('b − a wraps to ${(b - a).value}');
}
Output
b − a wraps to 12536
operator *operator
U16 operator *(U16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U16(60000), b = U16(7000);
  print('a × b wraps to ${(a * b).value}');
}
Output
a × b wraps to 45312
operator &operator
U16 operator &(U16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U16(0xff00), y = U16(0xff0);
  print('x & y = 0x${(x & y).value.toRadixString(16)}');
}
Output
x & y = 0xf00
operator |operator
U16 operator |(U16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U16(0xff00), y = U16(0xff0);
  print('x | y = 0x${(x | y).value.toRadixString(16)}');
}
Output
x | y = 0xfff0
operator ^operator
U16 operator ^(U16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U16(0xff00), y = U16(0xff0);
  print('x ^ y = 0x${(x ^ y).value.toRadixString(16)}');
}
Output
x ^ y = 0xf0f0
operator ~operator
U16 operator ~()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U16(0xff00), y = U16(0xff0);
  print('~x = 0x${(~x).value.toRadixString(16)}');
}
Output
~x = 0xff
operator <<operator
U16 operator <<(int s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U16(0xff00), y = U16(0xff0);
  print('x << 4 = 0x${(x << 4).value.toRadixString(16)} (high bits fall off)');
}
Output
x << 4 = 0xf000 (high bits fall off)
operator >>operator
U16 operator >>(int s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U16(0xff00), y = U16(0xff0);
  print('x >> 4 = 0x${(x >> 4).value.toRadixString(16)}');
}
Output
x >> 4 = 0xff0
operator <operator
bool operator <(U16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U16(60000), b = U16(7000);
  print('a < b: ${a < b}, b < a: ${b < a}');
}
Output
a < b: false, b < a: true
operator >operator
bool operator >(U16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U16(60000), b = U16(7000);
  print('a > b: ${a > b}');
}
Output
a > b: true
extension type

U32

extension type const U32._(int v) implements Object

Constructors

U32factory
factory U32(int v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print('U32(4000000000) = ${U32(4000000000).value}');
  try {
    U32(4294967296);
  } catch (e) {
    print('U32(4294967296) is out of range: ${e.runtimeType}');
  }
  print('max = ${U32.max.value}');
}
Output
U32(4000000000) = 4000000000
U32(4294967296) is out of range: IntegerOverflow
max = 4294967295
U32.wrapfactory
factory U32.wrap(int v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print('wrap(4294967301) = ${U32.wrap(4294967301).value}');
  print('wrap(−1) = ${U32.wrap(-1).value}');
}
Output
wrap(4294967301) = 5
wrap(−1) = 4294967295

Static

maxconstant
static const max = U32._(0xffffffff)
parsestaticmethod
static U32 parse(String s, {int radix = 10})
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print(U32.parse('4000000000').value);
  print(U32.parse('ff', radix: 16).value);
}
Output
4000000000
255

Properties

valuegetter
int get value

Methods

U32 checkedAdd(U32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U32(4000000000), b = U32(500000000);
  print('small: ${attempt(() => U32(3).checkedAdd(U32(2)).value)}');
  print('a + b: ${attempt(() => a.checkedAdd(b).value)}');
}
Output
small: 5
a + b: IntegerOverflow: 4000000000 + 500000000
U32 checkedSub(U32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U32(4000000000), b = U32(500000000);
  print('small: ${attempt(() => U32(3).checkedSub(U32(2)).value)}');
  print('b - a: ${attempt(() => b.checkedSub(a).value)}');
}
Output
small: 1
b - a: IntegerOverflow: 500000000 - 4000000000
U32 checkedMul(U32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U32(4000000000), b = U32(500000000);
  print('small: ${attempt(() => U32(3).checkedMul(U32(2)).value)}');
  print('a * b: ${attempt(() => a.checkedMul(b).value)}');
}
Output
small: 6
a * b: IntegerOverflow: 4000000000 * 500000000
U32 saturatingAdd(U32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U32(4000000000), b = U32(500000000);
  print('a + b clamps to ${a.saturatingAdd(b).value} (max)');
  print('small: ${U32(3).saturatingAdd(U32(4)).value}');
}
Output
a + b clamps to 4294967295 (max)
small: 7
U32 saturatingSub(U32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U32(4000000000), b = U32(500000000);
  print('b − a clamps to ${b.saturatingSub(a).value}');
  print('a − b = ${a.saturatingSub(b).value}');
}
Output
b − a clamps to 0
a − b = 3500000000
U32 rotateLeft(int s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U32(0x80000001);
  print('rotl 1 = 0x${x.rotateLeft(1).value.toRadixString(16)}');
  print('rotl 8 = 0x${x.rotateLeft(8).value.toRadixString(16)}');
}
Output
rotl 1 = 0x3
rotl 8 = 0x180

Operators

operator +operator
U32 operator +(U32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U32(4000000000), b = U32(500000000);
  print('a + b wraps to ${(a + b).value}');
}
Output
a + b wraps to 205032704
operator -operator
U32 operator -(U32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U32(4000000000), b = U32(500000000);
  print('b − a wraps to ${(b - a).value}');
}
Output
b − a wraps to 794967296
operator *operator
U32 operator *(U32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U32(4000000000), b = U32(500000000);
  print('a × b wraps to ${(a * b).value}');
}
Output
a × b wraps to 1321730048
operator &operator
U32 operator &(U32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U32(0xffff0000), y = U32(0xffff00);
  print('x & y = 0x${(x & y).value.toRadixString(16)}');
}
Output
x & y = 0xff0000
operator |operator
U32 operator |(U32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U32(0xffff0000), y = U32(0xffff00);
  print('x | y = 0x${(x | y).value.toRadixString(16)}');
}
Output
x | y = 0xffffff00
operator ^operator
U32 operator ^(U32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U32(0xffff0000), y = U32(0xffff00);
  print('x ^ y = 0x${(x ^ y).value.toRadixString(16)}');
}
Output
x ^ y = 0xff00ff00
operator ~operator
U32 operator ~()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U32(0xffff0000), y = U32(0xffff00);
  print('~x = 0x${(~x).value.toRadixString(16)}');
}
Output
~x = 0xffff
operator <<operator
U32 operator <<(int s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U32(0xffff0000), y = U32(0xffff00);
  print('x << 4 = 0x${(x << 4).value.toRadixString(16)} (high bits fall off)');
}
Output
x << 4 = 0xfff00000 (high bits fall off)
operator >>operator
U32 operator >>(int s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U32(0xffff0000), y = U32(0xffff00);
  print('x >> 4 = 0x${(x >> 4).value.toRadixString(16)}');
}
Output
x >> 4 = 0xffff000
operator <operator
bool operator <(U32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U32(4000000000), b = U32(500000000);
  print('a < b: ${a < b}, b < a: ${b < a}');
}
Output
a < b: false, b < a: true
operator >operator
bool operator >(U32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U32(4000000000), b = U32(500000000);
  print('a > b: ${a > b}');
}
Output
a > b: true
extension type

U64

extension type const U64._(int bits) implements Object

Unsigned 64-bit: the same 64 bits as int, compared and printed as unsigned.

Constructors

U64.fromBitsconstructor
const U64.fromBits(this.bits)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final m = U64.fromBits(-1);
  print('the bits of −1 as unsigned: ${(m).toRadixString(10)}');
}
Output
the bits of −1 as unsigned: 18446744073709551615
factory U64.fromBigInt(BigInt b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U64.fromBigInt(BigInt.two.pow(63) + BigInt.from(5));
  print('2⁶³ + 5 = ${(x).toRadixString(10)}');
}
Output
2⁶³ + 5 = 9223372036854775813

Static

maxconstant
static const max = U64._(-1)
zeroconstant
static const zero = U64._(0)
parsestaticmethod
static U64 parse(String s, {int radix = 10})
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print(U64.parse('18446744073709551615').toRadixString(16));
  print(U64.parse('ffff', radix: 16).toBigInt());
}
Output
ffffffffffffffff
65535

Methods

compareTomethod
int compareTo(U64 o)

Unsigned compare: flipping the sign bit maps unsigned order onto signed order.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('a vs b: ${a.compareTo(b)}, b vs a: ${b.compareTo(a)}');
}
Output
a vs b: 1, b vs a: -1
U64 checkedAdd(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('small: ${attempt(() => U64.fromBits(7).checkedAdd(U64.fromBits(3)).toRadixString(10))}');
  print('a Add b: ${attempt(() => a.checkedAdd(b).toRadixString(10))}');
}
Output
small: 10
a Add b: IntegerOverflow: U64 add
U64 checkedSub(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('small: ${attempt(() => U64.fromBits(7).checkedSub(U64.fromBits(3)).toRadixString(10))}');
  print('b Sub a: ${attempt(() => b.checkedSub(a).toRadixString(10))}');
}
Output
small: 4
b Sub a: IntegerOverflow: U64 sub
U64 checkedMul(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('small: ${attempt(() => U64.fromBits(7).checkedMul(U64.fromBits(3)).toRadixString(10))}');
  print('a Mul b: ${attempt(() => a.checkedMul(b).toRadixString(10))}');
}
Output
small: 21
a Mul b: IntegerOverflow: U64 mul
U64 saturatingAdd(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('a + b saturates to ${(a.saturatingAdd(b)).toRadixString(10)}');
}
Output
a + b saturates to 18446744073709551615
U64 saturatingSub(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('b − a saturates to ${(b.saturatingSub(a)).toRadixString(10)}');
}
Output
b − a saturates to 0
toBigIntmethod
BigInt toBigInt()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('a = ${a.toBigInt()}, as a signed int it is ${a == U64.fromBits(-616) ? -616 : '?'}');
}
Output
a = 18446744073709551000, as a signed int it is -616
String toRadixString(int radix)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('hex ${a.toRadixString(16)}');
  print('binary of b ${b.toRadixString(2)}');
}
Output
hex fffffffffffffd98
binary of b 1111101000

Operators

operator <operator
bool operator <(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('a < b: ${a < b} (unsigned: a is huge)');
  print('as signed ints: ${U64.fromBits(-616) == a ? -616 < 1000 : '?'}');
}
Output
a < b: false (unsigned: a is huge)
as signed ints: true
operator >operator
bool operator >(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('a > b: ${a > b} (unsigned: a is huge)');
  print('as signed ints: ${U64.fromBits(-616) == a ? -616 > 1000 : '?'}');
}
Output
a > b: true (unsigned: a is huge)
as signed ints: false
operator <=operator
bool operator <=(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('a <= b: ${a <= b} (unsigned: a is huge)');
  print('as signed ints: ${U64.fromBits(-616) == a ? -616 <= 1000 : '?'}');
}
Output
a <= b: false (unsigned: a is huge)
as signed ints: true
operator >=operator
bool operator >=(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('a >= b: ${a >= b} (unsigned: a is huge)');
  print('as signed ints: ${U64.fromBits(-616) == a ? -616 >= 1000 : '?'}');
}
Output
a >= b: true (unsigned: a is huge)
as signed ints: false
operator +operator
U64 operator +(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('a + b wraps to ${(a + b).toRadixString(10)}');
}
Output
a + b wraps to 384
operator -operator
U64 operator -(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('b − a wraps to ${(b - a).toRadixString(10)}');
}
Output
b − a wraps to 1616
operator *operator
U64 operator *(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U64.parse('4294967296');
  print('2³² × 2³² wraps to ${(x * x).toRadixString(10)}');
  print('3 × 5 = ${(U64.fromBits(3) * U64.fromBits(5)).toRadixString(10)}');
}
Output
2³² × 2³² wraps to 0
3 × 5 = 15
operator &operator
U64 operator &(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U64.parse('ff00ff00ff00ff00', radix: 16), y = U64.parse('0ff00ff00ff00ff0', radix: 16);
  print((x & y).toRadixString(16));
}
Output
f000f000f000f00
operator |operator
U64 operator |(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U64.parse('ff00ff00ff00ff00', radix: 16), y = U64.parse('0ff00ff00ff00ff0', radix: 16);
  print((x | y).toRadixString(16));
}
Output
fff0fff0fff0fff0
operator ^operator
U64 operator ^(U64 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U64.parse('ff00ff00ff00ff00', radix: 16), y = U64.parse('0ff00ff00ff00ff0', radix: 16);
  print((x ^ y).toRadixString(16));
}
Output
f0f0f0f0f0f0f0f0
operator ~operator
U64 operator ~()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U64.parse('ff00ff00ff00ff00', radix: 16), y = U64.parse('0ff00ff00ff00ff0', radix: 16);
  print((~x).toRadixString(16));
}
Output
ff00ff00ff00ff
operator <<operator
U64 operator <<(int s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U64.parse('ff00ff00ff00ff00', radix: 16), y = U64.parse('0ff00ff00ff00ff0', radix: 16);
  print((x << 8).toRadixString(16));
}
Output
ff00ff00ff0000
operator >>operator
U64 operator >>(int s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U64.parse('ff00ff00ff00ff00', radix: 16), y = U64.parse('0ff00ff00ff00ff0', radix: 16);
  print('logical shift: ${(x >> 60).toRadixString(16)} (no sign extension)');
}
Output
logical shift: f (no sign extension)
operator ~/operator
U64 operator ~/(U64 o)

Unsigned division and remainder (via BigInt for the top-bit-set cases).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U64.parse('18446744073709551000'), b = U64.parse('1000');
  print('a ~/ b = ${(a ~/ b).toRadixString(10)}');
  print('check: ${a.toBigInt() ~/ b.toBigInt()}');
}
Output
a ~/ b = 18446744073709551
check: 18446744073709551
extension type

U128

extension type const U128._((int, int) _r) implements Object

Unsigned 128-bit integer as (hi, lo) 64-bit halves, each read as unsigned.

Constructors

U128constructor
const U128(int hi, int lo)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  const x = U128(1, 0); // 2^64
  print('U128(1, 0) = ${x.toBigInt()}');
}
Output
U128(1, 0) = 18446744073709551616
factory U128.fromU64(U64 v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print(U128.fromU64(U64.fromBits(-1)).toBigInt());
}
Output
18446744073709551615
factory U128.fromBigInt(BigInt b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print(U128.fromBigInt(BigInt.parse('123456789012345678901234567890')).toRadixString(16));
}
Output
18ee90ff6c373e0ee4e3f0ad2

Static

zeroconstant
static const zero = U128(0, 0)
maxconstant
static const max = U128(-1, -1)
parsestaticmethod
static U128 parse(String s, {int radix = 10})
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print(U128.parse('ffffffffffffffffffffffffffffffff', radix: 16).toBigInt());
}
Output
340282366920938463463374607431768211455

Properties

higetter
int get hi
logetter
int get lo

Methods

compareTomethod
int compareTo(U128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
  print(a.compareTo(b));
}
Output
1
equalsmethod
bool equals(U128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print(U128.parse('42').equals(const U128(0, 42)));
}
Output
true
U128 wrappingMul(U128 o)

Product modulo 2^{128}, by 32-bit limbs (schoolbook).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
  print('a × b wraps to ${a.wrappingMul(b).toBigInt()}');
}
Output
a × b wraps to 340282366920938463463374607431767755456
U128 checkedAdd(U128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
  print(attempt(() => a.checkedAdd(b).toBigInt()));
  print(attempt(() => b.checkedAdd(b).toBigInt()));
}
Output
IntegerOverflow: U128 add
2000
U128 checkedMul(U128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
  print(attempt(() => a.checkedMul(b).toBigInt()));
}
Output
IntegerOverflow: U128 mul
U128 saturatingAdd(U128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
  print(a.saturatingAdd(b) == U128.max);
}
Output
true
toBigIntmethod
BigInt toBigInt()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
  print(a.toBigInt());
}
Output
340282366920938463463374607431768211000
String toRadixString(int radix)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
  print(a.toRadixString(16));
}
Output
fffffffffffffffffffffffffffffe38

Operators

operator <operator
bool operator <(U128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
  print('b < a: ${b < a}');
}
Output
b < a: true
operator >operator
bool operator >(U128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
  print('a > b: ${a > b}');
}
Output
a > b: true
operator +operator
U128 operator +(U128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
  print('a + b wraps to ${(a + b).toBigInt()}');
}
Output
a + b wraps to 544
operator -operator
U128 operator -(U128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = U128.parse('340282366920938463463374607431768211000'), b = U128.parse('1000');
  print('b − a wraps to ${(b - a).toBigInt()}');
}
Output
b − a wraps to 1456
operator *operator
U128 operator *(U128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U128.parse('18446744073709551616'); // 2^64
  print('2⁶⁴ × 3 = ${(x * U128.parse('3')).toBigInt()}');
  print('2⁶⁴ × 2⁶⁴ wraps to ${(x * x).toBigInt()}');
}
Output
2⁶⁴ × 3 = 55340232221128654848
2⁶⁴ × 2⁶⁴ wraps to 0
operator &operator
U128 operator &(U128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U128.parse('ffffffffffffffff0000000000000000', radix: 16), y = U128.parse('0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f', radix: 16);
  print((x & y).toRadixString(16));
}
Output
f0f0f0f0f0f0f0f0000000000000000
operator |operator
U128 operator |(U128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U128.parse('ffffffffffffffff0000000000000000', radix: 16), y = U128.parse('0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f', radix: 16);
  print((x | y).toRadixString(16));
}
Output
ffffffffffffffff0f0f0f0f0f0f0f0f
operator ^operator
U128 operator ^(U128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final x = U128.parse('ffffffffffffffff0000000000000000', radix: 16), y = U128.parse('0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f', radix: 16);
  print((x ^ y).toRadixString(16));
}
Output
f0f0f0f0f0f0f0f00f0f0f0f0f0f0f0f
operator ~operator
U128 operator ~()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print((~U128.zero).toRadixString(16));
}
Output
ffffffffffffffffffffffffffffffff
operator <<operator
U128 operator <<(int s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print((const U128(0, 1) << 100).toBigInt() == BigInt.two.pow(100));
}
Output
true
operator >>operator
U128 operator >>(int s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print((U128.max >> 120).toBigInt());
}
Output
255
extension type

I128

extension type const I128._(U128 u) implements Object

Signed 128-bit integer (two's complement over U128's bits).

Constructors

factory I128.fromInt(int v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print(I128.fromInt(-5).toBigInt());
}
Output
-5
factory I128.fromBigInt(BigInt b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print(I128.fromBigInt(-BigInt.two.pow(100)).toBigInt());
}
Output
-1267650600228229401496703205376

Static

maxstaticfield
static final max
minstaticfield
static final min
parsestaticmethod
static I128 parse(String s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  print(I128.parse('-12345678901234567890123').toBigInt());
}
Output
-12345678901234567890123

Properties

bool get isNegative

Methods

compareTomethod
int compareTo(I128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
  print('a vs b: ${a.compareTo(b)}');
}
Output
a vs b: -1
I128 checkedAdd(I128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
  print(attempt(() => (a - b).checkedAdd(I128.fromInt(-1000)).toBigInt()));
  print(attempt(() => a.checkedAdd(b).toBigInt()));
}
Output
170141183460469231731687303715884104456
-170141183460469231731687303715884104000
I128 checkedSub(I128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
  print(attempt(() => a.checkedSub(b).toBigInt()));
}
Output
IntegerOverflow: I128 sub
I128 checkedMul(I128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

String attempt(Object Function() f) {
  try {
    return '${f()}';
  } on IntegerOverflow catch (e) {
    return '$e';
  }
}

void main() {
  final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
  print(attempt(() => a.checkedMul(b).toBigInt()));
  print(attempt(() => b.checkedMul(b).toBigInt()));
}
Output
IntegerOverflow: I128 mul
1000000
I128 saturatingAdd(I128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
  print('a − 10⁶ saturates to min: ${(a.saturatingAdd(I128.fromInt(-1000000)) == I128.min)}');
}
Output
a − 10⁶ saturates to min: true
toBigIntmethod
BigInt toBigInt()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
  print(a.toBigInt());
}
Output
-170141183460469231731687303715884105000

Operators

operator +operator
I128 operator +(I128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
  print((a + b).toBigInt());
}
Output
-170141183460469231731687303715884104000
operator -operator
I128 operator -(I128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
  print('a − b wraps: ${(a - b).toBigInt()}');
}
Output
a − b wraps: 170141183460469231731687303715884105456
operator *operator
I128 operator *(I128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
  print((b * b * b).toBigInt());
}
Output
1000000000
operator -operator
I128 operator -()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
  print((-b).toBigInt());
  print('−min wraps to min: ${-I128.min == I128.min}');
}
Output
-1000
−min wraps to min: true
operator <operator
bool operator <(I128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
  print(a < b);
}
Output
true
operator >operator
bool operator >(I128 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';

void main() {
  final a = I128.parse('-170141183460469231731687303715884105000'), b = I128.fromInt(1000);
  print(a > b);
}
Output
false
class

Ik

abstract final class Ik

Inverse kinematics on joint position chains (root first). Bone lengths are preserved.

Static

fabrikstaticmethod
static void fabrik(List<Vec3> joints, Vec3 target, {int iterations = 16, double tolerance = 1e-4})

FABRIK (Aristidou and Lasenby 2011): alternate backward (end effector → root) and forward (root → end) passes, re-placing each joint along the line to its neighbour at the bone length. Converges in a few iterations.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final joints = [Vec3(0, 0, 0), Vec3(1, 0, 0), Vec3(2, 0, 0), Vec3(3, 0, 0)];
  final start = [for (final j in joints) [j.x, j.y]];
  final target = Vec3(1.5, 2, 0);
  Ik.fabrik(joints, target);
  print('end effector ${v3(joints.last)}, target ${v3(target)}');
  print('bone lengths kept: ${[for (var i = 0; i < 3; i++) f((joints[i + 1] - joints[i]).length)]}');
}
Output from the Raya VM
end effector (1.5000, 1.9999, 0.0000), target (1.5000, 2.0000, 0.0000)
bone lengths kept: [1.0000, 1.0000, 1.0000]
ccdstaticmethod
static void ccd(List<Vec3> joints, Vec3 target, {int iterations = 32, double tolerance = 1e-4})

Cyclic coordinate descent: from the last joint back to the root, rotate each joint so the end effector points at the target. Simple; tends to curl the chain toward the end.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final joints = [Vec3(0, 0, 0), Vec3(1, 0, 0), Vec3(2, 0, 0), Vec3(3, 0, 0)];
  final start = [for (final j in joints) [j.x, j.y]];
  final target = Vec3(1.5, 2, 0);
  Ik.ccd(joints, target);
  print('end effector ${v3(joints.last)}');
}
Output from the Raya VM
end effector (1.4999, 1.9999, 0.0000)
twoBonestaticmethod
static Vec3 twoBone(Vec3 root, double a, double b, Vec3 target, Vec3 pole)

Analytic two-bone IK (arm or leg): with upper length a, lower length b and target distance c, the law of cosines gives the root and middle angles exactly. pole picks the bending plane. Returns the middle joint.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final root = Vec3.zero, target = Vec3(1.2, 0.8, 0), pole = Vec3(0, 2, 0);
  final elbow = Ik.twoBone(root, 1, 1, target, pole);
  print('elbow ${v3(elbow)}; bones ${f((elbow - root).length)}, ${f((target - elbow).length)}');
}
Output
elbow (0.2157, 0.9765, 0.0000); bones 1.0000, 1.0000
class

Ray

final class Ray

A ray o + t \cdot d (d need not be unit; t is in units of |d|).

Constructors

Rayconstructor
const Ray(this.origin, this.direction)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final r = Ray(Vec3(1, 1, 0), Vec3(1, 0.5, 0));
  print('origin ${v3(r.origin)}, direction ${v3(r.direction)}');
}
Output
origin (1.0000, 1.0000, 0.0000), direction (1.0000, 0.5000, 0.0000)

Properties

originfield
final Vec3 origin
final Vec3 direction

Methods

atmethod
Vec3 at(double t)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final r = Ray(Vec3(0, 0, 0), Vec3(1, 2, 0));
  final pts = [for (final t in [0.0, 0.5, 1.0, 1.5]) r.at(t)];
  print([for (final p in pts) v3(p)]);
}
Output
[(0.0000, 0.0000, 0.0000), (0.5000, 1.0000, 0.0000), (1.0000, 2.0000, 0.0000), (1.5000, 3.0000, 0.0000)]
extension type

Mat3d

extension type Mat3d(Float64List m)

Small dense linear algebra in double precision (Float64List, row-major), for decompositions where float32 isn't enough: physics (inertia, shape matching), animation (polar decomposition), geometry (PCA, OBB fitting). A 3 \times 3 double matrix, row-major: m[r \cdot 3 + c].

Constructors

Mat3d.zerofactory
factory Mat3d.zero()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final z = Mat3d.zero();
  print([for (var i = 0; i < 9; i++) z.at(i ~/ 3, i % 3)]);
}
Output
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
factory Mat3d.identity()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final i = Mat3d.identity();
  final a = Mat3d.of([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  print('I·A == A: ${[for (var k = 0; k < 9; k++) (i * a).at(k ~/ 3, k % 3) == a.at(k ~/ 3, k % 3)].every((x) => x)}');
}
Output
I·A == A: true
Mat3d.offactory
factory Mat3d.of(List<double> rowMajor)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Mat3d.of([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  print('row 1, column 2: ${a.at(1, 2)}');
}
Output
row 1, column 2: 6.0

Properties

Mat3d get transposed
double get determinant

Methods

atmethod
double at(int r, int c)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Mat3d.of([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  print('trace ${a.at(0, 0) + a.at(1, 1) + a.at(2, 2)}');
}
Output
trace 15.0
setmethod
void set(int r, int c, double v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Mat3d.identity();
  a.set(0, 2, 5);
  print([for (var r = 0; r < 3; r++) [for (var c = 0; c < 3; c++) a.at(r, c)]]);
}
Output
[[1.0, 0.0, 5.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]

Operators

operator *operator
Mat3d operator *(Mat3d o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Mat3d.of([0, -1, 0, 1, 0, 0, 0, 0, 1]); // a quarter turn about z
  final a4 = a * a * a * a;
  print('R⁴ = I: ${[for (var k = 0; k < 9; k++) a4.at(k ~/ 3, k % 3)]}');
}
Output
R⁴ = I: [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
extension type

Mat4

extension type Mat4._(Float32x4List c)

A 4 \times 4 column-major matrix stored in 4 SIMD columns. A view over caller-owned storage: build it once (or slice a Mat4Buffer) and reuse it, writing results into an out matrix, so hot loops never allocate.

Constructors

factory Mat4.identity()

A new identity matrix (allocates its storage once).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat4.identity();
  print([for (var r = 0; r < 4; r++) [for (var c = 0; c < 4; c++) m.entry(r, c)]]);
}
Output
[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]
Mat4.viewfactory
factory Mat4.view(Float32x4List buffer, [int column = 0])

A matrix over 4 columns of buffer starting at column column.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // Two matrices sharing one buffer of 8 columns.
  final buf = Float32x4List(8);
  final a = Mat4.view(buf), b = Mat4.view(buf, 4);
  a.setIdentity();
  b.setTrs(Vec3(1, 2, 3), Quat.identity, Vec3(1, 1, 1));
  print('b translation column: ${b.col(3)}');
  print('buffer column 7 is the same: ${buf[7]}');
}
Output
b translation column: [1.000000, 2.000000, 3.000000, 1.000000]
buffer column 7 is the same: [1.000000, 2.000000, 3.000000, 1.000000]

Methods

colmethod
Float32x4 col(int i)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat4.identity()..setTrs(Vec3(5, 6, 7), Quat.identity, Vec3(2, 2, 2));
  for (var i = 0; i < 4; i++) {
    print('column $i: ${m.col(i)}');
  }
}
Output
column 0: [2.000000, 0.000000, 0.000000, 0.000000]
column 1: [0.000000, 2.000000, 0.000000, 0.000000]
column 2: [0.000000, 0.000000, 2.000000, 0.000000]
column 3: [5.000000, 6.000000, 7.000000, 1.000000]
void setIdentity()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat4.identity()..setTrs(Vec3(1, 1, 1), Quat.identity, Vec3(3, 3, 3));
  m.setIdentity();
  print('reset: ${m.entry(0, 0)}, ${m.entry(0, 3)}');
}
Output
reset: 1.0, 0.0
setFrommethod
void setFrom(Mat4 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Mat4.identity()..setTrs(Vec3(1, 2, 3), Quat.identity, Vec3(1, 1, 1));
  final b = Mat4.identity()..setFrom(a);
  print('copied translation: ${b.entry(0, 3)}, ${b.entry(1, 3)}, ${b.entry(2, 3)}');
}
Output
copied translation: 1.0, 2.0, 3.0
setTrsmethod
void setTrs(Vec3 t, Quat r, Vec3 s)

Translation · rotation · scale (TRS), the usual model matrix.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat4.identity()..setTrs(Vec3(2, 1, 0), Quat.axisAngle(Vec3.unitZ, math.pi / 6), Vec3(1.5, 1, 1));
  final sq = [Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(1, 1, 0), Vec3(-1, 1, 0)];
  final moved = [for (final p in sq) m.transformPoint(p).xyz];
  print('T·R·S applied to (1, 1, 0): ${v3(moved[2])}');
}
Output
T·R·S applied to (1, 1, 0): (2.7990, 2.6160, 0.0000)
void setPerspective(double fovY, double aspect, double near, double far)

A right-handed perspective projection to clip space with z in [0, 1] (Metal/Vulkan/WebGPU convention).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final p = Mat4.identity()..setPerspective(math.pi / 3, 16 / 9, 0.1, 100);
  for (final z in [-0.1, -1.0, -100.0]) {
    print('depth at z = $z -> ${f(p.transformPoint(Vec3(0, 0, z)).xyzOverW.z)}');
  }
}
Output
depth at z = -0.1 -> 0.0000
depth at z = -1.0 -> 0.9009
depth at z = -100.0 -> 1.0000
setLookAtmethod
void setLookAt(Vec3 eye, Vec3 target, Vec3 up)

A right-handed view matrix looking from eye at target.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final v = Mat4.identity()..setLookAt(Vec3(0, 0, 5), Vec3.zero, Vec3.unitY);
  print('the target in view space: ${v3(v.transformPoint(Vec3.zero).xyz)}');
  print('the eye: ${v3(v.transformPoint(Vec3(0, 0, 5)).xyz)}');
}
Output
the target in view space: (0.0000, 0.0000, -5.0000)
the eye: (0.0000, 0.0000, 0.0000)
void multiplyInto(Mat4 b, Mat4 out)

Out = this · b. out may be this or b.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final t = Mat4.identity()..setTrs(Vec3(3, 0, 0), Quat.identity, Vec3(1, 1, 1));
  final r = Mat4.identity()..setTrs(Vec3.zero, Quat.axisAngle(Vec3.unitZ, math.pi / 2), Vec3(1, 1, 1));
  final tr = Mat4.identity(), rt = Mat4.identity();
  t.multiplyInto(r, tr);
  r.multiplyInto(t, rt);
  print('T·R moves (1, 0, 0) to ${v3(tr.transformPoint(Vec3.unitX).xyz)}');
  print('R·T moves it to ${v3(rt.transformPoint(Vec3.unitX).xyz)}');
}
Output
T·R moves (1, 0, 0) to (3.0000, 1.0000, 0.0000)
R·T moves it to (0.0000, 4.0000, 0.0000)
Vec4 transformPoint(Vec3 p)

This · (p, 1): a point (translation applies).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat4.identity()..setTrs(Vec3(2, 1, 0), Quat.axisAngle(Vec3.unitZ, math.pi / 6), Vec3(1.5, 1, 1));
  final sq = [Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(1, 1, 0), Vec3(-1, 1, 0)];
  final moved = [for (final p in sq) m.transformPoint(p).xyz];
  final h = m.transformPoint(Vec3(1, 0, 0));
  print('homogeneous result (${f(h.x)}, ${f(h.y)}, ${f(h.z)}, ${f(h.w)})');
}
Output
homogeneous result (3.2990, 1.7500, 0.0000, 1.0000)
Vec3 transformDirection(Vec3 d)

This · (d, 0): a direction (no translation).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat4.identity()..setTrs(Vec3(2, 1, 0), Quat.axisAngle(Vec3.unitZ, math.pi / 6), Vec3(1.5, 1, 1));
  final sq = [Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(1, 1, 0), Vec3(-1, 1, 0)];
  final moved = [for (final p in sq) m.transformPoint(p).xyz];
  print('direction (1, 0, 0) -> ${v3(m.transformDirection(Vec3.unitX))} (no translation)');
}
Output
direction (1, 0, 0) -> (1.2990, 0.7500, 0.0000) (no translation)
void transposeInto(Mat4 out)

The transpose of this into out (may be this).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat4.identity()..setTrs(Vec3(1, 2, 3), Quat.identity, Vec3(1, 1, 1));
  final t = Mat4.identity();
  m.transposeInto(t);
  print('translation now in the bottom row: ${t.entry(3, 0)}, ${t.entry(3, 1)}, ${t.entry(3, 2)}');
}
Output
translation now in the bottom row: 1.0, 2.0, 3.0
bool invertInto(Mat4 out)

The general inverse via cofactors into out; returns false (and leaves out unchanged) when singular.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat4.identity()..setTrs(Vec3(2, 1, 0), Quat.axisAngle(Vec3.unitZ, math.pi / 6), Vec3(1.5, 1, 1));
  final sq = [Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(1, 1, 0), Vec3(-1, 1, 0)];
  final moved = [for (final p in sq) m.transformPoint(p).xyz];
  final inv = Mat4.identity();
  print('invertible: ${m.invertInto(inv)}');
  print('inverse undoes it: ${v3(inv.transformPoint(m.transformPoint(Vec3(0.3, -0.7, 0)).xyz).xyz)}');
}
Output
invertible: true
inverse undoes it: (0.3000, -0.7000, 0.0000)
entrymethod
double entry(int row, int column)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat4.identity()..setTrs(Vec3(2, 1, 0), Quat.axisAngle(Vec3.unitZ, math.pi / 6), Vec3(1.5, 1, 1));
  final sq = [Vec3(-1, -1, 0), Vec3(1, -1, 0), Vec3(1, 1, 0), Vec3(-1, 1, 0)];
  final moved = [for (final p in sq) m.transformPoint(p).xyz];
  print('row 0: ${[for (var c = 0; c < 4; c++) f(m.entry(0, c))]}');
}
Output
row 0: [1.2990, -0.5000, 0.0000, 2.0000]
extension type

Mat3

extension type Mat3._(Float32x4List c)

A 3 \times 3 matrix (normal matrices, rotations) as 3 SIMD columns (w lanes unused).

Constructors

factory Mat3.identity()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat3.identity();
  print('transform keeps points: ${v3(m.transform(Vec3(1, 2, 3)))}');
}
Output
transform keeps points: (1.0000, 2.0000, 3.0000)

Methods

void setRotation(Quat q)

The rotation of q.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat3.identity()..setRotation(Quat.axisAngle(Vec3.unitZ, math.pi / 3));
  final pts = [for (var k = 0; k < 6; k++) m.transform(Vec3(math.cos(0.0) * (1 + k * 0.2), 0, 0))];
  print('x rotated 60°: ${v3(m.transform(Vec3.unitX))}');
}
Output
x rotated 60°: (0.5000, 0.8660, 0.0000)
transformmethod
Vec3 transform(Vec3 p)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat3.identity()..setRotation(Quat.axisAngle(Vec3.unitZ, math.pi / 4));
  final sq = [Vec3(0, 0, 0), Vec3(1, 0, 0), Vec3(1, 1, 0), Vec3(0, 1, 0)];
  final r = [for (final p in sq) m.transform(p)];
  print('(1, 0, 0) -> ${v3(r[1])}');
}
Output
(1, 0, 0) -> (0.7071, 0.7071, 0.0000)
final class Mat4Buffer

Contiguous storage for many 4 \times 4 matrices (e.g. instance transforms uploaded to the GPU): Mat4.view slices it.

Constructors

Mat4Bufferconstructor
Mat4Buffer(this.count)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final b = Mat4Buffer(3);
  for (var i = 0; i < 3; i++) {
    b[i].setTrs(Vec3(i.toDouble(), 0, 0), Quat.identity, Vec3(1, 1, 1));
  }
  print('${b.count} matrices in ${b.floats.length} floats (ready to upload)');
}
Output
3 matrices in 48 floats (ready to upload)

Properties

countfield
final int count
columnsfield
final Float32x4List columns
floatsgetter
Float32List get floats

The raw float storage, ready to copy into a GPU buffer.

Operators

operator []operator
Mat4 operator [](int i)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final b = Mat4Buffer(2);
  b[1].setTrs(Vec3(0, 5, 0), Quat.identity, Vec3(2, 2, 2));
  print('second matrix moves the origin to ${v3(b[1].transformPoint(Vec3.zero).xyz)}');
}
Output
second matrix moves the origin to (0.0000, 5.0000, 0.0000)
final class HalfEdgeMesh

Half-edge mesh built from an indexed triangle mesh: each directed edge knows its twin (opposite half-edge) and next edge, so adjacency queries (one-ring, boundaries, manifoldness) are O(1) per step.

Constructors

HalfEdgeMesh.fromTriangles(int vertexCount, List<(int, int, int)> tris)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = HalfEdgeMesh.fromTriangles(5, [(0, 1, 4), (1, 2, 4), (2, 3, 4), (3, 0, 4)]);
  print('one-ring of the centre: ${m.oneRing(4)}');
}
Output
one-ring of the centre: [0, 1, 2, 3]

Properties

final Int32List vertexOf
nextfield
final Int32List next
twinfield
final Int32List twin
final Int32List outgoing
int get halfEdgeCount
int get boundaryEdgeCount

Edges with no twin lie on a boundary.

Methods

oneRingmethod
List<int> oneRing(int v)

The vertices around v (its one-ring), walking twin→next; stops at a boundary.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = HalfEdgeMesh.fromTriangles(4, [(0, 1, 2), (0, 2, 3)]);
  for (var v = 0; v < 4; v++) {
    print('vertex $v: ${m.oneRing(v)}');
  }
}
Output
vertex 0: [1, 2]
vertex 1: [2]
vertex 2: [0]
vertex 3: [0]
class

Noise

final class Noise

Gradient noise. Perlin's improved noise (Perlin 2002) and simplex noise (Perlin 2001; Gustavson's formulation), deterministic for a given seed, values roughly in [-1, 1].

Constructors

Noiseconstructor
Noise([int seed = 0])
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Noise(1), b = Noise(1), c = Noise(2);
  print('same seed, same value: ${a.perlin(0.5, 0.3, 0.1) == b.perlin(0.5, 0.3, 0.1)}');
  print('another seed: ${f(c.perlin(0.5, 0.3, 0.1))}');
}
Output
same seed, same value: true
another seed: 0.0815

Methods

perlinmethod
double perlin(double x, double y, double z)

Improved Perlin noise in 3D.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final n = Noise(7);
  print('at an integer lattice point: ${n.perlin(2, 3, 4)}');
}
Output
at an integer lattice point: 0.0
simplex2method
double simplex2(double x, double y)

2D simplex noise: sums of radially symmetric kernels on a skewed triangular grid; cheaper and with fewer directional artefacts than 2D Perlin.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final n = Noise(3);
  print('simplex2(1.3, 2.7) = ${f(n.simplex2(1.3, 2.7))}');
}
Output
simplex2(1.3, 2.7) = 0.8138
fbmmethod
double fbm(Vec3 p, {int octaves = 5, double lacunarity = 2, double gain = 0.5})

Fractal Brownian motion: octaves of Perlin noise, each at double frequency and gain amplitude.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final n = Noise(5);
  print('fbm at the origin region: ${f(n.fbm(Vec3(0.1, 0.2, 0.3)))}');
}
Output
fbm at the origin region: -0.0429
class

Sdf

abstract final class Sdf

Signed distance functions (negative inside). Composable primitives for ray marching, collision and procedural modelling (Quilez's catalogue).

Static

spherestaticmethod
static double sphere(Vec3 p, double r)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('inside ${Sdf.sphere(Vec3.zero, 1)}, on ${Sdf.sphere(Vec3(1, 0, 0), 1)}, outside ${Sdf.sphere(Vec3(3, 0, 0), 1)}');
}
Output
inside -1.0, on 0.0, outside 2.0
boxstaticmethod
static double box(Vec3 p, Vec3 halfExtents)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('d(2, 0, 0) = ${Sdf.box(Vec3(2, 0, 0), Vec3(1, 1, 1))}');
  print('d(2, 2, 0) = ${f(Sdf.box(Vec3(2, 2, 0), Vec3(1, 1, 1)))} (corner distance √2)');
}
Output
d(2, 0, 0) = 1.0
d(2, 2, 0) = 1.4142 (corner distance √2)
roundBoxstaticmethod
static double roundBox(Vec3 p, Vec3 halfExtents, double r)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print(f(Sdf.roundBox(Vec3(2, 2, 0), Vec3(1, 1, 1), 0.3)));
}
Output
1.5385
torusstaticmethod
static double torus(Vec3 p, double major, double minor)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('on the ring: ${f(Sdf.torus(Vec3(2, 0, 0), 2, 0.5))}');
}
Output
on the ring: -0.5000
capsulestaticmethod
static double capsule(Vec3 p, Vec3 a, Vec3 b, double r)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print(f(Sdf.capsule(Vec3(0, 1, 0), Vec3(-1, 0, 0), Vec3(1, 0, 0), 0.5)));
}
Output
0.5000
planestaticmethod
static double plane(Vec3 p, Vec3 n, double d)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('above ${Sdf.plane(Vec3(0, 2, 0), Vec3.unitY, 0)}, below ${Sdf.plane(Vec3(0, -1, 0), Vec3.unitY, 0)}');
}
Output
above 2.0, below -1.0
smoothUnionstaticmethod
static double smoothUnion(double a, double b, double k)

Polynomial smooth minimum (blends two shapes over width k).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  double scene(Vec3 p, double k) => Sdf.smoothUnion(Sdf.sphere(p - Vec3(-0.8, 0, 0), 1), Sdf.sphere(p - Vec3(0.8, 0, 0), 1), k);
  print('between the spheres: hard ${f(scene(Vec3(0, 0.9, 0), 0.001))}, smooth ${f(scene(Vec3(0, 0.9, 0), 0.5))}');
}
Output
between the spheres: hard 0.2039, smooth 0.0792
marchstaticmethod
static double? march(double Function(Vec3) sdf, Vec3 origin, Vec3 dir, {double maxDist = 100, int maxSteps = 256, double epsilon = 1e-4})

Sphere tracing (Hart 1996): step along the ray by the distance bound until within epsilon of the surface.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  double scene(Vec3 p) => math.min(Sdf.sphere(p - Vec3(0, 1, 0), 1), Sdf.plane(p, Vec3.unitY, 0));
  final rows = <List<double>>[];
  for (var i = 0; i < 9; i++) {
    rows.add([for (var j = 0; j < 13; j++) (Sdf.march(scene, Vec3(0, 1, 5), Vec3(-0.6 + j * 0.1, 0.4 - i * 0.1, -1).normalized()) ?? 0).toDouble()]);
  }
  print('distance along the centre ray: ${f(Sdf.march(scene, Vec3(0, 1, 5), Vec3(0, 0, -1))!)}');
}
Output
distance along the centre ray: 4.0000
normalstaticmethod
static Vec3 normal(double Function(Vec3) sdf, Vec3 p, {double h = 1e-4})

Surface normal by central differences of the distance field.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final n = Sdf.normal((p) => Sdf.sphere(p, 1), Vec3(1, 1, 0).normalized());
  print('sphere normal at 45°: ${v3(n)}');
}
Output
sphere normal at 45°: (0.7071, 0.7071, 0.0000)
extension type

Fx16

extension type const Fx16(int raw)

Q16.16 fixed point in a Raya int (64-bit on native): bit-identical results on every device, for deterministic lockstep or rollback netcode where floats can differ between CPUs and compilers.

Constructors

factory Fx16.fromDouble(double v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final x = Fx16.fromDouble(3.14159);
  print('raw ${x.raw}, back ${x.toDouble()}');
}
Output
raw 205887, back 3.1415863037109375
factory Fx16.fromInt(int v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(Fx16.fromInt(5).raw);
  print(Fx16.fromInt(5).toDouble());
}
Output
327680
5.0

Static

oneconstant
static const one = Fx16(1 << 16)

Methods

toDoublemethod
double toDouble()
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(Fx16(98304).toDouble());
}
Output
1.5

Operators

operator +operator
Fx16 operator +(Fx16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
  print('a + b = ${(a + b).toDouble()}');
}
Output
a + b = 3.25
operator -operator
Fx16 operator -(Fx16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
  print('a - b = ${(a - b).toDouble()}');
}
Output
a - b = 1.75
operator -operator
Fx16 operator -()
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
  print((-a).toDouble());
}
Output
-2.5
operator *operator
Fx16 operator *(Fx16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
  print('a * b = ${(a * b).toDouble()}');
}
Output
a * b = 1.875
operator /operator
Fx16 operator /(Fx16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
  print('a / b = ${(a / b).toDouble()}');
}
Output
a / b = 3.3333282470703125
operator <operator
bool operator <(Fx16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
  print(b < a);
}
Output
true
operator >operator
bool operator >(Fx16 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Fx16.fromDouble(2.5), b = Fx16.fromDouble(0.75);
  print(a > b);
}
Output
true
extension type

Fx32

extension type const Fx32(int raw)

Q32.32 fixed point. The 64 \times 64 product needs 128 bits, so multiplication splits each operand into 32-bit halves and recombines (exact for results in range).

Constructors

factory Fx32.fromDouble(double v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final x = Fx32.fromDouble(0.1);
  print('raw ${x.raw}, back ${x.toDouble()}');
}
Output
raw 429496730, back 0.10000000009313226
factory Fx32.fromInt(int v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(Fx32.fromInt(-3).toDouble());
}
Output
-3.0

Methods

toDoublemethod
double toDouble()
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(Fx32(1 << 31).toDouble());
}
Output
0.5

Operators

operator +operator
Fx32 operator +(Fx32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Fx32.fromDouble(1.5), b = Fx32.fromDouble(-0.25);
  print('a + b = ${(a + b).toDouble()}');
}
Output
a + b = 1.25
operator -operator
Fx32 operator -(Fx32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Fx32.fromDouble(1.5), b = Fx32.fromDouble(-0.25);
  print('a - b = ${(a - b).toDouble()}');
}
Output
a - b = 1.75
operator *operator
Fx32 operator *(Fx32 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Fx32.fromDouble(1.5), b = Fx32.fromDouble(-0.25);
  print('a * b = ${(a * b).toDouble()}');
}
Output
a * b = -0.375
abstract final class RobustPredicates

Exact geometric predicates. A fast floating-point evaluation is used when its error bound (Shewchuk 1997) proves the sign; otherwise the determinant is recomputed exactly with rationals built from the doubles' exact binary values (every finite double is m \cdot 2^{e}). The sign is always correct — the property Shewchuk's adaptive predicates guarantee — at the cost of a slower fallback in rare near-degenerate cases.

Static

orient2dstaticmethod
static int orient2d(double ax, double ay, double bx, double by, double cx, double cy)

Sign of the orientation of (a, b, c): > 0 counter-clockwise, < 0 clockwise, 0 collinear.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('left turn: ${RobustPredicates.orient2d(0, 0, 1, 0, 0, 1)}');
  print('right turn: ${RobustPredicates.orient2d(0, 0, 0, 1, 1, 0)}');
  // Nearly collinear points where a plain determinant in doubles gets the sign wrong.
  print('exact on tiny offsets: ${RobustPredicates.orient2d(0.5, 0.5, 12, 12, 24, 24 + 1e-14)}');
}
Output
left turn: 1
right turn: -1
exact on tiny offsets: 1
incirclestaticmethod
static int incircle(double ax, double ay, double bx, double by, double cx, double cy, double dx, double dy)

Sign of whether d is inside (> 0), outside (< 0) or on (0) the circle through counter-clockwise a, b, c.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('inside: ${RobustPredicates.incircle(0, 0, 1, 0, 0, 1, 0.5, 0.5)}');
  print('outside: ${RobustPredicates.incircle(0, 0, 1, 0, 0, 1, 2, 2)}');
  print('cocircular: ${RobustPredicates.incircle(1, 0, 0, 1, -1, 0, 0, -1)}');
}
Output
inside: 1
outside: -1
cocircular: 0
orient3dstaticmethod
static int orient3d(List<double> a, List<double> b, List<double> c, List<double> d)

Sign of the orientation of d relative to the plane through a, b, c (> 0 below, matching Shewchuk's orient3d).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print(RobustPredicates.orient3d([0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]));
  print(RobustPredicates.orient3d([0, 0, 0], [1, 0, 0], [0, 1, 0], [0.3, 0.3, 0]));
}
Output
-1
0
class

Rat

final class Rat

An exact rational number on BigInt (denominators stay powers of two here).

Constructors

Ratconstructor
Rat(this.n, this.d)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Rat(BigInt.from(1), BigInt.from(3)), b = Rat(BigInt.from(1), BigInt.from(6));
  print('n ${a.n}, d ${a.d}');
}
Output
n 1, d 3

Static

zerostaticfield
static final zero

Properties

nfield
final BigInt n
dfield
final BigInt d
signgetter
int get sign

Operators

operator +operator
Rat operator +(Rat o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Rat(BigInt.from(1), BigInt.from(3)), b = Rat(BigInt.from(1), BigInt.from(6));
  final r = a + b;
  print('${r.n}/${r.d} (not reduced)');
}
Output
9/18 (not reduced)
operator -operator
Rat operator -(Rat o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Rat(BigInt.from(1), BigInt.from(3)), b = Rat(BigInt.from(1), BigInt.from(6));
  final r = a - b;
  print('${r.n}/${r.d} (not reduced)');
}
Output
3/18 (not reduced)
operator *operator
Rat operator *(Rat o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Rat(BigInt.from(1), BigInt.from(3)), b = Rat(BigInt.from(1), BigInt.from(6));
  final r = a * b;
  print('${r.n}/${r.d} (not reduced)');
}
Output
1/18 (not reduced)
abstract final class Integrate

Time integrators for x'' = a(x, v, t), written over flat SIMD arrays so a whole particle system steps without allocation. Semi-implicit (symplectic) Euler and velocity Verlet conserve energy far better than explicit Euler; RK4 is 4th-order accurate for smooth forces.

Static

semiImplicitEulerstaticmethod
static void semiImplicitEuler(Float32x4List x, Float32x4List v, Float32x4List a, double dt)

v + = a \cdot dt; x + = v \cdot dt (semi-implicit Euler).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // A ball thrown at (3, 4) under gravity, one body, Δt = 0.1.
  final x = Float32x4List.fromList([Float32x4(0, 0, 0, 0)]), v = Float32x4List.fromList([Float32x4(3, 4, 0, 0)]);
  final a = Float32x4List.fromList([Float32x4(0, -9.8, 0, 0)]);
  final path = <List<double>>[];
  for (var i = 0; i < 9; i++) {
    path.add([x[0].x, x[0].y]);
    Integrate.semiImplicitEuler(x, v, a, 0.1);
  }
  print('after 0.9 s: (${f(x[0].x)}, ${f(x[0].y)})');
}
Output from the Raya VM
after 0.9 s: (2.7000, -0.8100)
verletstaticmethod
static void verlet(Float32x4List x, Float32x4List prev, Float32x4List a, double dt)

Position Verlet: x_{n + 1} = 2x_{n} - x_{n - 1} + a dt^{2} (velocity is implicit; prev holds x_{n - 1}).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final x = Float32x4List.fromList([Float32x4(1, 0, 0, 0)]), prev = Float32x4List.fromList([Float32x4(1, 0, 0, 0)]);
  final a = Float32x4List(1);
  final ys = <List<double>>[];
  for (var i = 0; i < 200; i++) {
    a[0] = Float32x4(-x[0].x, 0, 0, 0); // a spring: a = −x
    Integrate.verlet(x, prev, a, 0.05);
    ys.add([i * 0.05, x[0].x]);
  }
  print('x(10) = ${f(x[0].x)}, cos 10 = ${f(math.cos(10))}');
}
Output
x(10) = -0.8249, cos 10 = -0.8391
rk4staticmethod
static Float64List rk4(Float64List Function(double t, Float64List y) f, double t, Float64List y, double h)

Classic RK4 for one state y' = f(t, y) (state as a Float64List).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  Float64List f2(double t, Float64List y) => Float64List.fromList([y[1], -y[0]]);
  var y = Float64List.fromList([1, 0]);
  for (var i = 0; i < 10; i++) {
    y = Integrate.rk4(f2, i * 0.1, y, 0.1);
  }
  print('y(1) = ${y[0].toStringAsFixed(10)}, cos 1 = ${math.cos(1).toStringAsFixed(10)}');
}
Output
y(1) = 0.5403029671, cos 1 = 0.5403023059
class

Xpbd

abstract final class Xpbd

Extended position-based dynamics (Macklin, Müller and Chentanez 2016): constraints are solved on positions with a compliance \alpha (inverse stiffness) so stiffness is independent of the time step and iteration count.

Static

distancestaticmethod
static double distance(Float32x4List x, Float32List w, int i, int j, double restLength, double compliance, double dt, double lambda)

One XPBD distance-constraint projection between particles i and j (inverse masses w); lambda is the accumulated Lagrange multiplier for this constraint (reset to 0 each step). Returns the updated lambda.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // Two particles 2 apart, constrained to rest length 1 (rigid: compliance 0).
  final x = Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(2, 0, 0, 0)]);
  final w = Float32List.fromList([1, 1]);
  final dl = Xpbd.distance(x, w, 0, 1, 1, 0, 1 / 60, 0);
  print('after one solve: x0 = ${x[0].x}, x1 = ${x[1].x}, Δλ = ${f(dl)}');
}
Output
after one solve: x0 = 0.5, x1 = 1.5, Δλ = -0.5000
class

Inertia

abstract final class Inertia

Inertia tensors about the centre of mass (body frame).

Static

boxstaticmethod
static Mat3d box(double mass, Vec3 size)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final i = Inertia.box(12, Vec3(1, 2, 3));
  print('diagonal ${[f(i.at(0, 0)), f(i.at(1, 1)), f(i.at(2, 2))]}');
}
Output
diagonal [13.0000, 10.0000, 5.0000]
solidSpherestaticmethod
static Mat3d solidSphere(double mass, double r)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final i = Inertia.solidSphere(5, 2);
  print('I = ${f(i.at(0, 0))} on each axis (2/5 m r² = 8)');
}
Output
I = 8.0000 on each axis (2/5 m r² = 8)
cylinderstaticmethod
static Mat3d cylinder(double mass, double r, double h)

Solid cylinder along Y.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final i = Inertia.cylinder(6, 1, 2);
  print('about the axis ${f(i.at(1, 1))}, across ${f(i.at(0, 0))}');
}
Output
about the axis 3.0000, across 3.5000
meshstaticmethod
static ({double mass, Vec3 center, Mat3d inertia}) mesh(Float32x4List p, Uint32List idx, double density)

Mass properties of a closed triangle mesh of uniform density via the divergence theorem (Eberly's "Polyhedral Mass Properties", 2003): volume, centre of mass, inertia about the centre of mass.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // A unit cube as 12 triangles, density 2: mass 2, inertia 2/6 = 1/3 about the centre.
  final p = Float32x4List.fromList([for (var i = 0; i < 8; i++) Float32x4((i & 1).toDouble(), ((i >> 1) & 1).toDouble(), ((i >> 2) & 1).toDouble(), 0)]);
  final idx = Uint32List.fromList([0, 2, 1, 1, 2, 3, 4, 5, 6, 5, 7, 6, 0, 1, 4, 1, 5, 4, 2, 6, 3, 3, 6, 7, 0, 4, 2, 2, 4, 6, 1, 3, 5, 3, 7, 5]);
  final r = Inertia.mesh(p, idx, 2);
  print('mass ${f(r.mass)}, centre ${v3(r.center)}, I_xx ${f(r.inertia.at(0, 0))}');
}
Output
mass 2.0000, centre (0.5000, 0.5000, 0.5000), I_xx 0.3333
abstract final class Broadphase

Broadphase: find candidate overlapping pairs of AABBs cheaply before exact narrowphase tests.

Static

sweepAndPrunestaticmethod
static List<(int, int)> sweepAndPrune(Float32x4List mins, Float32x4List maxs)

Sweep and prune on X: sort by min.x, sweep keeping an active list, test Y and Z overlap. O(n \log n + k).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final mins = Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(1.5, 0.5, 0, 0), Float32x4(4, 0, 0, 0), Float32x4(4.5, 1, 0, 0)]);
  final maxs = Float32x4List.fromList([Float32x4(2, 2, 1, 0), Float32x4(3, 2.5, 1, 0), Float32x4(5, 1.5, 1, 0), Float32x4(6, 3, 1, 0)]);
  print('pairs: ${Broadphase.sweepAndPrune(mins, maxs)}');
}
Output
pairs: [(0, 1), (2, 3)]
uniformGridstaticmethod
static List<(int, int)> uniformGrid(Float32x4List mins, Float32x4List maxs, double cellSize)

Uniform grid: hash each box into cells of cellSize; boxes sharing a cell are candidates (deduplicated).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final mins = Float32x4List.fromList([Float32x4(0, 0, 0, 0), Float32x4(1.5, 0.5, 0, 0), Float32x4(4, 0, 0, 0), Float32x4(4.5, 1, 0, 0)]);
  final maxs = Float32x4List.fromList([Float32x4(2, 2, 1, 0), Float32x4(3, 2.5, 1, 0), Float32x4(5, 1.5, 1, 0), Float32x4(6, 3, 1, 0)]);
  print('pairs: ${Broadphase.uniformGrid(mins, maxs, 2)}');
}
Output
pairs: [(0, 1), (2, 3)]
extension type

Quat

extension type const Quat._(Float32x4 v)

A unit quaternion for rotations (lanes x, y, z = vector part, w = scalar part), unboxed like Vec3.

Constructors

Quatconstructor
Quat(double x, double y, double z, double w)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q = Quat(0, 0, math.sin(math.pi / 8), math.cos(math.pi / 8)); // 45° about z
  print('(${f(q.x)}, ${f(q.y)}, ${f(q.z)}, ${f(q.w)}), rotate x -> ${v3(q.rotate(Vec3.unitX))}');
}
Output
(0.0000, 0.0000, 0.3827, 0.9239), rotate x -> (0.7071, 0.7071, 0.0000)
factory Quat.axisAngle(Vec3 axis, double radians)

Rotation of radians about the unit axis: (\mathrm{axis} \cdot \sin(\frac{\theta}{2}), \cos(\frac{\theta}{2})), the half angle's turn made at RatioBoundary (the radians entry point; the rational one is RatioQuat.fromCayley).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final pts = [for (var k = 0; k < 8; k++) Quat.axisAngle(Vec3.unitZ, k * math.pi / 4).rotate(Vec3(1, 0.3, 0))];
  print('90° about z: ${v3(Quat.axisAngle(Vec3.unitZ, math.pi / 2).rotate(Vec3.unitX))}');
}
Output
90° about z: (0.0000, 1.0000, 0.0000)

Static

identitystaticfield
static final identity

Properties

xgetter
double get x
ygetter
double get y
zgetter
double get z
wgetter
double get w
conjugategetter
Quat get conjugate

Methods

dotmethod
double dot(Quat o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Quat.axisAngle(Vec3.unitZ, 0), b = Quat.axisAngle(Vec3.unitZ, math.pi / 2);
  print('dot = ${f(a.dot(b))} = cos(θ/2) for θ = 90°');
}
Output
dot = 0.7071 = cos(θ/2) for θ = 90°
Quat normalized()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q = Quat(1, 2, 3, 4).normalized();
  print('unit: ${f(q.dot(q))}');
}
Output
unit: 1.0000
rotatemethod
Vec3 rotate(Vec3 p)

Rotates p: v' = v + 2w(u \times v) + 2u \times (u \times v), with u the vector part (cheaper than q v q*).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q = Quat.axisAngle(Vec3(1, 1, 1).normalized(), 2 * math.pi / 3);
  print('x -> ${v3(q.rotate(Vec3.unitX))} (a cyclic permutation)');
}
Output
x -> (0.0000, 1.0000, 0.0000) (a cyclic permutation)
slerpmethod
Quat slerp(Quat to, double t)

Spherical linear interpolation along the shorter arc; falls back to normalised lerp when nearly parallel. The great circle through the two is walked by Turn.fraction of the turn (d, \sqrt{1 - d^{2}}) between them (square roots only): q = a \cdot (c - d \cdot \frac{s}{\sqrt{1 - d^{2}}}) + b \cdot \frac{s}{\sqrt{1 - d^{2}}}, the classical \frac{\sin((1 - t)\theta)}{\sin \theta}, \frac{\sin(t\theta)}{\sin \theta}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Quat.identity, b = Quat.axisAngle(Vec3.unitZ, 2 * math.pi / 3);
  final pts = [for (var i = 0; i <= 8; i++) a.slerp(b, i / 8).rotate(Vec3.unitX)];
  print('halfway: ${v3(a.slerp(b, 0.5).rotate(Vec3.unitX))}');
}
Output from the Raya VM
halfway: (0.5000, 0.8660, 0.0000)

Operators

operator *operator
Quat operator *(Quat o)

Hamilton product: this ∘ o (apply o first, then this).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final qz = Quat.axisAngle(Vec3.unitZ, math.pi / 2), qx = Quat.axisAngle(Vec3.unitX, math.pi / 2);
  print('qx·qz on x: ${v3((qx * qz).rotate(Vec3.unitX))}');
  print('qz·qx on x: ${v3((qz * qx).rotate(Vec3.unitX))}');
}
Output
qx·qz on x: (0.0000, 0.0000, 1.0000)
qz·qx on x: (0.0000, 1.0000, 0.0000)
extension QuatInterpolation on Quat

Extra quaternion interpolation: normalised lerp (cheap, constant direction, non-constant speed) and SQUAD (smooth cubic spline through key rotations, Shoemake 1987).

Methods

nlerpmethod
Quat nlerp(Quat to, double t)

Normalised linear interpolation along the shorter arc.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Quat.identity, b = Quat.axisAngle(Vec3.unitZ, 2.5);
  final s = [for (var i = 0; i <= 8; i++) a.slerp(b, i / 8).rotate(Vec3.unitX)];
  final n = [for (var i = 0; i <= 8; i++) a.nlerp(b, i / 8).rotate(Vec3.unitX)];
  print('halfway nlerp ${v3(n[4])}, slerp ${v3(s[4])}');
}
Output from the Raya VM
halfway nlerp (0.3153, 0.9490, 0.0000), slerp (0.3153, 0.9490, 0.0000)
logmethod
Quat log()

Quaternion logarithm of a unit quaternion: (\mathrm{axis} \cdot \frac{\theta}{2}, 0).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q = Quat.axisAngle(Vec3.unitZ, 1.2);
  final l = q.log();
  print('log q = (${f(l.x)}, ${f(l.y)}, ${f(l.z)}, ${f(l.w)}) = θ/2 · axis');
}
Output
log q = (0.0000, 0.0000, 0.6000, 0.0000) = θ/2 · axis
expmethod
Quat exp()

Exponential of a pure quaternion (w ignored).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final l = Quat(0, 0, 0.6, 0);
  final q = l.exp();
  print('exp gives a 1.2 rad turn: ${v3(q.rotate(Vec3.unitX))}');
}
Output
exp gives a 1.2 rad turn: (0.3624, 0.9320, 0.0000)
squadmethod
Quat squad(Quat s1, Quat s2, Quat q2, double t)

SQUAD between q1 (this) and q2 with inner control points s1, s2 from squadControl.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final keys = [for (final a in [0.0, 1.0, 2.5, 3.0]) Quat.axisAngle(Vec3.unitZ, a)];
  final s1 = keys[1].squadControl(keys[0], keys[2]), s2 = keys[2].squadControl(keys[1], keys[3]);
  final pts = [for (var i = 0; i <= 10; i++) keys[1].squad(s1, s2, keys[2], i / 10).rotate(Vec3.unitX)];
  print('squad endpoint matches: ${v3(pts.last)} vs ${v3(keys[2].rotate(Vec3.unitX))}');
}
Output from the Raya VM
squad endpoint matches: (-0.8011, 0.5985, 0.0000) vs (-0.8011, 0.5985, 0.0000)
Quat squadControl(Quat prev, Quat next)

The SQUAD inner control point for this key given its neighbours prev and next: s_{i} = q_{i} \cdot \exp(-(\log(q_{i}^{-1} q_{i + 1}) + \log(q_{i}^{-1} q_{i - 1})) / 4).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q0 = Quat.axisAngle(Vec3.unitZ, 0), q1 = Quat.axisAngle(Vec3.unitZ, 1), q2 = Quat.axisAngle(Vec3.unitZ, 3);
  final s = q1.squadControl(q0, q2);
  print('control (${f(s.x)}, ${f(s.y)}, ${f(s.z)}, ${f(s.w)})');
}
Output
control (0.0000, 0.0000, 0.3663, 0.9305)
final class DualQuat

A unit dual quaternion: rigid transform (rotation + translation) as q_{r} + \varepsilon q_{d} with q_{d} = \tfrac{1}{2} t q_{r}. Blending them (dual-quaternion skinning, Kavan et al. 2007) avoids the volume loss of linear blend skinning.

Constructors

DualQuatconstructor
DualQuat(this.real, this.dual)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final d = DualQuat(Quat.identity, Quat(0.5, 0, 0, 0)); // translation (1, 0, 0)
  print('moves origin to ${v3(d.transformPoint(Vec3.zero))}, translation ${v3(d.translation)}');
}
Output
moves origin to (1.0000, 0.0000, 0.0000), translation (1.0000, 0.0000, 0.0000)
factory DualQuat.fromRotationTranslation(Quat r, Vec3 t)

Rotation r then translation t.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final d = DualQuat.fromRotationTranslation(Quat.axisAngle(Vec3.unitZ, math.pi / 2), Vec3(2, 0, 0));
  print('(1, 0, 0) -> ${v3(d.transformPoint(Vec3.unitX))}');
}
Output
(1, 0, 0) -> (2.0000, 1.0000, 0.0000)

Static

blendstaticmethod
static DualQuat blend(List<DualQuat> dqs, List<double> weights)

Dual-quaternion linear blending (DLB) of transforms with weights, antipodality-corrected, normalised.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = DualQuat.fromRotationTranslation(Quat.identity, Vec3.zero);
  final b = DualQuat.fromRotationTranslation(Quat.axisAngle(Vec3.unitZ, math.pi / 2), Vec3(0, 2, 0));
  final pts = [for (var i = 0; i <= 10; i++) DualQuat.blend([a, b], [1 - i / 10, i / 10]).transformPoint(Vec3.unitX)];
  final lin = [for (var i = 0; i <= 10; i++) Vec3.unitX.lerp(b.transformPoint(Vec3.unitX), i / 10)];
  print('halfway: ${v3(pts[5])}');
}
Output from the Raya VM
halfway: (1.1213, 1.7071, 0.0000)

Properties

realfield
final Quat real
dualfield
final Quat dual
Vec3 get translation

Methods

Vec3 transformPoint(Vec3 p)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final d = DualQuat.fromRotationTranslation(Quat.axisAngle(Vec3.unitZ, math.pi / 4), Vec3(1, 1, 0));
  final sq = [Vec3(0, 0, 0), Vec3(1, 0, 0), Vec3(1, 1, 0), Vec3(0, 1, 0)];
  final m = [for (final p in sq) d.transformPoint(p)];
  print('(1, 0, 0) -> ${v3(m[1])}');
}
Output
(1, 0, 0) -> (1.7071, 1.7071, 0.0000)

Operators

operator *operator
DualQuat operator *(DualQuat o)

This ∘ o: apply o, then this.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = DualQuat.fromRotationTranslation(Quat.identity, Vec3(1, 0, 0));
  final b = DualQuat.fromRotationTranslation(Quat.axisAngle(Vec3.unitZ, math.pi / 2), Vec3.zero);
  print('a·b (turn, then move): ${v3((a * b).transformPoint(Vec3.unitX))}');
  print('b·a (move, then turn): ${v3((b * a).transformPoint(Vec3.unitX))}');
}
Output
a·b (turn, then move): (1.0000, 1.0000, 0.0000)
b·a (move, then turn): (0.0000, 2.0000, 0.0000)
abstract final class LowDiscrepancy

Low-discrepancy sequences: points that fill a domain more evenly than random numbers, so sampling-based estimates (image-based lighting, soft shadows, ambient occlusion, anti-aliasing jitter) converge faster with fewer samples.

Static

radicalInversestaticmethod
static double radicalInverse(int i, int base)

The radical inverse of i in base: mirror its digits about the decimal point (base 2: 1 \to 0.5, 2 \to 0.25, 3 \to 0.75).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print([for (var i = 0; i < 8; i++) f(LowDiscrepancy.radicalInverse(i, 3))]);
}
Output
[0.0000, 0.3333, 0.6667, 0.1111, 0.4444, 0.7778, 0.2222, 0.5556]
vanDerCorputstaticmethod
static double vanDerCorput(int i)

Base-2 radical inverse by bit reversal (van der Corput), exact for 32-bit indices.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print([for (var i = 0; i < 8; i++) LowDiscrepancy.vanDerCorput(i)]);
}
Output
[0.0, 0.5, 0.25, 0.75, 0.125, 0.625, 0.375, 0.875]
hammersleystaticmethod
static Vec2 hammersley(int i, int n)

The ith of n Hammersley points in [0,1)²: (\frac{i}{n}, radical inverse base 2).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('first: ${[for (var i = 0; i < 4; i++) '(${f(LowDiscrepancy.hammersley(i, 128).x)}, ${f(LowDiscrepancy.hammersley(i, 128).y)})']}');
}
Output
first: [(0.0000, 0.0000), (0.0078, 0.5000), (0.0156, 0.2500), (0.0234, 0.7500)]
haltonstaticmethod
static Vec2 halton(int i)

The ith Halton point in [0,1)² (bases 2 and 3); unlike Hammersley, needs no total count.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final p = LowDiscrepancy.halton(5);
  print('halton(5) = (${f(p.x)}, ${f(p.y)})');
}
Output
halton(5) = (0.6250, 0.7778)
r2staticmethod
static Vec2 r2(int i)

The ith point of Roberts' R2 sequence, based on the plastic number (the real root of x^{3} = x + 1).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final p = LowDiscrepancy.r2(1);
  print('r2(1) = (${f(p.x)}, ${f(p.y)})');
}
Output
r2(1) = (0.2549, 0.0698)
uniformSpherestaticmethod
static Vec3 uniformSphere(Vec2 u)

Maps a point in [0,1)² to a uniformly distributed unit direction on the sphere.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final d = [for (var i = 0; i < 300; i++) LowDiscrepancy.uniformSphere(LowDiscrepancy.hammersley(i, 300))];
  print('all unit: ${d.every((v) => (v.length - 1).abs() < 1e-5)}');
}
Output
all unit: true
static Vec3 importanceSampleGgx(Vec2 u, double roughness)

GGX importance sampling of a half vector around +Z for roughness (alpha = roughness²): the distribution used to prefilter environment maps for the split-sum approximation (Karis 2013).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  for (final r in [0.2, 0.8]) {
    final h = [for (var i = 0; i < 200; i++) LowDiscrepancy.importanceSampleGgx(LowDiscrepancy.hammersley(i, 200), r)];
    print('roughness $r: mean cos θ = ${f(h.map((v) => v.z).reduce((a, b) => a + b) / 200)}');
  }
  final h = [for (var i = 0; i < 200; i++) LowDiscrepancy.importanceSampleGgx(LowDiscrepancy.hammersley(i, 200), 0.5)];
}
Output
roughness 0.2: mean cos θ = 0.9967
roughness 0.8: mean cos θ = 0.7813
class

Sobol2

abstract final class Sobol2

2D Sobol sequence (first two dimensions, Joe–Kuo direction numbers; dimension 1 is van der Corput), and blue-noise point sets by Bridson's Poisson-disk sampling (no two points closer than r, so samples look even without the visible structure of a grid).

Static

atstaticmethod
static Vec2 at(int i)

The ith point (i \ge 0) in [0,1)².

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print([for (var i = 0; i < 4; i++) '(${Sobol2.at(i).x}, ${Sobol2.at(i).y})']);
}
Output
[(0.0, 0.0), (0.5, 0.5), (0.25, 0.75), (0.75, 0.25)]
class

Sh9

abstract final class Sh9

Real spherical harmonics up to band 2 (9 coefficients), the standard basis for diffuse lighting (Ramamoorthi and Hanrahan 2001, "An Efficient Representation for Irradiance Environment Maps").

A lighting environment L(\omega) is projected to coefficients L_{lm} = \int L(\omega) Y_{lm}(\omega) d\omega. Irradiance (cosine-convolved light) is then E(n) = \sum \hat{A}_{l} L_{lm} Y_{lm}(n) with \hat{A}_{0} = \pi, \hat{A}_{1} = \frac{2\pi}{3}, \hat{A}_{2} = \frac{\pi}{4}, so diffuse shading needs 9 RGB numbers per probe instead of a whole cube map.

Static

c0constant
static const c0 = 0.282095

Constant factors of the 9 basis functions Y_{00} \cdots Y_{22}.

c1constant
static const c1 = 0.488603
c2constant
static const c2 = 1.092548
c3constant
static const c3 = 0.315392
c4constant
static const c4 = 0.546274
a0constant
static const a0 = math.pi

Band convolution weights \hat{A}_{l} for a clamped cosine lobe.

a1constant
static const a1 = 2 * math.pi / 3
a2constant
static const a2 = math.pi / 4
basisstaticmethod
static void basis(Vec3 d, Float32List out)

The 9 basis values for the unit direction d, written into out (length ≥ 9).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final y = Float32List(9);
  Sh9.basis(Vec3.unitZ, y);
  print('Y at +z: ${y.map(f).toList()}');
}
Output
Y at +z: [0.2821, 0.0000, 0.4886, 0.0000, 0.0000, 0.0000, 0.6308, 0.0000, 0.0000]
class

ShRgb

final class ShRgb

Spherical-harmonics radiance for RGB: 9 coefficients per channel (27 floats), accumulated from samples.

Constructors

ShRgbconstructor
ShRgb()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // A sky: blue from above, orange from the horizon, sampled over 200 Hammersley directions.
  final sh = ShRgb();
  for (var i = 0; i < 200; i++) {
    final d = LowDiscrepancy.uniformSphere(LowDiscrepancy.hammersley(i, 200));
    final c = d.z > 0 ? Vec3(0.2, 0.4, 1).scale(d.z) + Vec3(1, 0.5, 0.1).scale(1 - d.z) : Vec3(0.1, 0.1, 0.1);
    sh.addSample(d, c, 4 * math.pi / 200);
  }
  print('27 coefficients; DC red/green/blue ${f(sh.coeffs[0])}, ${f(sh.coeffs[1])}, ${f(sh.coeffs[2])}');
}
Output
27 coefficients; DC red/green/blue 1.2336, -0.0087, 0.5674

Properties

coeffsfield
final Float32List coeffs

Methods

addSamplemethod
void addSample(Vec3 d, Vec3 rgb, double weight)

Adds radiance rgb arriving from unit direction d with Monte Carlo weight weight (e.g. \frac{4\pi}{N} for N uniform sphere samples).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // A sky: blue from above, orange from the horizon, sampled over 200 Hammersley directions.
  final sh = ShRgb();
  for (var i = 0; i < 200; i++) {
    final d = LowDiscrepancy.uniformSphere(LowDiscrepancy.hammersley(i, 200));
    final c = d.z > 0 ? Vec3(0.2, 0.4, 1).scale(d.z) + Vec3(1, 0.5, 0.1).scale(1 - d.z) : Vec3(0.1, 0.1, 0.1);
    sh.addSample(d, c, 4 * math.pi / 200);
  }
  print('up-facing irradiance ${v3(sh.irradiance(Vec3.unitZ))}');
}
Output
up-facing irradiance (1.4829, 1.3750, 2.2177)
Vec3 irradiance(Vec3 n)

Irradiance E(n) for unit normal n; divide by \pi for Lambertian outgoing radiance.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // A sky: blue from above, orange from the horizon, sampled over 200 Hammersley directions.
  final sh = ShRgb();
  for (var i = 0; i < 200; i++) {
    final d = LowDiscrepancy.uniformSphere(LowDiscrepancy.hammersley(i, 200));
    final c = d.z > 0 ? Vec3(0.2, 0.4, 1).scale(d.z) + Vec3(1, 0.5, 0.1).scale(1 - d.z) : Vec3(0.1, 0.1, 0.1);
    sh.addSample(d, c, 4 * math.pi / 200);
  }
  print('up ${v3(sh.irradiance(Vec3.unitZ))}');
  print('sideways ${v3(sh.irradiance(Vec3.unitX))}');
  print('down ${v3(sh.irradiance(Vec3(0, 0, -1)))}');
}
Output
up (1.4829, 1.3750, 2.2177)
sideways (1.1863, 0.8699, 0.9068)
down (0.3217, 0.3121, 0.2981)
class

Brdf

abstract final class Brdf

Physically based shading terms (Cook–Torrance microfacet model as used by UE4, Karis 2013, and Filament). These are CPU reference versions: the same formulas run in riptide_render's shaders, and tests compare the two.

Static

ggxDstaticmethod
static double ggxD(double nDotH, double roughness)

GGX / Trowbridge–Reitz normal distribution D(h) with alpha = roughness²: \alpha^{2} / (\pi ((n \cdot h)^{2}(\alpha^{2} - 1) + 1)^{2}).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('peak at n·h = 1: ${f(Brdf.ggxD(1, 0.5))}');
}
Output
peak at n·h = 1: 5.0930
static double smithGgxCorrelatedV(double nDotV, double nDotL, double roughness)

Height-correlated Smith visibility V = G / (4 (n \cdot l)(n \cdot v)) for GGX (Heitz 2014; Lagarde 2014 form).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('V at normal incidence: ${f(Brdf.smithGgxCorrelatedV(1, 1, 0.5))}');
}
Output
V at normal incidence: 0.2500
schlickFstaticmethod
static double schlickF(double vDotH, double f0)

Schlick's Fresnel: F_{0} + (1 - F_{0})(1 - v \cdot h)^{5}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('F at 0°: ${f(Brdf.schlickF(1, 0.04))}, at 90°: ${f(Brdf.schlickF(0, 0.04))}');
}
Output
F at 0°: 0.0400, at 90°: 1.0000
burleyDiffusestaticmethod
static double burleyDiffuse(double nDotV, double nDotL, double lDotH, double roughness)

Burley's ("Disney") diffuse, normalised so a white surface under uniform light reflects ≈ 1 (Burley 2012).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print('rough, grazing light: ${f(Brdf.burleyDiffuse(1, 0.1, 0.5, 1))}');
  print('smooth: ${f(Brdf.burleyDiffuse(1, 0.1, 0.5, 0))}');
}
Output
rough, grazing light: 0.3183
smooth: 0.2243
specularstaticmethod
static double specular(Vec3 n, Vec3 v, Vec3 l, double roughness, double f0)

Full specular lobe D \cdot V \cdot F for unit vectors n, v, l.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final n = Vec3.unitZ, v = Vec3(0, -0.5, 1).normalized();
  final lobe = [for (var i = -40; i <= 40; i++) (() { final a = i * math.pi / 90; final l = Vec3(0, math.sin(a), math.cos(a)); return [i * 2, Brdf.specular(n, v, l, 0.4, 0.04)]; })()];
  print('peak near the mirror direction');
}
Output from the Raya VM
peak near the mirror direction
class

CubeMap

abstract final class CubeMap

Cube-map direction math (D_{3}D/Vulkan/Metal face order +X, -X, +Y, -Y, +Z, -Z; u right, v down).

Static

directionstaticmethod
static Vec3 direction(int face, double u, double v)

The direction through texel coordinates (u, v) in [-1, 1] on face.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  for (var face = 0; face < 6; face++) {
    print('face $face centre -> ${v3(CubeMap.direction(face, 0.5, 0.5))}');
  }
}
Output
face 0 centre -> (0.8165, -0.4082, -0.4082)
face 1 centre -> (-0.8165, -0.4082, 0.4082)
face 2 centre -> (0.4082, 0.8165, 0.4082)
face 3 centre -> (0.4082, -0.8165, -0.4082)
face 4 centre -> (0.4082, -0.4082, 0.8165)
face 5 centre -> (-0.4082, -0.4082, -0.8165)
faceUvstaticmethod
static ({int face, double u, double v}) faceUv(Vec3 d)

The face and (u, v) in [-1, 1] for direction d (inverse of direction).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final r = CubeMap.faceUv(Vec3(0.2, 1, -0.3));
  print('face ${r.face}, uv (${f(r.u)}, ${f(r.v)})');
  final back = CubeMap.direction(r.face, r.u, r.v);
  print('back to a direction: ${v3(back.normalized())} vs ${v3(Vec3(0.2, 1, -0.3).normalized())}');
}
Output
face 2, uv (0.2000, -0.3000)
back to a direction: (0.1881, 0.9407, -0.2822) vs (0.1881, 0.9407, -0.2822)
texelSolidAnglestaticmethod
static double texelSolidAngle(double u0, double v0, double u1, double v1)

Solid angle subtended by the texel spanning (u_{0}, v_{0})–(u_{1}, v_{1}) on a face (exact, via the area element atan form).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // A face split into 4 × 4 texels: corner texels subtend less than centre ones; the face is 4π/6.
  var total = 0.0;
  final rows = <List<double>>[];
  for (var i = 0; i < 4; i++) {
    final row = <double>[];
    for (var j = 0; j < 4; j++) {
      final w = CubeMap.texelSolidAngle(j / 4, i / 4, (j + 1) / 4, (i + 1) / 4);
      total += w;
      row.add((w * 1e4).roundToDouble() / 1e4);
    }
    rows.add(row);
  }
  print('face total ${f(total)}, 4π/6 = ${f(4 * math.pi / 6)}');
}
Output
face total 0.5236, 4π/6 = 2.0944
class

Signal

abstract final class Signal

Audio and signal processing kernels.

Static

fftstaticmethod
static void fft(Float64List re, Float64List im, {bool inverse = false})

In-place iterative radix-2 Cooley–Tukey FFT on separate real and imaginary arrays (length a power of two). inverse computes the unnormalised inverse; divide by n afterwards.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const n = 16;
  final re = Float64List.fromList([for (var i = 0; i < n; i++) math.cos(2 * math.pi * 3 * i / n)]), im = Float64List(n);
  Signal.fft(re, im);
  print('|X_3| = ${f(math.sqrt(re[3] * re[3] + im[3] * im[3]))}');
}
Output
|X_3| = 8.0000
resampleLinearstaticmethod
static Float32List resampleLinear(Float32List input, double ratio)

Linear-interpolation resampling of input by ratio = outRate / inRate (cheap; use resampleSinc for audio).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final x = Float32List.fromList([for (var i = 0; i < 16; i++) math.sin(2 * math.pi * i / 16)]);
  final y = Signal.resampleLinear(x, 2.5);
  print('16 -> ${y.length} samples');
}
Output
16 -> 40 samples
resampleSincstaticmethod
static Float32List resampleSinc(Float32List input, double ratio, {int taps = 16})

Windowed-sinc resampling (Hann window, taps per side): band-limited, so downsampling doesn't alias.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final x = Float32List.fromList([for (var i = 0; i < 16; i++) math.sin(2 * math.pi * i / 16)]);
  final y = Signal.resampleSinc(x, 2.5);
  print('16 -> ${y.length} samples, windowed sinc');
}
Output
16 -> 40 samples, windowed sinc
class

Biquad

final class Biquad

Second-order IIR filter (biquad) with the RBJ "Audio EQ Cookbook" designs, Direct Form I.

Constructors

factory Biquad.lowPass(double sampleRate, double freq, {double q = 0.7071})
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final b = Biquad.lowPass(48000, 1000);
  print('gain at 1 kHz ${f(b.magnitudeAt(48000, 1000))}');
}
Output
gain at 1 kHz 0.7071
factory Biquad.highPass(double sampleRate, double freq, {double q = 0.7071})
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final b = Biquad.highPass(48000, 1000);
  print('gain at 1 kHz ${f(b.magnitudeAt(48000, 1000))}');
}
Output
gain at 1 kHz 0.7071
factory Biquad.bandPass(double sampleRate, double freq, {double q = 1})
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final b = Biquad.bandPass(48000, 1000, q: 4);
  print('gain at 1 kHz ${f(b.magnitudeAt(48000, 1000))}');
}
Output
gain at 1 kHz 1.0000
factory Biquad.peaking(double sampleRate, double freq, double gainDb, {double q = 1})
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final b = Biquad.peaking(48000, 1000, 6);
  print('gain at 1 kHz ${f(b.magnitudeAt(48000, 1000))}');
}
Output
gain at 1 kHz 1.9953

Properties

b0field
final double b0
b1field
final double b1
b2field
final double b2
a1field
final double a1
a2field
final double a2

Methods

processmethod
double process(double x)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final b = Biquad.lowPass(1000, 20);
  final y = <List<double>>[];
  for (var i = 0; i < 200; i++) {
    final x = (i ~/ 50).isEven ? 1.0 : -1.0; // a square wave
    y.add([i / 1000, b.process(x)]);
  }
  print('smoothed square wave at the end: ${f(y.last[1])}');
}
Output
smoothed square wave at the end: -1.0307
double magnitudeAt(double sampleRate, double freq)

Magnitude response |H(e^{j\omega})| at freq.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final b = Biquad.lowPass(48000, 1000);
  print('at the cutoff: ${f(b.magnitudeAt(48000, 1000))} (1/√2)');
  print('10× above: ${f(b.magnitudeAt(48000, 10000))}');
}
Output
at the cutoff: 0.7071 (1/√2)
10× above: 0.0073
extension Mat4Projections on Mat4

Projections and camera helpers beyond Mat4's basics. All use right-handed view space (camera looks down -Z) and clip-space depth in [0, 1] (Metal, Vulkan, WebGPU, D_{3}D).

Methods

void setOrthographic(double l, double r, double b, double t, double near, double far)

Orthographic projection of the box [l, r] \times [b, t] × [-near, -far] to clip space.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat4.identity()..setOrthographic(-2, 2, -1, 1, 0.1, 10);
  print('(2, 1, −0.1) -> ${v3(m.transformPoint(Vec3(2, 1, -0.1)).xyz)} (a corner of the clip cube)');
}
Output
(2, 1, −0.1) -> (1.0000, 1.0000, 0.0000) (a corner of the clip cube)
void setPerspectiveReversedZ(double fovY, double aspect, double near, double far)

Reversed-Z perspective: near maps to depth 1 and far to 0. With a floating-point depth buffer this spreads precision evenly across distance (Reed 2015, "Depth precision visualized").

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat4.identity()..setPerspectiveReversedZ(math.pi / 3, 1, 0.1, 100);
  for (final z in [-0.1, -1.0, -100.0]) {
    print('z = $z -> depth ${f(m.transformPoint(Vec3(0, 0, z)).xyzOverW.z)} (near 1, far 0)');
  }
}
Output from the Raya VM
z = -0.1 -> depth 1.0000 (near 1, far 0)
z = -1.0 -> depth 0.0991 (near 1, far 0)
z = -100.0 -> depth 0.0000 (near 1, far 0)
void setPerspectiveInfiniteReversedZ(double fovY, double aspect, double near)

Reversed-Z perspective with the far plane at infinity: depth = near / (-z_{\mathrm{view}}). No far clipping at all.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat4.identity()..setPerspectiveInfiniteReversedZ(math.pi / 3, 1, 0.1);
  print('depth at 1e6: ${m.transformPoint(Vec3(0, 0, -1e6)).xyzOverW.z.toStringAsExponential(2)} (no far plane)');
}
Output
depth at 1e6: 1.00e-7 (no far plane)
bool normalMatrixInto(Mat3 out)

The normal matrix: inverse-transpose of the upper 3 \times 3, so normals stay perpendicular to surfaces under non-uniform scale. Written into out; returns false when the 3 \times 3 is singular.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat4.identity()..setTrs(Vec3.zero, Quat.identity, Vec3(2, 1, 1)); // non-uniform scale
  final n = Mat3.identity();
  print('ok: ${m.normalMatrixInto(n)}');
  final normal = n.transform(Vec3(1, 1, 0).normalized()).normalized();
  print('normal of the x = y plane after stretching x: ${v3(normal)}');
}
Output
ok: true
normal of the x = y plane after stretching x: (0.4472, 0.8944, 0.0000)
extension type

Plane

extension type const Plane._(Float32x4 v)

A plane n \cdot p + d = 0 with unit normal n (lanes x, y, z = n, w = d). Points with n \cdot p + d > 0 are in front.

Constructors

Plane.fromNormalDistance(Vec3 n, double d)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final p = Plane.fromNormalDistance(Vec3.unitY, -2); // y = 2
  print('d(0, 5, 0) = ${f(p.signedDistance(Vec3(0, 5, 0)))}');
}
Output
d(0, 5, 0) = 3.0000
factory Plane.fromPointNormal(Vec3 p, Vec3 n)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final p = Plane.fromPointNormal(Vec3(1, 1, 0), Vec3(1, 1, 0).normalized());
  final pts = [Vec3(0, 0, 0), Vec3(2, 2, 0), Vec3(2, 0, 0)];
  for (final q in pts) {
    print('${v3(q)}: ${f(p.signedDistance(q))}');
  }
}
Output
(0.0000, 0.0000, 0.0000): -1.4142
(2.0000, 2.0000, 0.0000): 1.4142
(2.0000, 0.0000, 0.0000): 0.0000

Properties

normalgetter
Vec3 get normal
dgetter
double get d

Methods

double signedDistance(Vec3 p)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final p = Plane.fromPointNormal(Vec3.zero, Vec3.unitZ);
  print([for (final z in [-2.0, 0.0, 3.0]) f(p.signedDistance(Vec3(1, 1, z)))]);
}
Output
[-2.0000, 0.0000, 3.0000]
Plane normalized()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final p = Plane.fromNormalDistance(Vec3(0, 3, 4), -10).normalized();
  print('normal ${v3(p.normal)}, d ${f(p.d)}');
}
Output
normal (0.0000, 0.6000, 0.8000), d -2.0000
class

Frustum

final class Frustum

The 6 frustum planes of a view-projection matrix, pointing inward (Gribb and Hartmann 2001), for depth in [0, 1].

Constructors

Frustum.fromViewProjection(Mat4 m)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final proj = Mat4.identity()..setPerspective(math.pi / 2, 1, 1, 10);
  final fr = Frustum.fromViewProjection(proj);
  print('6 planes; near plane normal ${v3(fr.planes[4].normal.normalized())}');
}
Output
6 planes; near plane normal (0.0000, 0.0000, -1.0000)

Properties

planesfield
final List<Plane> planes

Methods

bool containsPoint(Vec3 p)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final proj = Mat4.identity()..setPerspective(math.pi / 2, 1, 1, 10);
  final fr = Frustum.fromViewProjection(proj);
  for (final (n, p) in [('in', Vec3(0, 0, -5)), ('out', Vec3(8, 0, -5)), ('far', Vec3(0, 0, -12))]) {
    print('$n: ${fr.containsPoint(p)}');
  }
}
Output
in: true
out: false
far: false
bool intersectsSphere(Vec3 center, double radius)

Conservative sphere test: false only when the sphere is entirely outside one plane.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final proj = Mat4.identity()..setPerspective(math.pi / 2, 1, 1, 10);
  final fr = Frustum.fromViewProjection(proj);
  print('sphere at (6, 0, −5) r 1.5: ${fr.intersectsSphere(Vec3(6, 0, -5), 1.5)}');
  print('sphere at (8, 0, −5) r 1: ${fr.intersectsSphere(Vec3(8, 0, -5), 1)}');
}
Output
sphere at (6, 0, −5) r 1.5: true
sphere at (8, 0, −5) r 1: false
bool intersectsAabb(Vec3 min, Vec3 max)

Conservative AABB test using each plane's "positive vertex".

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final proj = Mat4.identity()..setPerspective(math.pi / 2, 1, 1, 10);
  final fr = Frustum.fromViewProjection(proj);
  print('box near the axis: ${fr.intersectsAabb(Vec3(-1, -1, -6), Vec3(1, 1, -4))}');
  print('box off to the side: ${fr.intersectsAabb(Vec3(20, -1, -6), Vec3(22, 1, -4))}');
}
Output
box near the axis: true
box off to the side: false
extension type

Vec2

extension type const Vec2._(Float32x4 v)

A 2D vector (lanes x, y).

Constructors

Vec2constructor
Vec2(double x, double y)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec2(3, 1), b = Vec2(1, 2);
  print('a = (${a.x}, ${a.y}), |a| = ${f(a.length)}');
}
Output
a = (3.0, 1.0), |a| = 3.1623

Static

zerostaticfield
static final zero

Properties

xgetter
double get x
ygetter
double get y
length2getter
double get length2
lengthgetter
double get length

Methods

scalemethod
Vec2 scale(double s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec2(3, 1), b = Vec2(1, 2);
  final s = a.scale(1.5);
  print('1.5a = (${s.x}, ${s.y})');
}
Output
1.5a = (4.5, 1.5)
mulmethod
Vec2 mul(Vec2 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec2(3, 1), b = Vec2(1, 2);
  final m = a.mul(b);
  print('component-wise a·b = (${m.x}, ${m.y})');
}
Output
component-wise a·b = (3.0, 2.0)
dotmethod
double dot(Vec2 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec2(3, 1), b = Vec2(1, 2);
  print('a · b = ${a.dot(b)}');
  print('spread between them: ${f(1 - a.dot(b) * a.dot(b) / (a.length2 * b.length2))}');
}
Output
a · b = 5.0
spread between them: 0.5000
crossmethod
double cross(Vec2 o)

The z of the 3D cross product: positive when o is counter-clockwise from this.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec2(3, 1), b = Vec2(1, 2);
  print('a × b = ${a.cross(b)} (twice the triangle area, positive: b is to the left)');
}
Output
a × b = 5.0 (twice the triangle area, positive: b is to the left)
Vec2 normalized()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec2(3, 1), b = Vec2(1, 2);
  final n = a.normalized();
  print('(${f(n.x)}, ${f(n.y)}), length ${f(n.length)}');
}
Output from the Raya VM
(0.9487, 0.3162), length 1.0000
lerpmethod
Vec2 lerp(Vec2 to, double t)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec2(3, 1), b = Vec2(1, 2);
  final pts = [for (var i = 0; i <= 4; i++) a.lerp(b, i / 4)];
  print([for (final p in pts) '(${f(p.x)}, ${f(p.y)})']);
}
Output
[(3.0000, 1.0000), (2.5000, 1.2500), (2.0000, 1.5000), (1.5000, 1.7500), (1.0000, 2.0000)]

Operators

operator +operator
Vec2 operator +(Vec2 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec2(3, 1), b = Vec2(1, 2);
  final s = a + b;
  print('a + b = (${s.x}, ${s.y})');
}
Output
a + b = (4.0, 3.0)
operator -operator
Vec2 operator -(Vec2 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec2(3, 1), b = Vec2(1, 2);
  final d = a - b;
  print('a − b = (${d.x}, ${d.y})');
}
Output
a − b = (2.0, -1.0)
operator -operator
Vec2 operator -()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec2(3, 1), b = Vec2(1, 2);
  final n = -a;
  print('−a = (${n.x}, ${n.y})');
}
Output
−a = (-3.0, -1.0)
extension type

Vec3

extension type const Vec3._(Float32x4 v)

A 3D vector (lanes x, y, z; w is 0).

Constructors

Vec3constructor
Vec3(double x, double y, double z)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
  print('a = ${v3(a)}, |a|² = ${f(a.length2)}');
}
Output
a = (1.0000, 2.0000, 3.0000), |a|² = 14.0000
Vec3.fromSimdconstructor
Vec3.fromSimd(Float32x4 v)

Wraps a SIMD value read from storage (e.g. a Float32x4List); its w lane is cleared.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final v = Vec3.fromSimd(Float32x4(1, 2, 3, 99));
  print('${v3(v)} (the w lane is ignored)');
}
Output
(1.0000, 2.0000, 3.0000) (the w lane is ignored)

Static

zerostaticfield
static final zero
unitXstaticfield
static final unitX
unitYstaticfield
static final unitY
unitZstaticfield
static final unitZ

Properties

xgetter
double get x
ygetter
double get y
zgetter
double get z
length2getter
double get length2
lengthgetter
double get length

Methods

scalemethod
Vec3 scale(double s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
  print('2a = ${v3(a.scale(2))}');
}
Output
2a = (2.0000, 4.0000, 6.0000)
mulmethod
Vec3 mul(Vec3 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
  print('a ∘ b = ${v3(a.mul(b))}');
}
Output
a ∘ b = (-2.0000, 1.0000, 3.0000)
minmethod
Vec3 min(Vec3 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
  print('min = ${v3(a.min(b))}');
}
Output
min = (-2.0000, 0.5000, 1.0000)
maxmethod
Vec3 max(Vec3 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
  print('max = ${v3(a.max(b))}');
}
Output
max = (1.0000, 2.0000, 3.0000)
dotmethod
double dot(Vec3 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
  print('a · b = ${f(a.dot(b))}');
}
Output
a · b = 2.0000
crossmethod
Vec3 cross(Vec3 o)

Cross product via two shuffles: (a.yzx * b.zxy) - (a.zxy * b.yzx).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
  final c = a.cross(b);
  print('a × b = ${v3(c)}; perpendicular: ${f(c.dot(a))}, ${f(c.dot(b))}');
}
Output
a × b = (0.5000, -7.0000, 4.5000); perpendicular: 0.0000, 0.0000
Vec3 normalized()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
  final n = a.normalized();
  print('${v3(n)}, length ${f(n.length)}');
}
Output
(0.2673, 0.5345, 0.8018), length 1.0000
lerpmethod
Vec3 lerp(Vec3 to, double t)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
  print('halfway ${v3(a.lerp(b, 0.5))}');
}
Output
halfway (-0.5000, 1.2500, 2.0000)
reflectmethod
Vec3 reflect(Vec3 n)

Reflects this direction about the unit normal n: d - 2(d \cdot n)n.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final d = Vec3(1, -1, 0), n = Vec3.unitY;
  final r = d.reflect(n);
  print('${v3(d)} reflected off the floor: ${v3(r)}');
}
Output
(1.0000, -1.0000, 0.0000) reflected off the floor: (1.0000, 1.0000, 0.0000)
toVec4method
Vec4 toVec4(double w)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
  final p = a.toVec4(1), d = a.toVec4(0);
  print('point w = ${p.w}, direction w = ${d.w}');
}
Output
point w = 1.0, direction w = 0.0

Operators

operator +operator
Vec3 operator +(Vec3 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
  print('a + b = ${v3(a + b)}');
}
Output
a + b = (-1.0000, 2.5000, 4.0000)
operator -operator
Vec3 operator -(Vec3 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
  print('a − b = ${v3(a - b)}');
}
Output
a − b = (3.0000, 1.5000, 2.0000)
operator -operator
Vec3 operator -()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec3(1, 2, 3), b = Vec3(-2, 0.5, 1);
  print('−a = ${v3(-a)}');
}
Output
−a = (-1.0000, -2.0000, -3.0000)
extension type

Vec4

extension type const Vec4._(Float32x4 v)

A 4D vector or homogeneous point.

Constructors

Vec4constructor
Vec4(double x, double y, double z, double w)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
  print('a = ' + '(${f(a.x)}, ${f(a.y)}, ${f(a.z)}, ${f(a.w)})');
  print('perspective divide of (2, 4, 6, 2): ${v3(Vec4(2, 4, 6, 2).xyzOverW)}');
}
Output
a = (1.0000, 2.0000, 3.0000, 1.0000)
perspective divide of (2, 4, 6, 2): (1.0000, 2.0000, 3.0000)
Vec4.fromSimdconstructor
const Vec4.fromSimd(this.v)

Wraps a SIMD value read from storage or produced by a matrix.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final v = Vec4.fromSimd(Float32x4(1, 2, 3, 4));
  print('w = ${v.w}');
}
Output
w = 4.0

Static

zerostaticfield
static final zero

Properties

xgetter
double get x
ygetter
double get y
zgetter
double get z
wgetter
double get w
lengthgetter
double get length
xyzOverWgetter
Vec3 get xyzOverW

Perspective divide: \frac{x}{w}, \frac{y}{w}, \frac{z}{w}.

xyzgetter
Vec3 get xyz

Methods

scalemethod
Vec4 scale(double s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
  final s = a.scale(2);
  print('(${f(s.x)}, ${f(s.y)}, ${f(s.z)}, ${f(s.w)})');
}
Output
(2.0000, 4.0000, 6.0000, 2.0000)
mulmethod
Vec4 mul(Vec4 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
  final m = a.mul(b);
  print('(${f(m.x)}, ${f(m.y)}, ${f(m.z)}, ${f(m.w)})');
}
Output
(0.5000, 0.0000, -3.0000, 0.0000)
dotmethod
double dot(Vec4 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
  print('a · b = ${f(a.dot(b))}');
}
Output
a · b = -2.5000
lerpmethod
Vec4 lerp(Vec4 to, double t)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
  final m = a.lerp(b, 0.5);
  print('(${f(m.x)}, ${f(m.y)}, ${f(m.z)}, ${f(m.w)})');
}
Output
(0.7500, 1.0000, 1.0000, 0.5000)

Operators

operator +operator
Vec4 operator +(Vec4 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
  final s = a + b;
  print('(${f(s.x)}, ${f(s.y)}, ${f(s.z)}, ${f(s.w)})');
}
Output
(1.5000, 2.0000, 2.0000, 1.0000)
operator -operator
Vec4 operator -(Vec4 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
  final s = a - b;
  print('(${f(s.x)}, ${f(s.y)}, ${f(s.z)}, ${f(s.w)})');
}
Output
(0.5000, 2.0000, 4.0000, 1.0000)
operator -operator
Vec4 operator -()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Vec4(1, 2, 3, 1), b = Vec4(0.5, 0, -1, 0);
  final s = -a;
  print('(${f(s.x)}, ${f(s.y)}, ${f(s.z)}, ${f(s.w)})');
}
Output
(-1.0000, -2.0000, -3.0000, -1.0000)
class

Vec3d

final class Vec3d

A 3D vector in double precision for simulation state (rigid bodies, orbits, soft bodies), where float32's 7 digits lose momentum and energy over long runs. A value class: on Raya AOT it is stored inline and passed in registers; on other runtimes it is a small immutable object.

Constructors

Vec3dconstructor
const Vec3d(this.x, this.y, this.z)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print('a = $a, |a| = ${a.length}');
  print('double precision: 0.1 + 0.2 stays ${(b.x + b.y)}');
}
Output
a = Vec3d(1.0, 2.0, 2.0), |a| = 3.0
double precision: 0.1 + 0.2 stays 0.30000000000000004
Vec3d.readfactory
factory Vec3d.read(Float64List a, int i)

Reads element i of an interleaved xyz array.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final buf = Float64List.fromList([1, 2, 3, 4, 5, 6]);
  print('second vector: ${Vec3d.read(buf, 1)}');
}
Output
second vector: Vec3d(4.0, 5.0, 6.0)
factory Vec3d.fromVec3(Vec3 v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final v = Vec3d.fromVec3(Vec3(0.1, 0.2, 0.3));
  print('${v.x} (the float32 value of 0.1, widened)');
}
Output
0.10000000149011612 (the float32 value of 0.1, widened)

Static

zeroconstant
static const zero = Vec3d(0, 0, 0)
unitXconstant
static const unitX = Vec3d(1, 0, 0)
unitYconstant
static const unitY = Vec3d(0, 1, 0)
unitZconstant
static const unitZ = Vec3d(0, 0, 1)

Properties

xfield
final double x
yfield
final double y
zfield
final double z
length2getter
double get length2
lengthgetter
double get length
hashCodegetter
int get hashCode

Methods

mulmethod
Vec3d mul(Vec3d o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print(a.mul(b));
}
Output
Vec3d(0.1, 0.4, 0.6)
dotmethod
double dot(Vec3d o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print(a.dot(b));
}
Output
1.1
crossmethod
Vec3d cross(Vec3d o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  final c = a.cross(b);
  print('$c, ⟂ a: ${c.dot(a).abs() < 1e-15}');
}
Output
Vec3d(0.19999999999999996, -0.09999999999999998, 0.0), ⟂ a: true
Vec3d normalized()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print('${a.normalized()} (exactly (1, 2, 2)/3)');
}
Output
Vec3d(0.3333333333333333, 0.6666666666666666, 0.6666666666666666) (exactly (1, 2, 2)/3)
minmethod
Vec3d min(Vec3d o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print(a.min(b));
}
Output
Vec3d(0.1, 0.2, 0.3)
maxmethod
Vec3d max(Vec3d o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print(a.max(b));
}
Output
Vec3d(1.0, 2.0, 2.0)
absmethod
Vec3d abs()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print(const Vec3d(-1, 2, -3).abs());
}
Output
Vec3d(1.0, 2.0, 3.0)
writemethod
void write(Float64List a, int i)

Writes this vector as element i of an interleaved xyz array.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  final buf = Float64List(6);
  a.write(buf, 0);
  b.write(buf, 1);
  print(buf);
}
Output
[1.0, 2.0, 2.0, 0.1, 0.2, 0.3]
toVec3method
Vec3 toVec3()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print(v3(b.toVec3()));
}
Output
(0.1000, 0.2000, 0.3000)
basismethod
(Vec3d, Vec3d) basis()

Two unit vectors that complete this unit vector to an orthonormal basis (Duff et al. 2017, branchless).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final n = const Vec3d(1, 1, 1).normalized();
  final (t, s) = n.basis();
  print('t = ${d3(t)}, s = ${d3(s)}');
  print('orthonormal: ${f(t.dot(n))}, ${f(s.dot(n))}, ${f(t.dot(s))}, |t| = ${f(t.length)}');
}
Output
t = (0.7887, -0.2113, -0.5774), s = (-0.2113, 0.7887, -0.5774)
orthonormal: 0.0000, 0.0000, 0.0000, |t| = 1.0000
toStringmethod
String toString()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print(a);
}
Output
Vec3d(1.0, 2.0, 2.0)

Operators

operator +operator
Vec3d operator +(Vec3d o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print(a + b);
}
Output
Vec3d(1.1, 2.2, 2.3)
operator -operator
Vec3d operator -(Vec3d o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print(a - b);
}
Output
Vec3d(0.9, 1.8, 1.7)
operator -operator
Vec3d operator -()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print(-a);
}
Output
Vec3d(-1.0, -2.0, -2.0)
operator *operator
Vec3d operator *(double s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print(a * 0.5);
}
Output
Vec3d(0.5, 1.0, 1.0)
operator /operator
Vec3d operator /(double s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print(a / 3);
}
Output
Vec3d(0.3333333333333333, 0.6666666666666666, 0.6666666666666666)
operator []operator
double operator [](int i)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print([a[0], a[1], a[2]]);
}
Output
[1.0, 2.0, 2.0]
operator ==operator
bool operator ==(Object other)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const a = Vec3d(1, 2, 2), b = Vec3d(0.1, 0.2, 0.3);
  print(a == const Vec3d(1, 2, 2));
  print(b == const Vec3d(0.1, 0.2, 0.30000000000000004));
}
Output
true
false
class

Quatd

final class Quatd

A unit quaternion in double precision (x, y, z vector part, w scalar part), for rigid-body orientation.

Constructors

Quatdconstructor
const Quatd(this.x, this.y, this.z, this.w)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const q = Quatd(0, 0, 0, 1);
  print('identity: $q');
}
Output
identity: Quatd(0.0, 0.0, 0.0, 1.0)
factory Quatd.axisAngle(Vec3d axis, double radians)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
  print('x -> ${d3(q.rotate(Vec3d.unitX))}');
}
Output
x -> (0.0000, 1.0000, 0.0000)
factory Quatd.fromQuat(Quat q)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q = Quatd.fromQuat(Quat.axisAngle(Vec3.unitX, 1));
  print('widened: $q');
}
Output
widened: Quatd(0.4794255495071411, 0.0, 0.0, 0.8775825500488281)

Static

identityconstant
static const identity = Quatd(0, 0, 0, 1)

Properties

xfield
final double x
yfield
final double y
zfield
final double z
wfield
final double w
conjugategetter
Quatd get conjugate

Methods

dotmethod
double dot(Quatd o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
  print('q·q = ${q.dot(q)}');
}
Output
q·q = 1.0
Quatd normalized()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  print(const Quatd(1, 1, 1, 1).normalized());
}
Output
Quatd(0.5, 0.5, 0.5, 0.5)
rotatemethod
Vec3d rotate(Vec3d p)

Rotates p: p + 2w(u \times p) + 2u \times (u \times p).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
  final pts = [for (var k = 0; k <= 4; k++) Quatd.axisAngle(Vec3d.unitZ, k * math.pi / 8).rotate(const Vec3d(2, 0, 0))];
  print('y axis: ${d3(q.rotate(Vec3d.unitY))}');
}
Output
y axis: (-1.0000, 0.0000, 0.0000)
unrotatemethod
Vec3d unrotate(Vec3d p)

Rotates p by the inverse rotation.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
  final p = q.rotate(const Vec3d(1, 2, 3));
  print('back: ${d3(q.unrotate(p))}');
}
Output
back: (1.0000, 2.0000, 3.0000)
integratemethod
Quatd integrate(Vec3d omega, double dt)

Integrates angular velocity omega (world frame, rad/s) over dt: q + \tfrac{1}{2} dt (\omega, 0) \circ q, renormalised. The first-order update used by Box2D, Bullet and Jolt; exact enough at game time steps, and normalising each step keeps it a rotation.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // Spin at 1 rad/s about z for 1 s in 100 first-order steps.
  var q = Quatd.identity;
  for (var i = 0; i < 100; i++) {
    q = q.integrate(Vec3d.unitZ, 0.01);
  }
  print('angle reached ${f(2 * math.atan2(q.z, q.w))} rad (1)');
}
Output
angle reached 1.0000 rad (1)
Quatd integrateExact(Vec3d omega, double dt)

Integrates by the exact rotation exp(\tfrac{1}{2} \omega dt) (no first-order error; preferred for fast spins).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  var q = Quatd.identity;
  for (var i = 0; i < 10; i++) {
    q = q.integrateExact(const Vec3d(0, 0, 20), 0.01); // fast spin
  }
  print('angle ${f(2 * math.atan2(q.z, q.w))} rad (2 exactly)');
}
Output
angle 2.0000 rad (2 exactly)
toMat3dmethod
Mat3d toMat3d()

The rotation matrix R (row-major) with R v = \operatorname{rotate}(v).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
  final m = q.toMat3d();
  print('m · x = (${f(m.at(0, 0))}, ${f(m.at(1, 0))}, ${f(m.at(2, 0))})');
}
Output
m · x = (0.0000, 1.0000, 0.0000)
toQuatmethod
Quat toQuat()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
  final f32 = q.toQuat();
  print('as float32: ${v3(f32.rotate(Vec3.unitX))}');
}
Output
as float32: (0.0000, 1.0000, 0.0000)
toStringmethod
String toString()
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final q = Quatd.axisAngle(Vec3d.unitZ, math.pi / 2);
  print(q);
}
Output
Quatd(0.0, 0.0, 0.7071067811865475, 0.7071067811865476)

Operators

operator *operator
Quatd operator *(Quatd o)

Hamilton product: this ∘ o (apply o first).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Quatd.axisAngle(Vec3d.unitZ, math.pi / 4);
  print('two eighth turns: ${d3((a * a).rotate(Vec3d.unitX))}');
}
Output
two eighth turns: (0.0000, 1.0000, 0.0000)
extension

Mat3dVec

extension Mat3dVec on Mat3d

Mat3d operations on Vec3d, used for inertia tensors.

Static

skewstaticmethod
static Mat3d skew(Vec3d v)

The skew-symmetric cross-product matrix v× (so v× w = v \times w).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final s = Mat3dVec.skew(const Vec3d(1, 2, 3));
  print('[v]ₓ · w = ${d3(s.mulVec(const Vec3d(4, 5, 6)))}, v × w = ${d3(const Vec3d(1, 2, 3).cross(const Vec3d(4, 5, 6)))}');
}
Output
[v]ₓ · w = (-3.0000, 6.0000, -3.0000), v × w = (-3.0000, 6.0000, -3.0000)

Methods

mulVecmethod
Vec3d mulVec(Vec3d v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat3d.of([2, 0, 0, 0, 3, 0, 0, 0, 4]);
  print(d3(m.mulVec(const Vec3d(1, 1, 1))));
}
Output
(2.0000, 3.0000, 4.0000)
mulVecTmethod
Vec3d mulVecT(Vec3d v)

M^{T} v.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat3d.of([1, 2, 0, 0, 1, 0, 0, 0, 1]);
  print('M x = ${d3(m.mulVec(const Vec3d(1, 1, 0)))}, Mᵀx = ${d3(m.mulVecT(const Vec3d(1, 1, 0)))}');
}
Output
M x = (3.0000, 1.0000, 0.0000), Mᵀx = (1.0000, 3.0000, 0.0000)
scaledmethod
Mat3d scaled(double s)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat3d.of([2, 0, 0, 0, 3, 0, 0, 0, 4]);
  final s = m.scaled(0.5);
  print([s.at(0, 0), s.at(1, 1), s.at(2, 2)]);
}
Output
[1.0, 1.5, 2.0]
inversemethod
Mat3d inverse()

The inverse by the adjugate; a zero matrix when singular (infinite mass in that direction).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat3d.of([4, 7, 0, 2, 6, 0, 0, 0, 1]);
  final i = m.inverse();
  final p = m * i;
  print('M·M⁻¹ diagonal ${[f(p.at(0, 0)), f(p.at(1, 1)), f(p.at(2, 2))]}, off ${f(p.at(0, 1))}');
}
Output
M·M⁻¹ diagonal [1.0000, 1.0000, 1.0000], off 0.0000
rotatedBymethod
Mat3d rotatedBy(Mat3d r)

R M R^{T}: a body-frame tensor in world frame.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  // A body-frame inertia tensor seen in the world after a 45° turn about z.
  final i = Mat3d.of([1, 0, 0, 0, 4, 0, 0, 0, 5]);
  final r = Quatd.axisAngle(Vec3d.unitZ, math.pi / 4).toMat3d();
  final w = i.rotatedBy(r);
  print('trace kept: ${f(w.at(0, 0) + w.at(1, 1) + w.at(2, 2))}');
}
Output
trace kept: 10.0000

Operators

operator +operator
Mat3d operator +(Mat3d o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final m = Mat3d.of([2, 0, 0, 0, 3, 0, 0, 0, 4]);
  final s = m + Mat3d.identity();
  print([s.at(0, 0), s.at(1, 1), s.at(2, 2)]);
}
Output
[3.0, 4.0, 5.0]
abstract interface class RatioBackend<S, V>

One number system for ratio geometry: the same operations (quadrance, spread, cross, twist, solid and dihedral spreads, Gram determinants, reflections, Cayley rotations) in a fixed metric, over scalars S and vectors V.

This is the contract every backend meets and the law suite (package:riptide_math/ratio_laws.ray) checks: f64 and f32 (FloatRatio2, FloatRatio3), exact int64 rationals (Int64Ratio), the batched f32 kernels (CPU and Metal, RatioBatch), and in riptide_rational_geometry every exact field (\mathbb{Q}, \mathbb{Q}(\sqrt{d}), \mathbb{Z}/p) through FieldRatio. Hot paths don't call through this interface; they call the same formulas directly (RatioF, the Vec3 extensions, Turn, RatioQuat), which the float backends delegate to. Inputs are built from integers (vector) so every backend can be fed the same cases and compared.

Properties

namegetter
String get name
dimensiongetter
int get dimension
exactgetter
bool get exact

True when equality is exact (no tolerance).

Methods

fromIntmethod
S fromInt(int n)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print('fromInt(7) = ${b.fromInt(7)}');
}
Output
fromInt(7) = 7
addmethod
S add(S a, S b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 add 1/6 = ${b.add(x, y)}');
}
Output
1/3 add 1/6 = 1/2
submethod
S sub(S a, S b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 sub 1/6 = ${b.sub(x, y)}');
}
Output
1/3 sub 1/6 = 1/6
mulmethod
S mul(S a, S b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 mul 1/6 = ${b.mul(x, y)}');
}
Output
1/3 mul 1/6 = 1/18
divmethod
S div(S a, S b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 div 1/6 = ${b.div(x, y)}');
}
Output
1/3 div 1/6 = 2
isZeromethod
bool isZero(S a, S scale)

Whether a is zero: exactly for exact backends, within the backend's rounding relative to scale otherwise.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  final x = b.sub(b.div(b.fromInt(1), b.fromInt(3)), b.div(b.fromInt(2), b.fromInt(6)));
  print('1/3 − 2/6 is zero: ${b.isZero(x, b.fromInt(1))}');
}
Output
1/3 − 2/6 is zero: true
toDoublemethod
double? toDouble(S a)

A double for comparing across backends; null where there is none (finite fields).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print(b.toDouble(b.div(b.fromInt(2), b.fromInt(3))));
}
Output
0.6666666666666666
vectormethod
V vector(List<int> coords)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print(b.vector([3, 4]));
}
Output
[3, 4]
vaddmethod
V vadd(V a, V b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print(b.vadd(b.vector([3, 4]), b.vector([4, -3])));
}
Output
[7, 1]
vsubmethod
V vsub(V a, V b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print(b.vsub(b.vector([3, 4]), b.vector([4, -3])));
}
Output
[-1, 7]
vscalemethod
V vscale(S k, V v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print(b.vscale(b.div(b.fromInt(1), b.fromInt(2)), b.vector([3, 4])));
}
Output
[3/2, 2]
samemethod
bool same(V a, V b, S scale)

Whether a and b are equal: exactly for exact backends; for floats within the backend's rounding of an intermediate of coordinate size √scale (pass the largest Euclidean quadrance size involved, since an ill-conditioned step, such as a reflection in a nearly null normal, scales the error with it).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print(b.same(b.vscale(b.fromInt(2), b.vector([1, 2])), b.vadd(b.vector([1, 2]), b.vector([1, 2])), b.size(b.vector([1, 2]))));
}
Output
true
sizemethod
S size(V v)

The Euclidean quadrance of the coordinates (x^{2} + y^{2} + \cdots), whatever the metric: the size used for error scales.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print('size of [3, 4]: ${b.size(b.vector([3, 4]))}');
}
Output
size of [3, 4]: 25
dotmethod
S dot(V a, V b)

B(a, b) in the backend's metric.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print('dot([3, 4], [1, 2]) = ${b.dot(b.vector([3, 4]), b.vector([1, 2]))}');
  print('dot([3, 4], [4, -3]) = ${b.dot(b.vector([3, 4]), b.vector([4, -3]))}');
}
Output
dot([3, 4], [1, 2]) = 11
dot([3, 4], [4, -3]) = 0
quadrancemethod
S quadrance(V a)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print('Q([3, 4]) = ${b.quadrance(b.vector([3, 4]))}');
}
Output
Q([3, 4]) = 25
spreadmethod
S spread(V a, V b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print('spread([3, 4], [1, 2]) = ${b.spread(b.vector([3, 4]), b.vector([1, 2]))}');
  print('spread([3, 4], [4, -3]) = ${b.spread(b.vector([3, 4]), b.vector([4, -3]))}');
}
Output
spread([3, 4], [1, 2]) = 4/125
spread([3, 4], [4, -3]) = 1
crossmethod
S cross(V a, V b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print('cross([3, 4], [1, 2]) = ${b.cross(b.vector([3, 4]), b.vector([1, 2]))}');
  print('cross([3, 4], [4, -3]) = ${b.cross(b.vector([3, 4]), b.vector([4, -3]))}');
}
Output
cross([3, 4], [1, 2]) = 121/125
cross([3, 4], [4, -3]) = 0
twistmethod
S twist(V a, V b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print('twist([3, 4], [1, 2]) = ${b.twist(b.vector([3, 4]), b.vector([1, 2]))}');
  print('twist([3, 4], [1, 3]) = ${b.twist(b.vector([3, 4]), b.vector([1, 3]))}');
}
Output
twist([3, 4], [1, 2]) = 4/121
twist([3, 4], [1, 3]) = 1/9
S solidSpread(V a, V b, V c)

The solid spread \det \frac{G(a, b, c)}{Q(a)Q(b)Q(c)} (three dimensions).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  // Three-dimensional quantities need a 3-D metric.
  final b3 = Int64Ratio([1, 0, 0, 0, 1, 0, 0, 0, 1]);
  print('solid spread of the axes: ${b3.solidSpread(b3.vector([1, 0, 0]), b3.vector([0, 1, 0]), b3.vector([0, 0, 1]))}');
  print('of a flat triple: ${b3.solidSpread(b3.vector([1, 0, 0]), b3.vector([0, 1, 0]), b3.vector([1, 1, 0]))}');
}
Output
solid spread of the axes: 1
of a flat triple: 0
S dihedralSpread(V common, V a, V b)

The dihedral spread between span(common, a) and span(common, b) (three dimensions).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  // Three-dimensional quantities need a 3-D metric.
  final b3 = Int64Ratio([1, 0, 0, 0, 1, 0, 0, 0, 1]);
  print('dihedral spread of the xz and yz planes along z: ${b3.dihedralSpread(b3.vector([0, 0, 1]), b3.vector([1, 0, 0]), b3.vector([1, 1, 0]))}');
}
Output
dihedral spread of the xz and yz planes along z: 1/2
S gramDeterminant(List<V> vs)

Det of the Gram matrix B(v_{i}, v_{j}).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print(b.gramDeterminant([b.vector([3, 4]), b.vector([1, 2])]));
}
Output
4
reflectmethod
V reflect(V v, V normal)

The reflection of v in the hyperplane B-perpendicular to normal.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  print('reflect [3, 4] in [1, 2]: ${b.reflect(b.vector([3, 4]), b.vector([1, 2]))}');
}
Output
reflect [3, 4] in [1, 2]: [-7/5, -24/5]
V perpendicular(List<V> vs)

A vector B-perpendicular to the dimension - 1 vectors vs (2-D: one vector; 3-D: the B-vector product).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  final p = b.perpendicular([b.vector([3, 4])]);
  print('$p, dot ${b.dot(p, b.vector([3, 4]))}');
}
Output
[-4, 3], dot 0
rotatemethod
V rotate(List<S> cayley, V v)

v under the Cayley rotation (B - K)^{-1}(B + K) of the metric: cayley is t in two dimensions, the Gibbs vector g (K = gₓ) in three.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  // Any backend implements the same exact-geometry interface; here the exact int64 one.
  final RatioBackend<Frac, List<Frac>> b = Int64Ratio([1, 0, 0, 1]);
  final t = b.div(b.fromInt(1), b.fromInt(2));
  print('rotate [3, 4] by t = 1/2: ${b.rotate([t], b.vector([3, 4]))}');
}
Output
rotate [3, 4] by t = 1/2: [5, 0]
final class FloatRatio2 implements RatioBackend<double, Float64List>

F64 or f32 ratio geometry in a planar Metric2, on the float core. f32 stores every vector and result in single precision (what a GPU buffer holds); arithmetic is done in double and rounded once per result.

Constructors

FloatRatio2constructor
FloatRatio2(this.metric, {this.single = false})
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final blue = FloatRatio2(const Metric2(1, 0, 1)), red = FloatRatio2(const Metric2(1, 0, -1));
  final u = blue.vector([3, 4]);
  print('blue Q = ${blue.quadrance(u)}, red Q = ${red.quadrance(red.vector([3, 4]))}');
  final single = FloatRatio2(const Metric2(1, 0, 1), single: true);
  print('float32 rounding: ${single.div(single.fromInt(1), single.fromInt(3))}');
}
Output
blue Q = 25.0, red Q = -7.0
float32 rounding: 0.3333333432674408

Properties

metricfield
final Metric2 metric
singlefield
final bool single
namegetter
String get name
dimensiongetter
int get dimension
exactgetter
bool get exact

Methods

fromIntmethod
double fromInt(int n)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print('fromInt(7) = ${b.fromInt(7)}');
}
Output
fromInt(7) = 7.0
addmethod
double add(double a, double b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 add 1/6 = ${b.add(x, y)}');
}
Output
1/3 add 1/6 = 0.5
submethod
double sub(double a, double b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 sub 1/6 = ${b.sub(x, y)}');
}
Output
1/3 sub 1/6 = 0.16666666666666666
mulmethod
double mul(double a, double b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 mul 1/6 = ${b.mul(x, y)}');
}
Output
1/3 mul 1/6 = 0.05555555555555555
divmethod
double div(double a, double b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 div 1/6 = ${b.div(x, y)}');
}
Output
1/3 div 1/6 = 2.0
isZeromethod
bool isZero(double a, double scale)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  final x = b.sub(b.div(b.fromInt(1), b.fromInt(3)), b.div(b.fromInt(2), b.fromInt(6)));
  print('1/3 − 2/6 is zero: ${b.isZero(x, b.fromInt(1))}');
}
Output
1/3 − 2/6 is zero: true
toDoublemethod
double? toDouble(double a)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print(b.toDouble(b.div(b.fromInt(2), b.fromInt(3))));
}
Output
0.6666666666666666
vectormethod
Float64List vector(List<int> c)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print(b.vector([3, 4]));
}
Output
[3.0, 4.0]
vaddmethod
Float64List vadd(Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print(b.vadd(b.vector([3, 4]), b.vector([4, -3])));
}
Output
[7.0, 1.0]
vsubmethod
Float64List vsub(Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print(b.vsub(b.vector([3, 4]), b.vector([4, -3])));
}
Output
[-1.0, 7.0]
vscalemethod
Float64List vscale(double k, Float64List v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print(b.vscale(b.div(b.fromInt(1), b.fromInt(2)), b.vector([3, 4])));
}
Output
[1.5, 2.0]
samemethod
bool same(Float64List a, Float64List b, double scale)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print(b.same(b.vscale(b.fromInt(2), b.vector([1, 2])), b.vadd(b.vector([1, 2]), b.vector([1, 2])), b.size(b.vector([1, 2]))));
}
Output
true
sizemethod
double size(Float64List v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print('size of [3, 4]: ${b.size(b.vector([3, 4]))}');
}
Output
size of [3, 4]: 25.0
dotmethod
double dot(Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print('dot([3, 4], [1, 2]) = ${b.dot(b.vector([3, 4]), b.vector([1, 2]))}');
  print('dot([3, 4], [4, -3]) = ${b.dot(b.vector([3, 4]), b.vector([4, -3]))}');
}
Output
dot([3, 4], [1, 2]) = 11.0
dot([3, 4], [4, -3]) = 0.0
quadrancemethod
double quadrance(Float64List a)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print('Q([3, 4]) = ${b.quadrance(b.vector([3, 4]))}');
}
Output
Q([3, 4]) = 25.0
spreadmethod
double spread(Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print('spread([3, 4], [1, 2]) = ${b.spread(b.vector([3, 4]), b.vector([1, 2]))}');
  print('spread([3, 4], [4, -3]) = ${b.spread(b.vector([3, 4]), b.vector([4, -3]))}');
}
Output
spread([3, 4], [1, 2]) = 0.032
spread([3, 4], [4, -3]) = 1.0
crossmethod
double cross(Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print('cross([3, 4], [1, 2]) = ${b.cross(b.vector([3, 4]), b.vector([1, 2]))}');
  print('cross([3, 4], [4, -3]) = ${b.cross(b.vector([3, 4]), b.vector([4, -3]))}');
}
Output
cross([3, 4], [1, 2]) = 0.968
cross([3, 4], [4, -3]) = 0.0
twistmethod
double twist(Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print('twist([3, 4], [1, 2]) = ${b.twist(b.vector([3, 4]), b.vector([1, 2]))}');
  print('twist([3, 4], [1, 3]) = ${b.twist(b.vector([3, 4]), b.vector([1, 3]))}');
}
Output
twist([3, 4], [1, 2]) = 0.03305785123966942
twist([3, 4], [1, 3]) = 0.1111111111111111
double solidSpread(Float64List a, Float64List b, Float64List c)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  // Three-dimensional quantities need a 3-D metric.
  final b3 = FloatRatio3(const Metric3(1, 1, 1));
  print('solid spread of the axes: ${b3.solidSpread(b3.vector([1, 0, 0]), b3.vector([0, 1, 0]), b3.vector([0, 0, 1]))}');
  print('of a flat triple: ${b3.solidSpread(b3.vector([1, 0, 0]), b3.vector([0, 1, 0]), b3.vector([1, 1, 0]))}');
}
Output
solid spread of the axes: 1.0
of a flat triple: 0.0
double dihedralSpread(Float64List common, Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  // Three-dimensional quantities need a 3-D metric.
  final b3 = FloatRatio3(const Metric3(1, 1, 1));
  print('dihedral spread of the xz and yz planes along z: ${b3.dihedralSpread(b3.vector([0, 0, 1]), b3.vector([1, 0, 0]), b3.vector([1, 1, 0]))}');
}
Output
dihedral spread of the xz and yz planes along z: 0.5
double gramDeterminant(List<Float64List> vs)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print(b.gramDeterminant([b.vector([3, 4]), b.vector([1, 2])]));
}
Output
4.0
reflectmethod
Float64List reflect(Float64List v, Float64List n)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  print('reflect [3, 4] in [1, 2]: ${b.reflect(b.vector([3, 4]), b.vector([1, 2]))}');
}
Output
reflect [3, 4] in [1, 2]: [-1.4000000000000004, -4.800000000000001]
Float64List perpendicular(List<Float64List> vs)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  final p = b.perpendicular([b.vector([3, 4])]);
  print('$p, dot ${b.dot(p, b.vector([3, 4]))}');
}
Output
[-4.0, 3.0], dot 0.0
rotatemethod
Float64List rotate(List<double> cayley, Float64List v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio2(const Metric2(1, 0, 1));
  final t = b.div(b.fromInt(1), b.fromInt(2));
  print('rotate [3, 4] by t = 1/2: ${b.rotate([t], b.vector([3, 4]))}');
}
Output
rotate [3, 4] by t = 1/2: [5.0, 0.0]
final class FloatRatio3 implements RatioBackend<double, Float64List>

F64 or f32 ratio geometry in a three-dimensional Metric3, on the float core.

Constructors

FloatRatio3constructor
FloatRatio3(this.metric, {this.single = false})
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final e = FloatRatio3(const Metric3(1, 1, 1));
  print('Q(1, 2, 2) = ${e.quadrance(e.vector([1, 2, 2]))}');
}
Output
Q(1, 2, 2) = 9.0

Properties

metricfield
final Metric3 metric
singlefield
final bool single
namegetter
String get name
dimensiongetter
int get dimension
exactgetter
bool get exact

Methods

fromIntmethod
double fromInt(int n)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print('fromInt(7) = ${b.fromInt(7)}');
}
Output
fromInt(7) = 7.0
addmethod
double add(double a, double b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 add 1/6 = ${b.add(x, y)}');
}
Output
1/3 add 1/6 = 0.5
submethod
double sub(double a, double b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 sub 1/6 = ${b.sub(x, y)}');
}
Output
1/3 sub 1/6 = 0.16666666666666666
mulmethod
double mul(double a, double b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 mul 1/6 = ${b.mul(x, y)}');
}
Output
1/3 mul 1/6 = 0.05555555555555555
divmethod
double div(double a, double b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 div 1/6 = ${b.div(x, y)}');
}
Output
1/3 div 1/6 = 2.0
isZeromethod
bool isZero(double a, double scale)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  final x = b.sub(b.div(b.fromInt(1), b.fromInt(3)), b.div(b.fromInt(2), b.fromInt(6)));
  print('1/3 − 2/6 is zero: ${b.isZero(x, b.fromInt(1))}');
}
Output
1/3 − 2/6 is zero: true
toDoublemethod
double? toDouble(double a)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print(b.toDouble(b.div(b.fromInt(2), b.fromInt(3))));
}
Output
0.6666666666666666
vectormethod
Float64List vector(List<int> c)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print(b.vector([1, 2, 2]));
}
Output
[1.0, 2.0, 2.0]
vaddmethod
Float64List vadd(Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print(b.vadd(b.vector([1, 2, 2]), b.vector([2, 1, -2])));
}
Output
[3.0, 3.0, 0.0]
vsubmethod
Float64List vsub(Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print(b.vsub(b.vector([1, 2, 2]), b.vector([2, 1, -2])));
}
Output
[-1.0, 1.0, 4.0]
vscalemethod
Float64List vscale(double k, Float64List v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print(b.vscale(b.div(b.fromInt(1), b.fromInt(2)), b.vector([1, 2, 2])));
}
Output
[0.5, 1.0, 1.0]
samemethod
bool same(Float64List a, Float64List b, double scale)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print(b.same(b.vscale(b.fromInt(2), b.vector([0, 0, 1])), b.vadd(b.vector([0, 0, 1]), b.vector([0, 0, 1])), b.size(b.vector([0, 0, 1]))));
}
Output
true
sizemethod
double size(Float64List v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print('size of [1, 2, 2]: ${b.size(b.vector([1, 2, 2]))}');
}
Output
size of [1, 2, 2]: 9.0
dotmethod
double dot(Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print('dot([1, 2, 2], [0, 0, 1]) = ${b.dot(b.vector([1, 2, 2]), b.vector([0, 0, 1]))}');
  print('dot([1, 2, 2], [2, 1, -2]) = ${b.dot(b.vector([1, 2, 2]), b.vector([2, 1, -2]))}');
}
Output
dot([1, 2, 2], [0, 0, 1]) = 2.0
dot([1, 2, 2], [2, 1, -2]) = 0.0
quadrancemethod
double quadrance(Float64List a)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print('Q([1, 2, 2]) = ${b.quadrance(b.vector([1, 2, 2]))}');
}
Output
Q([1, 2, 2]) = 9.0
spreadmethod
double spread(Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print('spread([1, 2, 2], [0, 0, 1]) = ${b.spread(b.vector([1, 2, 2]), b.vector([0, 0, 1]))}');
  print('spread([1, 2, 2], [2, 1, -2]) = ${b.spread(b.vector([1, 2, 2]), b.vector([2, 1, -2]))}');
}
Output
spread([1, 2, 2], [0, 0, 1]) = 0.5555555555555556
spread([1, 2, 2], [2, 1, -2]) = 1.0
crossmethod
double cross(Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print('cross([1, 2, 2], [0, 0, 1]) = ${b.cross(b.vector([1, 2, 2]), b.vector([0, 0, 1]))}');
  print('cross([1, 2, 2], [2, 1, -2]) = ${b.cross(b.vector([1, 2, 2]), b.vector([2, 1, -2]))}');
}
Output
cross([1, 2, 2], [0, 0, 1]) = 0.4444444444444444
cross([1, 2, 2], [2, 1, -2]) = 0.0
twistmethod
double twist(Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print('twist([1, 2, 2], [0, 0, 1]) = ${b.twist(b.vector([1, 2, 2]), b.vector([0, 0, 1]))}');
  print('twist([1, 2, 2], [2, 1, -2]) = ${b.twist(b.vector([1, 2, 2]), b.vector([2, 1, -2]))}');
}
Output
twist([1, 2, 2], [0, 0, 1]) = 1.25
twist([1, 2, 2], [2, 1, -2]) = Infinity
double solidSpread(Float64List a, Float64List b, Float64List c)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print(b.solidSpread(b.vector([1, 0, 0]), b.vector([0, 1, 0]), b.vector([1, 1, 1])));
}
Output
0.3333333333333333
double dihedralSpread(Float64List c, Float64List a, Float64List b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print(b.dihedralSpread(b.vector([0, 0, 1]), b.vector([1, 0, 0]), b.vector([1, 1, 0])));
}
Output
0.5
double gramDeterminant(List<Float64List> vs)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print(b.gramDeterminant([b.vector([1, 2, 2]), b.vector([0, 0, 1]), b.vector([2, 1, -2])]));
}
Output
9.0
reflectmethod
Float64List reflect(Float64List v, Float64List n)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print('reflect [1, 2, 2] in [0, 0, 1]: ${b.reflect(b.vector([1, 2, 2]), b.vector([0, 0, 1]))}');
}
Output
reflect [1, 2, 2] in [0, 0, 1]: [1.0, 2.0, -2.0]
Float64List perpendicular(List<Float64List> vs)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  final p = b.perpendicular([b.vector([1, 2, 2]), b.vector([0, 0, 1])]);
  print('$p, dots ${b.dot(p, b.vector([1, 2, 2]))}, ${b.dot(p, b.vector([0, 0, 1]))}');
}
Output
[2.0, -1.0, 0.0], dots 0.0, 0.0
rotatemethod
Float64List rotate(List<double> g, Float64List v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = FloatRatio3(const Metric3(1, 1, 1));
  print('rotate x by g = (0, 0, 1): ${b.rotate([b.fromInt(0), b.fromInt(0), b.fromInt(1)], b.vector([1, 0, 0]))}');
}
Output
rotate x by g = (0, 0, 1): [0.0, 1.0, 0.0]
class

Frac

final class Frac

An exact rational held as two int64s, reduced. Arithmetic throws RatioOverflow instead of wrapping.

Constructors

Fracfactory
factory Frac(int numerator, [int denominator = 1])
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(Frac(6, 8));
  print(Frac(3, -9));
  print(Frac(5));
}
Output
3/4
-1/3
5

Static

zeroconstant
static const zero = Frac._(0, 1)
oneconstant
static const one = Frac._(1, 1)

Properties

final int numerator
final int denominator
isZerogetter
bool get isZero
hashCodegetter
int get hashCode

Methods

toDoublemethod
double toDouble()
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Frac(1, 3), b = Frac(-1, 6);
  print(a.toDouble());
}
Output
0.3333333333333333
toStringmethod
String toString()
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Frac(1, 3), b = Frac(-1, 6);
  print('$a and $b');
}
Output
1/3 and -1/6

Operators

operator +operator
Frac operator +(Frac o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Frac(1, 3), b = Frac(-1, 6);
  print('a + b = ${a + b}');
}
Output
a + b = 1/6
operator -operator
Frac operator -(Frac o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Frac(1, 3), b = Frac(-1, 6);
  print('a - b = ${a - b}');
}
Output
a - b = 1/2
operator *operator
Frac operator *(Frac o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Frac(1, 3), b = Frac(-1, 6);
  print('a * b = ${a * b}');
}
Output
a * b = -1/18
operator /operator
Frac operator /(Frac o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Frac(1, 3), b = Frac(-1, 6);
  print('a / b = ${a / b}');
}
Output
a / b = -2
operator -operator
Frac operator -()
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Frac(1, 3), b = Frac(-1, 6);
  print(-a);
}
Output
-1/3
operator ==operator
bool operator ==(Object other)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(Frac(2, 4) == Frac(1, 2));
  print(Frac(1, 3) == Frac(1, 4));
}
Output
true
false
final class RatioOverflow extends StateError

An exact int64 result left the 62-bit range. Use the \mathbb{Q} backend (FieldRatio over BigInt rationals) instead.

Constructors

RatioOverflowconstructor
RatioOverflow(String what)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  try {
    var x = Frac(1, 3);
    for (var i = 0; i < 60; i++) {
      x = x * Frac(7, 3) + Frac(1, 5);
    }
    print(x);
  } on RatioOverflow catch (e) {
    print('caught: ${e.message}');
  } on StateError catch (e) {
    print('caught: ${e.message}');
  }
  print(RatioOverflow('demo').message);
}
Output
caught: int64 ratio overflow: 4049457491068014344 × 7
int64 ratio overflow: demo