vec/vec2i
Types
Values
pub fn absolute_value(vector: vec2.Vec2(Int)) -> vec2.Vec2(Int)
Returns a new vector with all elements in absolute values.
Examples
Vec2(12, -34) |> absolute_value()
// -> Vec2(12, 34)
pub fn add(
a: vec2.Vec2(Int),
b: vec2.Vec2(Int),
) -> vec2.Vec2(Int)
Adds two vectors together.
Examples
Vec2(12, -34) |> add(Vec2(21, 45))
// -> Vec2(33, 11)
pub fn anchor_position(
vector: vec2.Vec2(Int),
at position: vec2.Vec2(Int),
then fun: fn(vec2.Vec2(Int)) -> vec2.Vec2(Int),
) -> vec2.Vec2(Int)
Return the equivalent of vector |> subtract(position) |> fun() |> add(position)
.
Examples
Vec2(12, -34)
|> anchor_position(Vec2(10, 21), rotate(_, 1))
// -> Vec2(65, 23)
pub fn anchor_rotation(
vector: vec2.Vec2(Int),
at angle: Int,
then fun: fn(vec2.Vec2(Int)) -> vec2.Vec2(Int),
) -> vec2.Vec2(Int)
Return the equivalent of vector |> rotate(-angle) |> fun() |> rotate(angle)
.
Examples
Vec2(12, -34)
|> anchor_rotation(1, add(_, Vec2(6, 9)))
// -> Vec2(3, -28)
pub fn clamp(
vector: vec2.Vec2(Int),
min min_bound: vec2.Vec2(Int),
max max_bound: vec2.Vec2(Int),
) -> vec2.Vec2(Int)
Returns a new vector with all components clamped between a lower and upper bound.
Examples
Vec2(12, -34) |> clamp(
min: Vec2(10, 21),
max: Vec2(14, 18),
)
// -> Vec2(12, 21)
pub fn compare_distance(
a: vec2.Vec2(Int),
with b: vec2.Vec2(Int),
to vector: vec2.Vec2(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
compare_distance(Vec2(12, -34), Vec2(2, 3), Vec2(-25, 67))
// -> Gt
pub fn compare_length(
a: vec2.Vec2(Int),
with b: vec2.Vec2(Int),
) -> 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(Vec2(12, -34), Vec2(2, 3))
// -> Gt
pub fn cross(a: vec2.Vec2(Int), b: vec2.Vec2(Int)) -> Int
Returns the cross product of two vectors.
Examples
Vec2(12, -34) |> cross(Vec2(2, 3))
// -> 104
pub fn decoder() -> decode.Decoder(vec2.Vec2(Int))
A decoder that decodes 2D int vectors.
pub fn distance(
a: vec2.Vec2(Int),
with b: vec2.Vec2(Int),
) -> Float
Returns the distance between two vectors.
Examples
Vec2(12, -34) |> distance(Vec2(2, 3))
// -> 38.33
pub fn distance_squared(
a: vec2.Vec2(Int),
with b: vec2.Vec2(Int),
) -> Int
Returns the squared distance between two vectors.
Examples
Vec2(12, -34) |> distance_squared(Vec2(2, 3))
// -> 1469
pub fn divide(
dividend: vec2.Vec2(Int),
by divisor: vec2.Vec2(Int),
) -> Result(vec2.Vec2(Int), Nil)
Returns division of the inputs as a Result
.
Examples
Vec2(12, -34) |> divide(Vec2(2, 5))
// -> Ok(Vec2(6, -6))
Vec2(12, -34) |> divide(Vec2(0, 5))
// -> Error(Nil)
pub fn dot(a: vec2.Vec2(Int), b: vec2.Vec2(Int)) -> Int
Returns the dot product of two vectors.
Examples
Vec2(12, -34) |> dot(Vec2(2, 3))
// -> -78
pub fn floor_divide(
dividend: vec2.Vec2(Int),
by divisor: vec2.Vec2(Int),
) -> Result(vec2.Vec2(Int), Nil)
Performs a floored integer vector division, which means that the result will always be rounded towards negative infinity.
Examples
Vec2(12, -34) |> floor_divide(Vec2(2, 5))
// -> Ok(Vec2(6, -7))
Vec2(12, -34) |> floor_divide(Vec2(0, 5))
// -> Error(Nil)
pub fn length(vector: vec2.Vec2(Int)) -> Float
Returns the length (magnitude) of the vector.
Examples
Vec2(12, -34) |> length()
// -> 36.06
pub fn length_squared(vector: vec2.Vec2(Int)) -> Int
Returns the squared length (squared magnitude) of the vector.
Examples
Vec2(12, -34) |> length_squared()
// -> 1300
pub fn max(
a: vec2.Vec2(Int),
b: vec2.Vec2(Int),
) -> vec2.Vec2(Int)
Compares two vectors, returning the larger of the two.
Examples
max(Vec2(12, -34), Vec2(14, -93))
// -> Vec2(14, -34)
pub fn min(
a: vec2.Vec2(Int),
b: vec2.Vec2(Int),
) -> vec2.Vec2(Int)
Compares two vectors, returning the smaller of the two.
Examples
min(Vec2(12, -34), Vec2(10, 21))
// -> Vec2(10, -34)
pub fn mirror(
vector: vec2.Vec2(Int),
through normal: vec2.Vec2(Int),
) -> vec2.Vec2(Int)
Returns the mirror of a vector through a plane defined by the given normal vector.
Examples
Vec2(12, -34) |> mirror(Vec2(2, 3))
// -> Vec2(36, 2)
pub fn modulo(
dividend: vec2.Vec2(Int),
by divisor: vec2.Vec2(Int),
) -> Result(vec2.Vec2(Int), Nil)
Returns the modulo of the inputs as a Result
.
Examples
Vec2(13, 13) |> modulo(Vec2(3, -3))
// -> Ok(Vec2(1, -2))
Vec2(-13, -13) |> modulo(Vec2(3, -3))
// -> Ok(Vec2(2, -1))
pub fn multiply(
a: vec2.Vec2(Int),
b: vec2.Vec2(Int),
) -> vec2.Vec2(Int)
Multiplies two vectors together.
Examples
Vec2(12, -34) |> multiply(Vec2(2, -3))
// -> Vec2(24, 102)
pub fn negate(vector: vec2.Vec2(Int)) -> vec2.Vec2(Int)
Returns a new vector with all elements negated.
Examples
Vec2(12, -34) |> negate()
// -> Vec2(-12, 34)
pub fn product(vectors: List(vec2.Vec2(Int))) -> vec2.Vec2(Int)
Multiplies a list of vectors and returns the product.
Examples
[
Vec2(12, -34),
Vec2(21, -10),
Vec2(32, 20),
]
|> product()
// -> Vec2(8064, 6800)
pub fn project(
a: vec2.Vec2(Int),
on b: vec2.Vec2(Int),
) -> vec2.Vec2(Int)
Returns the projection of a vector on another vector.
Examples
Vec2(12, -34) |> project(Vec2(2, 3))
// -> Vec2(-12, -18)
pub fn reflect(
vector: vec2.Vec2(Int),
through normal: vec2.Vec2(Int),
) -> vec2.Vec2(Int)
Returns the reflection of a vector through a plane defined by the given normal vector.
Examples
Vec2(12, -34) |> reflect(Vec2(2, 3))
// -> Vec2(-36, -2)
pub fn remainder(
dividend: vec2.Vec2(Int),
by divisor: vec2.Vec2(Int),
) -> Result(vec2.Vec2(Int), Nil)
Computes the remainder of an integer vector division of inputs as a
Result
.
Examples
Vec2(13, -13) |> remainder(Vec2(3, 3))
// -> Ok(Vec2(1, -1))
Vec2(12, -34) |> remainder(Vec2(0, 1))
// -> Error(Nil)
pub fn rotate(
vector: vec2.Vec2(Int),
by angle: Int,
) -> vec2.Vec2(Int)
Rotate a vector by an angle (in 90 degree steps).
Examples
Vec2(12, -34) |> rotate(1)
// -> Vec2(34, 12)
pub fn scale(
vector: vec2.Vec2(Int),
by scalar: Int,
) -> vec2.Vec2(Int)
Returns a new vector containing the elements multiplies by scalar
.
Examples
Vec2(12, -34) |> scale(2)
// -> Vec2(24, -68)
pub fn slide(
a: vec2.Vec2(Int),
on b: vec2.Vec2(Int),
) -> vec2.Vec2(Int)
Returns a new vector resulting from sliding this vector along a plane defined by the given normal vector.
Examples
Vec2(12, -34) |> slide(Vec2(2, 3))
// -> -16
pub fn subtract(
a: vec2.Vec2(Int),
b: vec2.Vec2(Int),
) -> vec2.Vec2(Int)
Subtracts one vector from another.
Examples
Vec2(12, -34) |> subtract(Vec2(7, -45))
// -> Vec2(5, 11)