vec/vec3f

Types

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

pub type Vec3f =
  vec3.Vec3(Float)

Values

pub fn absolute_value(
  vector: vec3.Vec3(Float),
) -> vec3.Vec3(Float)

Returns a new vector with all elements in absolute values.

Examples

Vec3(1.2, -3.4, 42.0) |> absolute_value()
// -> Vec3(1.2, 3.4, 42.0)
pub fn add(
  a: vec3.Vec3(Float),
  b: vec3.Vec3(Float),
) -> vec3.Vec3(Float)

Adds two vectors together.

Examples

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

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

Examples

Vec3(1.2, -3.4, 42.0)
|> anchor_position(
  Vec3(2.0, 4.0, 0.0),
  rotate(_, Vec3(6.0, 9.0, 1.0), maths.pi() *. 0.25),
)
// -> Vec3(26.08, -18.35, 27.2)
pub fn anchor_rotation(
  vector: vec3.Vec3(Float),
  around axis: vec3.Vec3(Float),
  at angle: Float,
  then fun: fn(vec3.Vec3(Float)) -> vec3.Vec3(Float),
) -> vec3.Vec3(Float)

Return the equivalent of vector |> rotate(axis, float.negate(angle)) |> fun() |> rotate(axis, angle).

Examples

Vec3(1.2, -3.4, 42.0)
|> anchor_rotation(
  Vec3(6.0, 9.0, 1.0),
  maths.pi() *. 0.25,
  add(_, Vec3(2.0, 4.0, 0.0)),
)
// -> Vec3(3.07, 0.63, 42.51)
pub fn angle(a: vec3.Vec3(Float), b: vec3.Vec3(Float)) -> Float

Returns the angle (in radians) between two vectors.

Examples

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

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

Examples

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

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

Examples

