vec/vec4f

Types

A 4-element structure that can be used to represent 4D coordinates or any other quadruplet of float values.

pub type Vec4f =
  vec4.Vec4(Float)

Values

pub fn absolute_value(
  vector: vec4.Vec4(Float),
) -> vec4.Vec4(Float)

Returns a new vector with all elements in absolute values.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> absolute_value()
// -> Vec4(1.2, 3.4, 42.0, 0.69)
pub fn add(
  a: vec4.Vec4(Float),
  b: vec4.Vec4(Float),
) -> vec4.Vec4(Float)

Adds two vectors together.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> add(Vec4(2.1, 4.5, -2.0, 9.01))
// -> Vec4(3.3, 1.1, 40.0, 9.7)
pub fn anchor_position(
  vector: vec4.Vec4(Float),
  at position: vec4.Vec4(Float),
  then fun: fn(vec4.Vec4(Float)) -> vec4.Vec4(Float),
) -> vec4.Vec4(Float)

Return the equivalent of vector |> subtract(position) |> fun() |> add(position).

Examples

Vec4(1.2, -3.4, 42.0, 0.69)
|> anchor_position(Vec4(1.0, 2.1, 3.2, 4.3), scale(_, 2.0))
// -> Vec4(1.4, -8.9, 80.8, -2.92)
pub fn angle(a: vec4.Vec4(Float), b: vec4.Vec4(Float)) -> Float

Returns the angle (in radians) between two vectors.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> angle(Vec4(1.0, 2.1, 3.2, 4.3))
// -> 1.0
pub fn ceiling(vector: vec4.Vec4(Float)) -> vec4.Vec4(Float)

Returns a new vector with all elements rounded to the next highest whole number as a Float.

Examples

Vec4(1.2, -3.6, 42.0, 0.5) |> ceiling()
// -> Vec4(2.0, -3.0, 42.0, 1.0)
pub fn clamp(
  vector: vec4.Vec4(Float),
  min min_bound: vec4.Vec4(Float),
  max max_bound: vec4.Vec4(Float),
) -> vec4.Vec4(Float)

Returns a new vector with all components clamped between a lower and upper bound.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> clamp(
  min: Vec4(1.0, 2.1, -5.4, 7.5),
  max: Vec4(1.4, 18.2, 32.3, 9.1),
)
// -> Vec4(1.2, 2.1, 32.3, 7.5)
pub fn compare_distance(
  a: vec4.Vec4(Float),
  with b: vec4.Vec4(Float),
  to vector: vec4.Vec4(Float),
) -> order.Order

Compares two vector’s distances to a vector, returning an Order: Lt for lower than, Eq for equals, or Gt for greater than.

Examples

compare_distance(
  Vec4(1.2, -3.4, 42.0, 0.69),
  Vec4(1.0, 2.1, 3.2, 4.3),
  Vec4(-2.5, 6.7, 19.4, 0.0),
)
// -> Gt
pub fn compare_length(
  a: vec4.Vec4(Float),
  with b: vec4.Vec4(Float),
) -> order.Order

Compares two vector’s lengths, returning an Order: Lt for lower than, Eq for equals, or Gt for greater than.

Examples

compare_length(
  Vec4(1.2, -3.4, 42.0, 0.69),
  Vec4(1.0, 2.1, 3.2, 4.3),
)
// -> Gt
pub fn decoder() -> decode.Decoder(vec4.Vec4(Float))

A decoder that decodes 4D float vectors where all elements are decoded with a given decoder.

pub fn direction(
  a: vec4.Vec4(Float),
  to b: vec4.Vec4(Float),
) -> vec4.Vec4(Float)

Returns a normalized vector pointing from a to b.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> direction(Vec4(1.0, 2.1, 3.2, 4.3))
// -> Vec4(-0.0, 0.14, -0.99, 0.092)
pub fn distance(
  a: vec4.Vec4(Float),
  with b: vec4.Vec4(Float),
) -> Float

Returns the distance between two vectors.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> distance(Vec4(1.0, 2.1, 3.2, 4.3))
// -> 39.35
pub fn distance_squared(
  a: vec4.Vec4(Float),
  b: vec4.Vec4(Float),
) -> Float

Returns the squared distance between two vectors.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> distance_squared(Vec4(1.0, 2.1, 3.2, 4.3))
// -> 1548.76
pub fn divide(
  dividend: vec4.Vec4(Float),
  by divisor: vec4.Vec4(Float),
) -> Result(vec4.Vec4(Float), Nil)

