Bitbybit Docs
    Preparing search index...

    Class Vector

    Vector maths on plain number arrays. A vector is an array of numbers; in 3D it is [x, y, z] with Y pointing up, the same shape as a point, so the two can be passed to each other's methods. Every method returns a new array or a number and never changes its inputs. Angles are in degrees.

    Index

    Constructors

    angles

    • Measures the angle between two vectors in degrees, always between 0 and 180.

      The direction of turning is not considered; use signedAngleBetween for that. Example: [1,0,0] and [0,1,0] -> 90

      Parameters

      Returns number

      Angle in degrees

      const angle = bitbybit.vector.angleBetween({ first: [1, 0, 0], second: [0, 1, 0] });
      
    • Measures the signed angle from the first 2D vector to the second, in degrees from -180 to 180.

      Only the first two entries of each vector are used; a positive angle turns counter-clockwise. Example: [1,0] to [0,1] -> 90, [0,1] to [1,0] -> -90

      Parameters

      Returns number

      Signed angle in degrees

      const angle = bitbybit.vector.angleBetweenNormalized2d({ first: [1, 0], second: [0, 1] });
      
    • Measures the angle from the first vector to the second, turning around a reference direction, in degrees from 0 to 360.

      The turn is counter-clockwise when the reference vector points toward you. Example: [1,0,0] to [0,0,-1] around [0,1,0] -> 90

      Parameters

      Returns number

      Angle in degrees from 0 to 360

      const angle = bitbybit.vector.positiveAngleBetween({ first: [1, 0, 0], second: [0, 0, -1], reference: [0, 1, 0] });
      
    • Measures the angle from the first vector to the second, turning around a reference direction, in degrees from 0 to 360.

      The turn is counter-clockwise when the reference vector points toward you: a clockwise turn of 30 degrees reads as 330. Example: [1,0,0] to [0,0,-1] around [0,1,0] -> 90

      Parameters

      Returns number

      Angle in degrees from 0 to 360

      const angle = bitbybit.vector.signedAngleBetween({ first: [1, 0, 0], second: [0, 0, -1], reference: [0, 1, 0] });
      

    base

    • Computes the cross product of two 3D vectors: a vector at right angles to both.

      Its direction follows the right-hand rule and its length is the area of the parallelogram the two vectors span. Example: [1,0,0] x [0,1,0] -> [0,0,1]

      Parameters

      Returns number[]

      The vector perpendicular to both

      const normal = bitbybit.vector.cross({ first: [1, 0, 0], second: [0, 1, 0] });
      
    • Divides every entry of a vector by one number.

      Example: [10,20,30] / 2 -> [5,10,15]

      Parameters

      Returns number[]

      The divided vector

      const half = bitbybit.vector.div({ vector: [10, 20, 30], scalar: 2 });
      
    • Subtracts the first value of a vector from its last, which for a sorted list is its range.

      Example: [1,3,5,9] -> 8

      Parameters

      Returns number

      Last value minus first value

      const span = bitbybit.vector.domain({ vector: [1, 3, 5, 9] });
      
    • Computes the dot product of two vectors: the sum of the products of matching entries.

      It is 0 for vectors at right angles and, for unit vectors, the cosine of the angle between them. Example: [1,2,3] and [4,5,6] -> 32

      Parameters

      Returns number

      The dot product

      const projection = bitbybit.vector.dot({ first: [1, 2, 3], second: [4, 5, 6] });
      
    • Multiplies every entry of a vector by one number.

      Example: [2,3,4] x 5 -> [10,15,20]

      Parameters

      Returns number[]

      The scaled vector

      const scaled = bitbybit.vector.mul({ vector: [2, 3, 4], scalar: 5 });
      
    • Flips the sign of every entry, so the vector points the opposite way.

      Example: [5,-3,2] -> [-5,3,-2]

      Parameters

      Returns number[]

      The negated vector

      const opposite = bitbybit.vector.neg({ vector: [5, -3, 2] });
      
    • Computes the squared length of a vector, which avoids the square root when only comparing lengths.

      Example: [3,4,0] -> 25

      Parameters

      Returns number

      The squared length

      const n2 = bitbybit.vector.normSquared({ vector: [3, 4, 0] });
      
    • Computes the length of a vector.

      Example: [3,4,0] -> 5, [1,0,0] -> 1

      Parameters

      Returns number

      The length in model units

      const len = bitbybit.vector.norm({ vector: [3, 4, 0] });
      
    • Scales a 3D vector to length 1 while keeping its direction.

      A vector shorter than 1e-8 has no direction to keep, so the result is undefined. Example: [3,4,0] -> [0.6,0.8,0]

      Parameters

      Returns number[]

      The unit vector, or undefined for a zero-length input

      const direction = bitbybit.vector.normalized({ vector: [3, 4, 0] });
      
    • Finds the point at a given distance from a start point along a direction.

      The direction is used as given, so a direction of length 2 travels twice the distance. Example: start [0,0,0], direction [1,0,0], distance 5 -> [5,0,0]

      Parameters

      • inputs: RayPointDto

        The start point, the direction and the distance

      Returns number[]

      The point on the ray

      const ahead = bitbybit.vector.onRay({ point: [0, 0, 0], vector: [1, 0, 0], distance: 5 });
      
    • Subtracts the second vector from the first, entry by entry.

      Example: [10,20,30] - [1,2,3] -> [9,18,27]

      Parameters

      • inputs: TwoVectorsDto

        The vector to subtract from and the vector to subtract

      Returns number[]

      The vector of differences

      const diff = bitbybit.vector.sub({ first: [10, 20, 30], second: [1, 2, 3] });
      
    • Adds up all values of a vector into one number.

      Example: [1,2,3,4] -> 10

      Parameters

      Returns number

      The total

      const total = bitbybit.vector.sum({ vector: [1, 2, 3, 4] });
      
    • Computes the squared length of a 3D vector, which avoids the square root when only comparing lengths.

      Example: [3,4,0] -> 25

      Parameters

      Returns number

      The squared length

      const l2 = bitbybit.vector.lengthSq({ vector: [3, 4, 0] });
      
    • Computes the length of a 3D vector.

      Example: [3,4,0] -> 5

      Parameters

      Returns number

      The length in model units

      const len = bitbybit.vector.length({ vector: [3, 4, 0] });
      

    create

    • Builds a 3D vector from its x, y and z values.

      Example: x=1, y=2, z=3 -> [1,2,3]

      Parameters

      Returns Vector3

      The vector [x, y, z]

      const up = bitbybit.vector.vectorXYZ({ x: 0, y: 1, z: 0 });
      
    • Builds a 2D vector from its x and y values.

      Example: x=3, y=4 -> [3,4]

      Parameters

      Returns Vector2

      The vector [x, y]

      const right = bitbybit.vector.vectorXY({ x: 1, y: 0 });
      
    • Lists the whole numbers from 0 up to, but not including, max.

      Example: max=5 -> [0,1,2,3,4]

      Parameters

      • inputs: RangeMaxDto

        The end of the range, which is left out

      Returns number[]

      The numbers from 0 to max - 1

      const indices = bitbybit.vector.range({ max: 5 });
      
    • Lists the numbers from min to max, stepping by step; max is included when a step lands on it.

      Example: min=0, max=10, step=2 -> [0,2,4,6,8,10]

      Parameters

      • inputs: SpanDto

        The start, the end and the step

      Returns number[]

      The numbers in the span

      const values = bitbybit.vector.span({ min: 0, max: 10, step: 2.5 });
      
    • Lists nrItems numbers from min to max spaced by an easing curve, so they bunch up at one end or both.

      With intervals on, the result holds the gaps between neighbors instead of the values themselves. Example: min=0, max=100, nrItems=5, ease='easeInQuad' -> [0, 6.25, 25, 56.25, 100]

      Parameters

      • inputs: SpanEaseItemsDto

        The start, the end, the number of items, the easing and whether to return the gaps

      Returns number[]

      The eased numbers, or the gaps between them

      const eased = bitbybit.vector.spanEaseItems({ min: 0, max: 100, nrItems: 5, ease: Bit.Inputs.Math.easeEnum.easeInQuad, intervals: false });
      
    • Lists nrItems evenly spaced numbers from min to max, both included.

      Example: min=0, max=10, nrItems=5 -> [0, 2.5, 5, 7.5, 10]

      Parameters

      Returns number[]

      The evenly spaced numbers

      const values = bitbybit.vector.spanLinearItems({ min: 0, max: 10, nrItems: 5 });
      
    • Turns a list of number strings into numbers.

      A string that is not a number becomes NaN. Example: ['1', '2.5', '3'] -> [1, 2.5, 3]

      Parameters

      Returns number[]

      The numbers

      const numbers = bitbybit.vector.parseNumbers({ vector: ["1", "2.5", "-3"] });
      

    distance

    • Computes the squared distance between two vectors, which avoids the square root when only comparing distances.

      Example: [0,0,0] to [3,4,0] -> 25

      Parameters

      Returns number

      The squared distance

      const d2 = bitbybit.vector.distSquared({ first: [0, 0, 0], second: [3, 4, 0] });
      
    • Computes the straight-line distance between two vectors.

      Example: [0,0,0] to [3,4,0] -> 5

      Parameters

      Returns number

      The distance in model units

      const distance = bitbybit.vector.dist({ first: [0, 0, 0], second: [3, 4, 0] });
      
    • Blends two vectors linearly by a fraction.

      fraction is the share of first: 1 gives first, 0 gives second, 0.5 the midpoint. Example: [0,0,0] and [10,10,10] at 0.5 -> [5,5,5]

      Parameters

      Returns number[]

      The blended vector

      const mid = bitbybit.vector.lerp({ first: [0, 0, 0], second: [10, 10, 10], fraction: 0.5 });
      

    extract

    • Finds the largest value in a vector.

      Example: [3, 7, 2, 9, 1] -> 9

      Parameters

      Returns number

      The largest entry

      const largest = bitbybit.vector.max({ vector: [3, 7, 2, 9, 1] });
      
    • Finds the smallest value in a vector.

      Example: [3, 7, 2, 9, 1] -> 1

      Parameters

      Returns number

      The smallest entry

      const smallest = bitbybit.vector.min({ vector: [3, 7, 2, 9, 1] });
      

    remove

    • Removes every repeated vector from a list, keeping the first occurrence of each.

      Two vectors count as the same when every entry differs by less than tolerance. Example: [[1,2,3], [4,5,6], [1,2,3], [7,8,9]] -> [[1,2,3], [4,5,6], [7,8,9]]

      Parameters

      Returns number[][]

      The vectors without repeats, in their original order

      const unique = bitbybit.vector.removeAllDuplicateVectors({
      vectors: [[1, 2, 3], [4, 5, 6], [1, 2, 3]],
      tolerance: 1e-7,
      });
    • Removes a vector when it repeats the one right before it; the same vector further away is kept.

      With checkFirstAndLast on, a last vector that repeats the first is dropped too, which closes a loop of points cleanly. Entries within tolerance of each other count as equal. Example: [[1,2], [1,2], [3,4], [1,2]] -> [[1,2], [3,4], [1,2]]

      Parameters

      Returns number[][]

      The vectors without consecutive repeats

      const cleaned = bitbybit.vector.removeConsecutiveDuplicateVectors({
      vectors: [[0, 0], [0, 0], [1, 1], [0, 0]],
      checkFirstAndLast: true,
      tolerance: 1e-7,
      });

    sum

    • Adds a list of vectors together entry by entry into one vector.

      The result has as many entries as the first vector. Example: [[1,2,3], [4,5,6], [7,8,9]] -> [12,15,18]

      Parameters

      Returns number[]

      The vector of sums

      const total = bitbybit.vector.addAll({ vectors: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] });
      
    • Adds two vectors entry by entry.

      Example: [1,2,3] + [4,5,6] -> [5,7,9]

      Parameters

      Returns number[]

      The vector of sums

      const sum = bitbybit.vector.add({ first: [1, 2, 3], second: [4, 5, 6] });
      
    • Tells whether every value in a list of booleans is true.

      Example: [true, true, true] -> true, [true, false, true] -> false

      Parameters

      Returns boolean

      True when no entry is false

      const allTrue = bitbybit.vector.all({ vector: [true, true, false] });
      

    validate

    • Tells whether two vectors are the same within a tolerance, entry by entry.

      Vectors of different length are never the same. Example: [1,2,3] and [1.0001,2.0001,3.0001] with tolerance 0.001 -> true

      Parameters

      Returns boolean

      True when every entry differs by less than the tolerance

      const same = bitbybit.vector.vectorsTheSame({ vec1: [1, 2, 3], vec2: [1, 2, 3.0000001], tolerance: 1e-6 });
      
    • Marks which entries of a vector are finite numbers.

      Example: [1, 2, Infinity, 3] -> [true, true, false, true]

      Parameters

      Returns boolean[]

      One boolean per entry, true when it is finite

      const flags = bitbybit.vector.finite({ vector: [1, Infinity, 3] });
      
    • Tells whether a vector has no length, that is, every entry is exactly 0.

      Example: [0,0,0] -> true, [0,0,0.001] -> false

      Parameters

      Returns boolean

      True when the length is 0

      const zero = bitbybit.vector.isZero({ vector: [0, 0, 0] });