Vec3(1.2, -3.4, 42.0) |> clamp(
  min: Vec3(1.0, 2.1, -5.4),
  max: Vec3(1.4, 18.2, 32.3),
)
// -> Vec3(1.2, 2.1, 32.3)
pub fn compare_distance(
  a: vec3.Vec3(Float),
  with b: vec3.Vec3(Float),
  to vector: vec3.Vec3(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(
  Vec3(1.2, -3.4, 42.0),
  Vec3(1.0, 2.1, 3.2),
  Vec3(-2.5, 6.7, 19.4),
)
// -> Gt
pub fn compare_length(
  a: vec3.Vec3(Float),
  with b: vec3.Vec3(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(
  Vec3(1.2, -3.4, 42.0),
  Vec3(1.0, 2.1, 3.2),
)
// -> Gt
pub fn cross(
  a: vec3.Vec3(Float),
  b: vec3.Vec3(Float),
) -> vec3.Vec3(Float)

Returns the cross product of two vectors.

Examples

Vec3(1.2, -3.4, 42.0) |> dot(Vec3(1.0, 2.1, 3.2))
// -> Vec3(-99.08, 38.16, 5.92)
pub fn decoder() -> decode.Decoder(vec3.Vec3(Float))

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

pub fn direction(
  a: vec3.Vec3(Float),
  to b: vec3.Vec3(Float),
) -> vec3.Vec3(Float)

Returns a normalized vector pointing from a to b.

Examples

Vec3(1.2, -3.4, 42.0) |> direction(Vec3(1.0, 2.1, 3.2))
// -> Vec3(-0.01, 0.14, -1.0)
pub fn distance(
  a: vec3.Vec3(Float),
  with b: vec3.Vec3(Float),
) -> Float

Returns the distance between two vectors.

Examples

Vec3(1.2, -3.4, 42.0) |> distance(Vec3(1.0, 2.1, 3.2))
// -> 39.19
pub fn distance_squared(
  a: vec3.Vec3(Float),
  with b: vec3.Vec3(Float),
) -> Float

Returns the squared distance between two vectors.

Examples

Vec3(1.2, -3.4, 42.0) |> distance_squared(Vec3(1.0, 2.1, 3.2))
// -> 1535.73
pub fn divide(
  dividend: vec3.Vec3(Float),
  by divisor: vec3.Vec3(Float),
) -> Result(vec3.Vec3(Float), Nil)

Returns division of the inputs as a Result.

Examples

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

Returns the dot product of two vectors.

Examples

Vec3(1.2, -3.4, 42.0) |> dot(Vec3(1.0, 2.1, 3.2))
// -> 128.46
pub fn floor(vector: vec3.Vec3(Float)) -> vec3.Vec3(Float)

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

Examples

Vec3(1.2, -3.6, 42.0) |> floor()
// -> Vec3(1.0, -4.0, 42.0)
pub fn length(vector: vec3.Vec3(Float)) -> Float

Returns the length (magnitude) of the vector.

Examples

Vec3(1.2, -3.4, 42.0) |> length()
// -> 42.15
pub fn length_squared(vector: vec3.Vec3(Float)) -> Float

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

Examples

Vec3(1.2, -3.4, 42.0) |> length_squared()
// -> 1777.0
pub fn loosely_compare_distance(
  a: vec3.Vec3(Float),
  with b: vec3.Vec3(Float),
  to vector: vec3.Vec3(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(
  Vec3(1.2, -3.4, 42.0),
  Vec3(1.25, -3.43, 42.0001),
  Vec3(-2.5, 6.7, 19.4),
  tolerating: 1.0,
)
// -> Eq
pub fn loosely_compare_length(
  a: vec3.Vec3(Float),
  with b: vec3.Vec3(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(
  Vec3(1.2, -3.4, 42.0),
  Vec3(-1.25, 3.43, -42.0001),
  tolerating: 0.5,
)
// -> Eq
pub fn loosely_equals(
  a: vec3.Vec3(Float),
  with b: vec3.Vec3(Float),
  tolerating tolerance: Float,
) -> Bool

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

Examples

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

Compares two vectors, returning the larger of the two.

Examples

max(Vec3(1.2, -3.4, 42.0), Vec3(1.4, -9.3, 32.3))
// -> Vec3(1.4, -3.4, 42.0)
pub fn min(
  a: vec3.Vec3(Float),
  b: vec3.Vec3(Float),
) -> vec3.Vec3(Float)

Compares two vectors, returning the smaller of the two.

Examples

min(Vec3(1.2, -3.4, 42.0), Vec3(1.0, 2.1, -5.4))
// -> Vec3(1.0, -3.4, -5.4)
pub fn mirror(
  vector: vec3.Vec3(Float),
  through normal: vec3.Vec3(Float),
) -> vec3.Vec3(Float)

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

Examples

Vec3(1.2, -3.4, 42.0) |> mirror(Vec3(1.0, 2.1, 3.2))
// -> Vec3(-15.22, -37.87, -10.53)
pub fn modulo(
  dividend: vec3.Vec3(Float),
  by divisor: vec3.Vec3(Float),
) -> Result(vec3.Vec3(Float), Nil)

Returns the modulo of the inputs as a Result.

Examples

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

Multiplies two vectors together.

Examples

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

Returns a new vector with all elements negated.

Examples

Vec3(1.2, -3.4, 42.0) |> negate()
// -> Vec3(-1.2, 3.4, -42.0)
pub fn normalize(vector: vec3.Vec3(Float)) -> vec3.Vec3(Float)

Normalize the vector.

Examples

Vec3(1.2, -3.4, 42.0) |> normalize()
// -> Vec3(0.03, -0.08, 1.0)
pub fn product(
  vectors: List(vec3.Vec3(Float)),
) -> vec3.Vec3(Float)

Multiplies a list of vectors and returns the product.

Examples

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

Returns the projection of a vector on another vector.

Examples

Vec3(1.2, -3.4, 42.0) |> project(Vec3(1.0, 2.1, 3.2))
// -> Vec3(8.21, 17.24, 26.27)
pub fn reflect(
  vector: vec3.Vec3(Float),
  through normal: vec3.Vec3(Float),
) -> vec3.Vec3(Float)

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

Examples

Vec3(1.2, -3.4, 42.0) |> reflect(Vec3(1.0, 2.1, 3.2))
// -> Vec3(15.22, 37.87, 10.53)
pub fn rotate(
  vector: vec3.Vec3(Float),
  around axis: vec3.Vec3(Float),
  by angle: Float,
) -> vec3.Vec3(Float)

Rotate a vector around a given axis by an angle (in radians).

Examples

Vec3(1.2, -3.4, 42.0)
|> rotate(around: Vec3(1.0, 2.1, 3.2), by: maths.pi() *. 0.25)
// -> Vec3(20.96, -4.18, 36.33)
pub fn round(vector: vec3.Vec3(Float)) -> vec3.Vec3(Int)

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

Examples

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

Returns a new vector containing the elements multiplies by scalar.

Examples

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

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

Examples

Vec3(1.2, -3.4, 42.0) |> slide(Vec3(1.0, 2.1, 3.2))
// -> Vec3(-7.01, -20.64, 15.73)
pub fn subtract(
  a: vec3.Vec3(Float),
  b: vec3.Vec3(Float),
) -> vec3.Vec3(Float)

Subtracts one vector from another.

Examples

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

Sums a list of vectors.

Examples

[
  Vec3(1.2, -3.4, 42.0),
  Vec3(2.1, 4.5, -2.0),
  Vec3(3.3, 0.0, -20.0),
]
|> sum()
// -> Vec3(6.6, 1.1, 20.0)
pub fn to_precision(
  vector: vec3.Vec3(Float),
  precision: Int,
) -> vec3.Vec3(Float)

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

Examples

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

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

Examples

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