Skip to content
FutureBlitz

    Install

    Import std.math.vector

    std.math.vector

    2D, 3D, and 4D vectors of Floats.

    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.

    Examples

    
    Local a = Vec2(3, 4)
    Expect(Length(a) == 5.0 && LengthSquared(a) == 25.0 && DistanceSquared(a, Vec2(0, 0)) == 25.0)
    Expect(IsClose(Normalize(a), Vec2(0.6, 0.8)) && Cross(Vec2(1, 0), Vec2(0, 1)) == 1.0)
    Expect(Reflect(Vec2(1, -1), Vec2(0, 1)) == Vec2(1, 1))
    Expect(Project(Vec2(2, 3), Vec2(1, 0)) == Vec2(2, 0))
    Expect(IsClose(Angle(Vec2(1, 0), Vec2(0, 1)), Deg(90)))
    Expect(MoveTowards(Vec2(0, 0), Vec2(10, 0), 3) == Vec2(3, 0))
    Expect(Floor(Vec2(1.5, -1.5)) == Vec2(1, -2) && Ceil(Vec2(1.5, -1.5)) == Vec2(2, -1))
    Expect(Round(Vec2(1.4, 1.6)) == Vec2(1, 2) && Abs(Vec2(-1, 2)) == Vec2(1, 2))
    Expect(Min(a, Vec2(4, 3)) == Vec2(3, 3) && Max(a, Vec2(4, 3)) == Vec2(4, 4))
    Expect(Clamp(Vec2(-1, 9), Vec2(0, 0), Vec2(5, 5)) == Vec2(0, 5))
    Expect(Perpendicular(Vec2(1, 0)) == Vec2(0, 1))
    Expect(IsClose(Rotate(Vec2(1, 0), Deg(90)), Vec2(0, 1)))
    Local v = Vec4(1, 2, 3, 4)
    Expect(Dot(v, Vec4(1, 1, 1, 1)) == 10.0 && v.xy == Vec2(1, 2))
    Expect(Lerp(Vec4(0, 0, 0, 0), v, 0.5) == Vec4(0.5, 1, 1.5, 2))
    Expect(Distance(Vec3(0, 0, 0), Vec3(0, 3, 4)) == 5.0)

    std/math/vector.fb

    Types

    Vec2 struct

    Public Struct Vec2 Implements Numeric, Hashable, Printable, Defaultable

    A point or direction in 2D.

    Examples

    Local v = Vec2(3, 4)
    Expect(Length(v) == 5.0 && Distance(v, Vec2(0, 0)) == 5.0)
    Expect(Perpendicular(Vec2(1, 0)) == Vec2(0, 1))
    Expect(IsClose(Rotate(Vec2(1, 0), Deg(90)), Vec2(0, 1)))

    std/math/vector.fb, line 49

    • Public Field x: Float field #

      Its x component.

    • Public Field y: Float field #

      Its y component.

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

      The zero vector, (0, 0).

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

      The vector (1, 1), which multiplying leaves alone.

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

      The zero vector, (0, 0).

    • @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 components, as "(1, 2)".

    • @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 plus b, component by component.

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

      a minus b, component by component.

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

      a times b, component by component.

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

      a divided by b, component by component.

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

    Examples

    Local right = Vec3(1, 0, 0)
    Local up = Vec3(0, 1, 0)
    Expect(Dot(right, up) == 0.0)
    Expect(Cross(right, up) == Vec3(0, 0, 1))
    Expect(Length(Vec3(3, 4, 0)) == 5.0)
    Expect(Normalize(Vec3(0, 0, 9)) == Vec3(0, 0, 1))
    Expect(Lerp(right, up, 0.5) == Vec3(0.5, 0.5, 0))
    Expect(right * 2 + up == Vec3(2, 1, 0))

    std/math/vector.fb, line 109

    • 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 zero vector, (0, 0, 0).

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

      The vector (1, 1, 1), which multiplying leaves alone.

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

      The zero vector, (0, 0, 0).

    • 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 components, as "(1, 2, 3)".

    • @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 plus b, component by component.

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

      a minus b, component by component.

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

      a times b, component by component.

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

      a divided by b, component by component.

    • 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 194

    • 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 zero vector, (0, 0, 0, 0).

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

      The vector (1, 1, 1, 1), which multiplying leaves alone.

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

      The zero vector, (0, 0, 0, 0).

    • 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 components, as "(1, 2, 3, 4)".

    • @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 plus b, component by component.

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

      a minus b, component by component.

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

      a times b, component by component.

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

      a divided by b, component by component.

    • 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 263

    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 266

    Dot function

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

    The sum of the products of the components.

    std/math/vector.fb, line 269

    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 272

    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 276

    Length function

    Public Function Length(v: Vec2): Float

    How long the vector is.

    std/math/vector.fb, line 286

    Length function

    Public Function Length(v: Vec3): Float

    How long v is.

    std/math/vector.fb, line 288

    Length function

    Public Function Length(v: Vec4): Float

    How long v is.

    std/math/vector.fb, line 290

    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 293

    LengthSquared function

    Public Function LengthSquared(v: Vec3): Float

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

    std/math/vector.fb, line 295

    LengthSquared function

    Public Function LengthSquared(v: Vec4): Float

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

    std/math/vector.fb, line 297

    Distance function

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

    How far apart the two points are.

    std/math/vector.fb, line 300

    Distance function

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

    How far apart the two points are.

    std/math/vector.fb, line 305

    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 311

    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 313

    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 316

    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 323

    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 330

    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 339

    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 343

    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 347

    Min function

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

    The smaller of each component.

    std/math/vector.fb, line 350

    Min function

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

    The smaller of each component.

    std/math/vector.fb, line 353

    Min function

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

    The smaller of each component.

    std/math/vector.fb, line 356

    Max function

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

    The larger of each component.

    std/math/vector.fb, line 361

    Max function

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

    The larger of each component.

    std/math/vector.fb, line 364

    Max function

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

    The larger of each component.

    std/math/vector.fb, line 367

    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 373

    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 380

    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 387

    Abs function

    Public Function Abs(v: Vec2): Vec2

    Each component of v without its sign.

    std/math/vector.fb, line 394

    Abs function

    Public Function Abs(v: Vec3): Vec3

    Each component of v without its sign.

    std/math/vector.fb, line 396

    Abs function

    Public Function Abs(v: Vec4): Vec4

    Each component of v without its sign.

    std/math/vector.fb, line 398

    Floor function

    Public Function Floor(v: Vec2): Vec2

    Each component of v rounded down.

    std/math/vector.fb, line 400

    Floor function

    Public Function Floor(v: Vec3): Vec3

    Each component of v rounded down.

    std/math/vector.fb, line 402

    Floor function

    Public Function Floor(v: Vec4): Vec4

    Each component of v rounded down.

    std/math/vector.fb, line 404

    Ceil function

    Public Function Ceil(v: Vec2): Vec2

    Each component of v rounded up.

    std/math/vector.fb, line 406

    Ceil function

    Public Function Ceil(v: Vec3): Vec3

    Each component of v rounded up.

    std/math/vector.fb, line 408

    Ceil function

    Public Function Ceil(v: Vec4): Vec4

    Each component of v rounded up.

    std/math/vector.fb, line 410

    Round function

    Public Function Round(v: Vec2): Vec2

    Each component of v rounded to the nearest whole number.

    std/math/vector.fb, line 412

    Round function

    Public Function Round(v: Vec3): Vec3

    Each component of v rounded to the nearest whole number.

    std/math/vector.fb, line 414

    Round function

    Public Function Round(v: Vec4): Vec4

    Each component of v rounded to the nearest whole number.

    std/math/vector.fb, line 416

    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 420

    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 424

    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 434

    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 442

    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 451

    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 458

    Rotate function

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

    The vector turned counterclockwise by the angle, in radians.

    std/math/vector.fb, line 466

    Perpendicular function

    Public Function Perpendicular(v: Vec2): Vec2

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

    std/math/vector.fb, line 473

    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 477

    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 488

    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 500

    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 503

    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 508

    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 513

    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 518

    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 524

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