Returns division of the inputs as a Result.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> divide(Vec4(2.0, 0.5, 4.0, 1.0))
// -> Ok(Vec4(0.6, -6.8, 10.5, 0.69))
Vec4(1.2, -3.4, 42.0, 0.69) |> divide(Vec4(0.0, 0.5, 4.0, 1.0))
// -> Error(Nil)
pub fn dot(a: vec4.Vec4(Float), b: vec4.Vec4(Float)) -> Float

Returns the dot product of two vectors.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> dot(Vec4(1.0, 2.1, 3.2, 4.3))
// -> 131.43
pub fn floor(vector: vec4.Vec4(Float)) -> vec4.Vec4(Float)

Returns a new vector with all elements rounded to the next lowest whole number as an Float.

Examples

Vec4(1.2, -3.6, 42.0, 0.5) |> floor()
// -> Vec4(1.0, -4.0, 42.0, 0.0)
pub fn length(vector: vec4.Vec4(Float)) -> Float

Returns the length (magnitude) of the vector.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> length()
// -> 42.16
pub fn length_squared(vector: vec4.Vec4(Float)) -> Float

Returns the squared length (squared magnitude) of the vector.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> length_squared()
// -> 1777.47
pub fn loosely_compare_distance(
  a: vec4.Vec4(Float),
  with b: vec4.Vec4(Float),
  to vector: vec4.Vec4(Float),
  tolerating tolerance: Float,
) -> order.Order

Compares two vector’s distances to a vector within a tolerance, returning an Order: Lt for lower than, Eq for equals, or Gt for greater than.

Examples

loosely_compare_distance(
  Vec4(1.2, -3.4, 42.0, 0.69),
  Vec4(1.25, -3.43, 42.0001, 0.6999),
  Vec4(-2.5, 6.7, 19.4, 0.0),
  tolerating: 1.0,
)
// -> Eq
pub fn loosely_compare_length(
  a: vec4.Vec4(Float),
  with b: vec4.Vec4(Float),
  tolerating tolerance: Float,
) -> order.Order

Compares two vector’s lengths within a tolerance, returning an Order: Lt for lower than, Eq for equals, or Gt for greater than.

Examples

loosely_compare_length(
  Vec4(1.2, -3.4, 42.0, 0.69),
  Vec4(-1.25, 3.43, -42.0001, -0.6999),
  tolerating: 0.5,
)
// -> Eq
pub fn loosely_equals(
  a: vec4.Vec4(Float),
  with b: vec4.Vec4(Float),
  tolerating tolerance: Float,
) -> Bool

Checks for equality of two vectors within a tolerance, returning an Bool.

Examples

Vec4(1.2, -3.4, 42.0, 0.69)
|> loosely_equals(Vec4(1.25, -3.43, 42.0001, 0.6999), tolerating: 0.1)
// -> True
pub fn max(
  a: vec4.Vec4(Float),
  b: vec4.Vec4(Float),
) -> vec4.Vec4(Float)

Compares two vectors, returning the larger of the two.

Examples

max(Vec4(1.2, -3.4, 42.0, 0.69), Vec4(1.4, -9.3, 32.3, 9.1))
// -> Vec4(1.4, -3.4, 42.0, 9.1)
pub fn min(
  a: vec4.Vec4(Float),
  b: vec4.Vec4(Float),
) -> vec4.Vec4(Float)

Compares two vectors, returning the smaller of the two.

Examples

min(Vec4(1.2, -3.4, 42.0, 0.69), Vec4(1.0, 2.1, -5.4, 7.5))
// -> Vec4(1.0, -3.4, -5.4, 0.69)
pub fn mirror(
  vector: vec4.Vec4(Float),
  through normal: vec4.Vec4(Float),
) -> vec4.Vec4(Float)

Returns the mirror of a vector through a plane defined by the given normal vector.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> mirror(Vec4(1.0, 2.1, 3.2, 4.3))
// -> Vec4(-6.5, -19.57, 17.36, -32.42)
pub fn modulo(
  dividend: vec4.Vec4(Float),
  by divisor: vec4.Vec4(Float),
) -> Result(vec4.Vec4(Float), Nil)

Returns the modulo of the inputs as a Result.

Examples

Vec4(13.3, -13.3, 13.3, -13.3) |> modulo(Vec4(3.3, 3.3, -3.3, -3.3))
// -> Ok(Vec4(0.1, 3.2, -3.2, -0.1))
pub fn multiply(
  a: vec4.Vec4(Float),
  b: vec4.Vec4(Float),
) -> vec4.Vec4(Float)

