Skip to content
FutureBlitz

    Install

    Import std.math.vector

    std.math.vector

    Vec2, Vec3, and Vec4: points and directions, as Floats, in the space std.math works in: right-handed, Y up, and -Z forward. Each is a Struct of its components, x, y, z, and w, in that order with nothing between, so SIMD can work on it as it stands. Their Operators work component by component, and a Float scales: v * 2, 2 * v, and v / 2. The math on them is Functions, as on Floats: Dot, Cross, Length, Normalize, Lerp, and the rest, beside the scalar ones of their names. Lengths and distances are worked out in Doubles, so no Float overflows on the way.

    std/math/vector.fb

    Types

    Vec2 struct

    Public Struct Vec2 Implements Numeric, Hashable, Printable, Defaultable

    A point or direction in 2D.

    std/math/vector.fb, line 14

    • Public Field x: Float field #

      Its x component.

    • Public Field y: Float field #

      Its y component.

    • Public Static Method Zero(): Vec2 static method #

      The number that adding leaves alone: 0.

      Doc from std.core.interfaces.Numeric.Zero.

    • Public Static Method One(): Vec2 static method #

      The number that multiplying leaves alone: 1.

      Doc from std.core.interfaces.Numeric.One.

    • Public Static Method DefaultValue(): Vec2 static method #

      The value to start from, like 0 for a number, "" for a String, or an empty collection.

      Doc from std.core.interfaces.Defaultable.DefaultValue.

    • @PanicsAtCaller Public Method ToVec2i(): Vec2i method #

      The components as Ints, toward 0, as As converts them: one outside an Int's range panics.

    • Public Method ToString(): String method #

      Its text, as a string interpolation like "{value}" shows it.

      Doc from std.core.interfaces.Printable.ToString.

    • @PanicsAtCaller Operator [](index: Int): Float operator #

      The component at the index: x at 0, and y at 1. Any other index panics.

    • Operator +(a: Vec2, b: Vec2): Vec2 operator #

      a and b added up.

      Doc from std.core.interfaces.Numeric.Operator+.

    • Operator -(a: Vec2, b: Vec2): Vec2 operator #

      b taken away from a.

      Doc from std.core.interfaces.Numeric.Operator-.

    • Operator *(a: Vec2, b: Vec2): Vec2 operator #

      a and b multiplied together.

      Doc from std.core.interfaces.Numeric.Operator*.

    • Operator /(a: Vec2, b: Vec2): Vec2 operator #

      a divided by b.

      Doc from std.core.interfaces.Numeric.Operator/.

    • Operator *(a: Vec2, scale: Float): Vec2 operator #

      a times scale.

    • Operator *(scale: Float, a: Vec2): Vec2 operator #

      scale times a.

    • Operator /(a: Vec2, scale: Float): Vec2 operator #

      a divided by scale.

    • Operator -(a: Vec2): Vec2 operator #

      a negated.

    Vec3 struct

    Public Struct Vec3 Implements Numeric, Hashable, Printable, Defaultable

    A point or direction in 3D.

    std/math/vector.fb, line 53

    • Public Field x: Float field #

      Its x component.

    • Public Field y: Float field #

      Its y component.

    • Public Field z: Float field #

      Its z component.

    • Public Static Method Zero(): Vec3 static method #

      The number that adding leaves alone: 0.

      Doc from std.core.interfaces.Numeric.Zero.

    • Public Static Method One(): Vec3 static method #

      The number that multiplying leaves alone: 1.

      Doc from std.core.interfaces.Numeric.One.

    • Public Static Method DefaultValue(): Vec3 static method #

      The value to start from, like 0 for a number, "" for a String, or an empty collection.

      Doc from std.core.interfaces.Defaultable.DefaultValue.

    • Public Static Property Up: Vec3 property #

      Up: +Y.

    • Public Static Property Down: Vec3 property #

      Down: -Y.

    • Public Static Property Right: Vec3 property #

      Right: +X.

    • Public Static Property Left: Vec3 property #

      Left: -X.

    • Public Static Property Forward: Vec3 property #

      -Z, where a camera with no rotation looks.

    • Public Static Property Back: Vec3 property #

      Back: +Z, toward the camera.

    • Public Property xy: Vec2 property #

      Its x and y, as a Vec2.

    • @PanicsAtCaller Public Method ToVec3i(): Vec3i method #

      The components as Ints, toward 0, as As converts them: one outside an Int's range panics.

    • Public Method ToString(): String method #

      Its text, as a string interpolation like "{value}" shows it.

      Doc from std.core.interfaces.Printable.ToString.

    • @PanicsAtCaller Operator [](index: Int): Float operator #

      The component at the index: x at 0, y at 1, and z at 2. Any other index panics.

    • Operator +(a: Vec3, b: Vec3): Vec3 operator #

      a and b added up.

      Doc from std.core.interfaces.Numeric.Operator+.

    • Operator -(a: Vec3, b: Vec3): Vec3 operator #

      b taken away from a.

      Doc from std.core.interfaces.Numeric.Operator-.

    • Operator *(a: Vec3, b: Vec3): Vec3 operator #

      a and b multiplied together.

      Doc from std.core.interfaces.Numeric.Operator*.

    • Operator /(a: Vec3, b: Vec3): Vec3 operator #

      a divided by b.

      Doc from std.core.interfaces.Numeric.Operator/.

    • Operator *(a: Vec3, scale: Float): Vec3 operator #

      a times scale.

    • Operator *(scale: Float, a: Vec3): Vec3 operator #

      scale times a.

    • Operator /(a: Vec3, scale: Float): Vec3 operator #

      a divided by scale.

    • Operator -(a: Vec3): Vec3 operator #

      a negated.

    Vec4 struct

    Public Struct Vec4 Implements Numeric, Hashable, Printable, Defaultable

    A point or direction in 4D, or a 3D one with a w, as a Mat4 transforms it.

    std/math/vector.fb, line 130

    • Public Field x: Float field #

      Its x component.

    • Public Field y: Float field #

      Its y component.

    • Public Field z: Float field #

      Its z component.

    • Public Field w: Float field #

      Its w component.

    • Public Static Method Zero(): Vec4 static method #

      The number that adding leaves alone: 0.

      Doc from std.core.interfaces.Numeric.Zero.

    • Public Static Method One(): Vec4 static method #

      The number that multiplying leaves alone: 1.

      Doc from std.core.interfaces.Numeric.One.

    • Public Static Method DefaultValue(): Vec4 static method #

      The value to start from, like 0 for a number, "" for a String, or an empty collection.

      Doc from std.core.interfaces.Defaultable.DefaultValue.

    • Public Property xy: Vec2 property #

      Its x and y, as a Vec2.

    • Public Property xyz: Vec3 property #

      Its x, y, and z, as a Vec3.

    • Public Method ToString(): String method #

      Its text, as a string interpolation like "{value}" shows it.

      Doc from std.core.interfaces.Printable.ToString.

    • @PanicsAtCaller Operator [](index: Int): Float operator #

      The component at the index: x at 0, y at 1, z at 2, and w at 3. Any other index panics.

    • Operator +(a: Vec4, b: Vec4): Vec4 operator #

      a and b added up.

      Doc from std.core.interfaces.Numeric.Operator+.

    • Operator -(a: Vec4, b: Vec4): Vec4 operator #

      b taken away from a.

      Doc from std.core.interfaces.Numeric.Operator-.

    • Operator *(a: Vec4, b: Vec4): Vec4 operator #

      a and b multiplied together.

      Doc from std.core.interfaces.Numeric.Operator*.

    • Operator /(a: Vec4, b: Vec4): Vec4 operator #

      a divided by b.

      Doc from std.core.interfaces.Numeric.Operator/.

    • Operator *(a: Vec4, scale: Float): Vec4 operator #

      a times scale.

    • Operator *(scale: Float, a: Vec4): Vec4 operator #

      scale times a.

    • Operator /(a: Vec4, scale: Float): Vec4 operator #

      a divided by scale.

    • Operator -(a: Vec4): Vec4 operator #

      a negated.

    Functions

    Dot function

    Public Function Dot(a: Vec2, b: Vec2): Float

    The sum of the products of the components: for unit vectors, the cosine of the angle between.

    std/math/vector.fb, line 191

    Dot function

    Public Function Dot(a: Vec3, b: Vec3): Float

    The sum of the products of the components: for unit vectors, the cosine of the angle between.

    std/math/vector.fb, line 194

    Dot function

    Public Function Dot(a: Vec4, b: Vec4): Float

    The sum of the products of the components.

    std/math/vector.fb, line 197

    Cross function

    Public Function Cross(a: Vec2, b: Vec2): Float

    The Z of the two as 3D vectors' Cross: above 0 when b turns counterclockwise from a.

    std/math/vector.fb, line 200

    Cross function

    Public Function Cross(a: Vec3, b: Vec3): Vec3

    The vector at right angles to both, by the right-hand rule: Cross(Right, Up) is Back, and its length is the area of the parallelogram the two make.

    std/math/vector.fb, line 204

    Length function

    Public Function Length(v: Vec2): Float

    How long the vector is.

    std/math/vector.fb, line 214

    Length function

    Public Function Length(v: Vec3): Float

    How long v is.

    std/math/vector.fb, line 216

    Length function

    Public Function Length(v: Vec4): Float

    How long v is.

    std/math/vector.fb, line 218

    LengthSquared function

    Public Function LengthSquared(v: Vec2): Float

    The length times itself, which takes no square root, for comparing lengths, in Floats.

    std/math/vector.fb, line 221

    LengthSquared function

    Public Function LengthSquared(v: Vec3): Float

    How long v is, times itself, which takes no square root.

    std/math/vector.fb, line 223

    LengthSquared function

    Public Function LengthSquared(v: Vec4): Float

    How long v is, times itself, which takes no square root.

    std/math/vector.fb, line 225

    Distance function

    Public Function Distance(a: Vec2, b: Vec2): Float

    How far apart the two points are.

    std/math/vector.fb, line 228

    Distance function

    Public Function Distance(a: Vec3, b: Vec3): Float

    How far apart the two points are.

    std/math/vector.fb, line 233

    DistanceSquared function

    Public Function DistanceSquared(a: Vec2, b: Vec2): Float

    The distance times itself, which takes no square root, for comparing distances, in Floats.

    std/math/vector.fb, line 239

    DistanceSquared function

    Public Function DistanceSquared(a: Vec3, b: Vec3): Float

    How far apart a and b are, times itself, which takes no square root.

    std/math/vector.fb, line 241

    Normalize function

    Public Function Normalize(v: Vec2): Vec2

    The vector in the same direction, of length 1, and the zero vector for the zero vector.

    std/math/vector.fb, line 244

    Normalize function

    Public Function Normalize(v: Vec3): Vec3

    The vector in the same direction, of length 1, and the zero vector for the zero vector.

    std/math/vector.fb, line 251

    Normalize function

    Public Function Normalize(v: Vec4): Vec4

    The vector in the same direction, of length 1, and the zero vector for the zero vector.

    std/math/vector.fb, line 258

    Lerp function

    Public Function Lerp(a: Vec2, b: Vec2, t: Float): Vec2

    The point t of the way from a to b: a at 0, and b itself at 1, though b - a rounds.

    std/math/vector.fb, line 267

    Lerp function

    Public Function Lerp(a: Vec3, b: Vec3, t: Float): Vec3

    The point t of the way from a to b: a at 0, and b itself at 1, though b - a rounds.

    std/math/vector.fb, line 271

    Lerp function

    Public Function Lerp(a: Vec4, b: Vec4, t: Float): Vec4

    The point t of the way from a to b: a at 0, and b itself at 1, though b - a rounds.

    std/math/vector.fb, line 275

    Min function

    Public Function Min(a: Vec2, b: Vec2): Vec2

    The smaller of each component.

    std/math/vector.fb, line 278

    Min function

    Public Function Min(a: Vec3, b: Vec3): Vec3

    The smaller of each component.

    std/math/vector.fb, line 281

    Min function

    Public Function Min(a: Vec4, b: Vec4): Vec4

    The smaller of each component.

    std/math/vector.fb, line 284

    Max function

    Public Function Max(a: Vec2, b: Vec2): Vec2

    The larger of each component.

    std/math/vector.fb, line 289

    Max function

    Public Function Max(a: Vec3, b: Vec3): Vec3

    The larger of each component.

    std/math/vector.fb, line 292

    Max function

    Public Function Max(a: Vec4, b: Vec4): Vec4

    The larger of each component.

    std/math/vector.fb, line 295

    Clamp function

    @PanicsAtCaller Public Function Clamp(v: Vec2, low: Vec2, high: Vec2): Vec2

    Each component kept between low's and high's, as Clamp keeps a Float. A component whose low is above its high panics.

    std/math/vector.fb, line 301

    Clamp function

    @PanicsAtCaller Public Function Clamp(v: Vec3, low: Vec3, high: Vec3): Vec3

    Each component kept between low's and high's, as Clamp keeps a Float. A component whose low is above its high panics.

    std/math/vector.fb, line 308

    Clamp function

    @PanicsAtCaller Public Function Clamp(v: Vec4, low: Vec4, high: Vec4): Vec4

    Each component kept between low's and high's, as Clamp keeps a Float. A component whose low is above its high panics.

    std/math/vector.fb, line 315

    Abs function

    Public Function Abs(v: Vec2): Vec2

    Each component of v without its sign.

    std/math/vector.fb, line 322

    Abs function

    Public Function Abs(v: Vec3): Vec3

    Each component of v without its sign.

    std/math/vector.fb, line 324

    Abs function

    Public Function Abs(v: Vec4): Vec4

    Each component of v without its sign.

    std/math/vector.fb, line 326

    Floor function

    Public Function Floor(v: Vec2): Vec2

    Each component of v rounded down.

    std/math/vector.fb, line 328

    Floor function

    Public Function Floor(v: Vec3): Vec3

    Each component of v rounded down.

    std/math/vector.fb, line 330

    Floor function

    Public Function Floor(v: Vec4): Vec4

    Each component of v rounded down.

    std/math/vector.fb, line 332

    Ceil function

    Public Function Ceil(v: Vec2): Vec2

    Each component of v rounded up.

    std/math/vector.fb, line 334

    Ceil function

    Public Function Ceil(v: Vec3): Vec3

    Each component of v rounded up.

    std/math/vector.fb, line 336

    Ceil function

    Public Function Ceil(v: Vec4): Vec4

    Each component of v rounded up.

    std/math/vector.fb, line 338

    Round function

    Public Function Round(v: Vec2): Vec2

    Each component of v rounded to the nearest whole number.

    std/math/vector.fb, line 340

    Round function

    Public Function Round(v: Vec3): Vec3

    Each component of v rounded to the nearest whole number.

    std/math/vector.fb, line 342

    Round function

    Public Function Round(v: Vec4): Vec4

    Each component of v rounded to the nearest whole number.

    std/math/vector.fb, line 344

    Reflect function

    Public Function Reflect(v: Vec2, normal: Vec2): Vec2

    The direction v bounces off a surface with this normal, of length 1, in: as a ball does off a wall.

    std/math/vector.fb, line 348

    Reflect function

    Public Function Reflect(v: Vec3, normal: Vec3): Vec3

    The direction v bounces off a surface with this normal, of length 1, in: as a ball does off a wall.

    std/math/vector.fb, line 352

    Project function

    Public Function Project(v: Vec2, onto: Vec2): Vec2

    The part of v along onto, the zero vector when onto is, worked out in Doubles.

    std/math/vector.fb, line 362

    Project function

    Public Function Project(v: Vec3, onto: Vec3): Vec3

    The part of v along onto, the zero vector when onto is, worked out in Doubles.

    std/math/vector.fb, line 370

    Angle function

    Public Function Angle(a: Vec2, b: Vec2): Float

    The angle between the two, in radians, from 0 to Pi, and 0 when either is the zero vector, worked out in Doubles.

    std/math/vector.fb, line 379

    Angle function

    Public Function Angle(a: Vec3, b: Vec3): Float

    The angle between the two, in radians, from 0 to Pi, and 0 when either is the zero vector, worked out in Doubles.

    std/math/vector.fb, line 386

    Rotate function

    Public Function Rotate(v: Vec2, angle: Float): Vec2

    The vector turned counterclockwise by the angle, in radians.

    std/math/vector.fb, line 394

    Perpendicular function

    Public Function Perpendicular(v: Vec2): Vec2

    The vector a quarter turn counterclockwise: (-y, x).

    std/math/vector.fb, line 401

    MoveTowards function

    Public Function MoveTowards(current: Vec2, target: Vec2, maxDistance: Float): Vec2

    Current moved toward target by maxDistance, or to target when it's no further, worked out in Doubles.

    std/math/vector.fb, line 405

    MoveTowards function

    Public Function MoveTowards(current: Vec3, target: Vec3, maxDistance: Float): Vec3

    Current moved toward target by maxDistance, or to target when it's no further, worked out in Doubles.

    std/math/vector.fb, line 416

    IsClose function

    Public Function IsClose(a: Vec2, b: Vec2): Bool

    Whether each component is close to b's, as IsClose tells for Floats.

    std/math/vector.fb, line 428

    IsClose function

    Public Function IsClose(a: Vec3, b: Vec3): Bool

    Whether each component is close to b's, as IsClose tells for Floats.

    std/math/vector.fb, line 431

    IsClose function

    Public Function IsClose(a: Vec4, b: Vec4): Bool

    Whether each component is close to b's, as IsClose tells for Floats.

    std/math/vector.fb, line 436

    IsClose function

    Public Function IsClose(a: Vec2, b: Vec2, tolerance: Float): Bool

    Whether each component is no further from b's than tolerance.

    std/math/vector.fb, line 441

    IsClose function

    Public Function IsClose(a: Vec3, b: Vec3, tolerance: Float): Bool

    Whether each component is no further from b's than tolerance.

    std/math/vector.fb, line 446

    IsClose function

    Public Function IsClose(a: Vec4, b: Vec4, tolerance: Float): Bool

    Whether each component is no further from b's than tolerance.

    std/math/vector.fb, line 452

    Report an issue with this page or with FutureBlitz on GitHub.