vec/vec4i
Types
Values
pub fn absolute_value(vector: vec4.Vec4(Int)) -> vec4.Vec4(Int)
Returns a new vector with all elements in absolute values.
Examples
assert Vec4(12, -34, 420, 69) |> absolute_value == Vec4(12, 34, 420, 69)
pub fn add(
a: vec4.Vec4(Int),
b: vec4.Vec4(Int),
) -> vec4.Vec4(Int)
Adds two vectors together.
Examples
assert
Vec4(12, -34, 420, 69)
|> add(Vec4(21, 45, -20, -9))
== Vec4(33, 11, 400, 60)
pub fn anchor_position(
vector: vec4.Vec4(Int),
at position: vec4.Vec4(Int),
then fun: fn(vec4.Vec4(Int)) -> vec4.Vec4(Int),
) -> vec4.Vec4(Int)
Return the equivalent of vector |> subtract(position) |> fun |> add(position).
Examples
assert
Vec4(12, -34, 420, 69)
|> anchor_position(Vec4(2, 3, 4, 5), scale(_, 2))
== Vec4(22, -71, 836, 133)
pub fn clamp(
vector: vec4.Vec4(Int),
start_bound: vec4.Vec4(Int),
stop_bound: vec4.Vec4(Int),
) -> vec4.Vec4(Int)
Returns a new vector with all components clamped between a lower and upper bound.
Examples
assert
Vec4(12, -34, 420, 69)
|> clamp(
Vec4(10, 21, -54, 75),
Vec4(14, 18, 323, 91),
)
== Vec4(12, 18, 323, 75)
pub fn compare_distance(
a: vec4.Vec4(Int),
with b: vec4.Vec4(Int),
to vector: vec4.Vec4(Int),
) -> 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
assert
compare_distance(
Vec4(12, -34, 420, 69),
Vec4(2, 3, 4, 5),
Vec4(-25, 67, 194, 0),
)
== Gt
pub fn compare_length(
a: vec4.Vec4(Int),
with b: vec4.Vec4(Int),
) -> order.Order
Compares two vector’s lengths, returning an Order:
Lt for lower than, Eq for equals, or Gt for greater than.
Examples
assert compare_length(Vec4(12, -34, 420, 69), Vec4(2, 3, 4, 5)) == Gt
pub fn distance(
a: vec4.Vec4(Int),
with b: vec4.Vec4(Int),
) -> Float
Returns the distance between two vectors.
Examples
assert Vec4(12, -34, 420, 69) |> distance(Vec4(2, 3, 4, 5)) == 422.64
pub fn distance_squared(
a: vec4.Vec4(Int),
with b: vec4.Vec4(Int),
) -> Int
Returns the squared distance between two vectors.
Examples
assert
Vec4(12, -34, 420, 69)
|> distance_squared(Vec4(2, 3, 4, 5))
== 178_621
pub fn divide(
dividend: vec4.Vec4(Int),
by divisor: vec4.Vec4(Int),
) -> Result(vec4.Vec4(Int), Nil)
Returns division of the inputs as a Result.
Examples
assert
Vec4(12, -34, 420, 69)
|> divide(Vec4(2, 5, 4, 1))
== Ok(Vec4(6, -6, 105, 69))
assert Vec4(12, -34, 420, 69) |> divide(Vec4(0, 5, 4, 1)) == Error(Nil)
pub fn dot(a: vec4.Vec4(Int), b: vec4.Vec4(Int)) -> Int
Returns the dot product of two vectors.
Examples
assert Vec4(12, -34, 420, 69) |> dot(Vec4(2, 3, 4, 5)) == 1947
pub fn floor_divide(
dividend: vec4.Vec4(Int),
by divisor: vec4.Vec4(Int),
) -> Result(vec4.Vec4(Int), Nil)
Performs a floored integer vector division, which means that the result will always be rounded towards negative infinity.
Examples
assert
Vec4(12, -34, 420, 69)
|> floor_divide(Vec4(2, 5, 4, 1))
== Ok(Vec4(6, -7, 105, 69))
assert
Vec4(12, -34, 420, 69)
|> floor_divide(Vec4(0, 5, 4, 1))
== Error(Nil)
pub fn length(vector: vec4.Vec4(Int)) -> Float
Returns the length (magnitude) of the vector.
Examples
assert Vec4(12, -34, 420, 69) |> length == 427.15
pub fn length_squared(vector: vec4.Vec4(Int)) -> Int
Returns the squared length (squared magnitude) of the vector.
Examples
assert Vec4(12, -34, 420, 69) |> length_squared == 182_461
pub fn max(
a: vec4.Vec4(Int),
b: vec4.Vec4(Int),
) -> vec4.Vec4(Int)
Compares two vectors, returning the larger of the two.
Examples
assert
max(Vec4(12, -34, 420, 69), Vec4(14, -93, 323, 91))
== Vec4(14, -34, 420, 91)
pub fn min(
a: vec4.Vec4(Int),
b: vec4.Vec4(Int),
) -> vec4.Vec4(Int)
Compares two vectors, returning the smaller of the two.
Examples
assert
min(Vec4(12, -34, 420, 69), Vec4(10, 21, -54, 75))
== Vec4(10, -34, -54, 69)
pub fn mirror(
vector: vec4.Vec4(Int),
through normal: vec4.Vec4(Int),
) -> vec4.Vec4(Int)
Returns the mirror of a vector through a plane defined by the given normal vector.
Examples
assert
Vec4(12, -34, 420, 69)
|> mirror(Vec4(2, 3, 4, 5))
== Vec4(-132, -250, 132, -291)
pub fn modulo(
dividend: vec4.Vec4(Int),
by divisor: vec4.Vec4(Int),
) -> Result(vec4.Vec4(Int), Nil)
Returns the modulo of the inputs as a Result.
Examples
assert
Vec4(13, -13, 13, -13)
|> modulo(Vec4(3, 3, -3, -3))
== Ok(Vec4(1, 2, -2, -1))
pub fn multiply(
a: vec4.Vec4(Int),
b: vec4.Vec4(Int),
) -> vec4.Vec4(Int)
Multiplies two vectors together.
Examples
assert
Vec4(12, -34, 420, 69)
|> multiply(Vec4(2, -3, 0, 1))
== Vec4(24, 102, 0, 69)
pub fn negate(vector: vec4.Vec4(Int)) -> vec4.Vec4(Int)
Returns a new vector with all elements negated.
Examples
assert Vec4(12, -34, 420, 69) |> negate == Vec4(-12, 34, -420, -69)
pub const negative_w: vec4.Vec4(Int)
Negative W axis unit-vector, a vector with the w component set to -1
and all other components set to 0.
pub const negative_x: vec4.Vec4(Int)
Negative X axis unit-vector, a vector with the x component set to -1
and all other components set to 0.
pub const negative_y: vec4.Vec4(Int)
Negative Y axis unit-vector, a vector with the y component set to -1
and all other components set to 0.
pub const negative_z: vec4.Vec4(Int)
Negative Z axis unit-vector, a vector with the z component set to -1
and all other components set to 0.
pub const positive_w: vec4.Vec4(Int)
W axis unit-vector, a vector with the w component set to 1 and all
other components set to 0.
pub const positive_x: vec4.Vec4(Int)
X axis unit-vector, a vector with the x component set to 1 and all
other components set to 0.
pub const positive_y: vec4.Vec4(Int)
Y axis unit-vector, a vector with the y component set to 1 and all
other components set to 0.
pub const positive_z: vec4.Vec4(Int)
Z axis unit-vector, a vector with the z component set to 1 and all
other components set to 0.
pub fn product(vectors: List(vec4.Vec4(Int))) -> vec4.Vec4(Int)
Multiplies a list of vectors and returns the product.
Examples
assert
[
Vec4(12, -34, 420, 69),
Vec4(21, -10, 999, 20),
Vec4(32, 20, 0, 5),
]
|> product
== Vec4(8064, 6800, 0, 6900)
pub fn project(
a: vec4.Vec4(Int),
on b: vec4.Vec4(Int),
) -> vec4.Vec4(Int)
Returns the projection of a vector on another vector.
Examples
assert
Vec4(12, -34, 420, 69)
|> project(Vec4(2, 3, 4, 5))
== Vec4(72, 108, 144, 180)
pub fn range(
from start: vec4.Vec4(Int),
to stop: vec4.Vec4(Int),
with acc: acc,
run reducer: fn(acc, vec4.Vec4(Int)) -> acc,
) -> acc
Run a function for each vector between vectors from and to.
from is inclusive, and to is exclusive.
Examples
range(from: zero, to: vec4.splat(3), with: dict.new(), run: fn(acc, v) {
acc |> dict.insert(v, v.x + v.y + v.z + v.w)
})
pub fn reflect(
vector: vec4.Vec4(Int),
through normal: vec4.Vec4(Int),
) -> vec4.Vec4(Int)
Returns the reflection of a vector through a plane defined by the given normal vector.
Examples
assert
Vec4(12, -34, 420, 69)
|> reflect(Vec4(2, 3, 4, 5))
== Vec4(132, 250, -132, 291)
pub fn remainder(
dividend: vec4.Vec4(Int),
by divisor: vec4.Vec4(Int),
) -> Result(vec4.Vec4(Int), Nil)
Computes the remainder of an integer vector division of inputs as a
Result.
Examples
assert
Vec4(13, -13, 13, -13)
|> remainder(Vec4(3, 3, -3, -3))
== Ok(Vec4(1, -1, 1, -1))
assert Vec4(12, -34, 420, 69) |> remainder(Vec4(0, 1, 2, 3)) == Error(Nil)
pub fn scale(
vector: vec4.Vec4(Int),
by scalar: Int,
) -> vec4.Vec4(Int)
Returns a new vector containing the elements multiplies by scalar.
Examples
assert Vec4(12, -34, 420, 69) |> scale(2) == Vec4(24, -68, 840, 138)
pub fn slide(
a: vec4.Vec4(Int),
on b: vec4.Vec4(Int),
) -> vec4.Vec4(Int)
Returns a new vector resulting from sliding this vector along a plane defined by the given normal vector.
Examples
assert
Vec4(12, -34, 420, 69)
|> slide(Vec4(2, 3, 4, 5))
== Vec4(-60, -142, 276, -111)
pub fn subtract(
a: vec4.Vec4(Int),
b: vec4.Vec4(Int),
) -> vec4.Vec4(Int)
Subtracts one vector from another.
Examples
assert
Vec4(12, -34, 420, 69)
|> subtract(Vec4(7, -45, 20, 32))
== Vec4(5, 11, 400, 37)
pub fn sum(vectors: List(vec4.Vec4(Int))) -> vec4.Vec4(Int)
Sums a list of vectors.
Examples
assert
[
Vec4(12, -34, 420, 69),
Vec4(21, 45, -20, 9),
Vec4(33, 0, -200, 3),
]
|> sum
== Vec4(66, 11, 200, 81)
pub fn to_string(
vector: vec4.Vec4(Int),
with separator: String,
) -> String
Prints a given vector to a string with a given separator.
Examples
assert Vec4(12, -34, 420, 69) |> to_string(" ") == "12 -34 420 69"