Multiplies two vectors together.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> multiply(Vec4(2.1, -1.0, 0.0, 1.0))
// -> Vec4(2.52, 3.4, 0.0, 0.69)
pub fn negate(vector: vec4.Vec4(Float)) -> vec4.Vec4(Float)

Returns a new vector with all elements negated.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> negate()
// -> Vec4(-1.2, 3.4, -42.0, -0.69)
pub fn normalize(vector: vec4.Vec4(Float)) -> vec4.Vec4(Float)

Normalize the vector.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> normalize()
// -> Vec4(0.03, -0.08, 1.0, 0.02)
pub fn product(
  vectors: List(vec4.Vec4(Float)),
) -> vec4.Vec4(Float)

Multiplies a list of vectors and returns the product.

Examples

[
  Vec4(1.2, -3.4, 42.0, 0.69),
  Vec4(2.1, -1.0, 999.9, 2.0),
  Vec4(3.2, 2.0, 0.0, 0.5),
]
|> product()
// -> Vec4(8.064, 6.8, 0.0, 0.69)
pub fn project(
  a: vec4.Vec4(Float),
  on b: vec4.Vec4(Float),
) -> vec4.Vec4(Float)

Returns the projection of a vector on another vector.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> project(Vec4(1.0, 2.1, 3.2, 4.3))
// -> Vec4(3.85, 8.08, 12.32, 16.55)
pub fn reflect(
  vector: vec4.Vec4(Float),
  through normal: vec4.Vec4(Float),
) -> vec4.Vec4(Float)

Returns the reflection of a vector through a plane defined by the given normal vector.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> reflect(Vec4(1.0, 2.1, 3.2, 4.3))
// -> Vec4(6.5, 19.57, -17.36, 32.42)
pub fn round(vector: vec4.Vec4(Float)) -> vec4.Vec4(Int)

Returns a new vector with all elements rounded to the nearest whole number as an Int.

Examples

Vec4(1.2, -3.6, 42.0, 0.5) |> round()
// -> Vec4(1, -4, 42, 1)
pub fn scale(
  vector: vec4.Vec4(Float),
  by scalar: Float,
) -> vec4.Vec4(Float)

Returns a new vector containing the elements multiplies by scalar.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> scale(2.5)
// -> Vec4(3.0, -8.5, 105.0, 1.72)
pub fn slide(
  a: vec4.Vec4(Float),
  on b: vec4.Vec4(Float),
) -> vec4.Vec4(Float)

Returns a new vector resulting from sliding this vector along a plane defined by the given normal vector.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> slide(Vec4(1.0, 2.1, 3.2, 4.3))
// -> Vec4(-2.65, -11.48, 29.68, -15.86)
pub fn subtract(
  a: vec4.Vec4(Float),
  b: vec4.Vec4(Float),
) -> vec4.Vec4(Float)

Subtracts one vector from another.

Examples

Vec4(1.2, -3.4, 42.0, 0.69) |> subtract(Vec4(0.7, -4.5, 2.0, 1.39))
// -> Vec4(0.5, 1.1, 40.0, -0.7)
pub fn sum(vectors: List(vec4.Vec4(Float))) -> vec4.Vec4(Float)

Sums a list of vectors.

Examples

[
  Vec4(1.2, -3.4, 42.0, 0.69),
  Vec4(2.1, 4.5, -2.0, 9.01),
  Vec4(3.3, 0.0, -20.0, 0.3),
]
|> sum()
// -> Vec4(6.6, 1.1, 20.0, 10.0)
pub fn to_precision(
  vector: vec4.Vec4(Float),
  precision: Int,
) -> vec4.Vec4(Float)

Returns a new vector with all elements converted to a given precision.

Examples

Vec4(2.43434348473, -3.656565, 42.0, 0.5) |> to_precision(2)
// -> Vec4(2.43, -3.66, 42.0, 0.5)
Vec4(547_890.453444, -3.656565, 42.0, 0.5) |> to_precision(-3)
// -> Vec4(548_000.0, 0.0, 0.0, 0.0)
pub fn truncate(vector: vec4.Vec4(Float)) -> vec4.Vec4(Int)

Returns a new vector with all elements truncated as an Int.

Examples

Vec4(1.2323232827383238, -3.656565, 42.0, 0.5) |> truncate()
// -> Vec4(1, -3, 42, 0)
Search Document