vec/vec2f
Types
Values
pub fn absolute_value(
vector: vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Returns a new vector with all elements in absolute values.
Examples
Vec2(1.2, -3.4) |> absolute_value()
// -> Vec2(1.2, 3.4)
pub fn add(
a: vec2.Vec2(Float),
b: vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Adds two vectors together.
Examples
Vec2(1.2, -3.4) |> add(Vec2(2.1, 4.5))
// -> Vec2(3.3, 1.1)
pub fn anchor_position(
vector: vec2.Vec2(Float),
at position: vec2.Vec2(Float),
then fun: fn(vec2.Vec2(Float)) -> vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Return the equivalent of vector |> subtract(position) |> fun() |> add(position)
.
Examples
Vec2(1.2, -3.4)
|> anchor_position(
Vec2(1.0, 2.1),
rotate(_, maths.pi() *. 0.25),
)
// -> Vec2(5.03, -1.65)
pub fn anchor_rotation(
vector: vec2.Vec2(Float),
at angle: Float,
then fun: fn(vec2.Vec2(Float)) -> vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Return the equivalent of vector |> rotate(float.negate(angle)) |> fun() |> rotate(angle)
.
Examples
Vec2(1.2, -3.4)
|> anchor_rotation(
maths.pi() *. 0.25,
add(_, Vec2(6.0, 9.0)),
)
// -> Vec2(-0.92, 7.21)
pub fn angle(a: vec2.Vec2(Float), b: vec2.Vec2(Float)) -> Float
Returns the angle (in radians) between two vectors.
Examples
Vec2(1.2, -3.4) |> angle(Vec2(1.0, 2.1))
// -> 2.36
pub fn ceiling(vector: vec2.Vec2(Float)) -> vec2.Vec2(Float)
Returns a new vector with all elements rounded to the next highest whole
number as a Float
.
Examples
Vec2(1.2, -3.6) |> ceiling()
// -> Vec2(2.0, -3.0)
pub fn clamp(
vector: vec2.Vec2(Float),
min min_bound: vec2.Vec2(Float),
max max_bound: vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Returns a new vector with all components clamped between a lower and upper bound.
Examples
Vec2(1.2, -3.4) |> clamp(
min: Vec2(1.0, 2.1),
max: Vec2(1.4, 18.2),
)
// -> Vec2(1.2, 2.1)
pub fn compare_distance(
a: vec2.Vec2(Float),
with b: vec2.Vec2(Float),
to vector: vec2.Vec2(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(Vec2(1.2, -3.4), Vec2(1.0, 2.1), Vec2(-2.5, 6.7))
// -> Gt
pub fn compare_length(
a: vec2.Vec2(Float),
with b: vec2.Vec2(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(Vec2(1.2, -3.4), Vec2(1.0, 2.1))
// -> Gt
pub fn cross(a: vec2.Vec2(Float), b: vec2.Vec2(Float)) -> Float
Returns the cross product of two vectors.
Examples
Vec2(1.2, -3.4) |> cross(Vec2(1.0, 2.1))
// -> 5.92
pub fn direction(
a: vec2.Vec2(Float),
to b: vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Returns a normalized vector pointing from a
to b
.
Examples
Vec2(1.2, -3.4) |> direction(Vec2(1.0, 2.1))
// -> Vec2(-0.04, 1.0)
pub fn distance(
a: vec2.Vec2(Float),
with b: vec2.Vec2(Float),
) -> Float
Returns the distance between two vectors.
Examples
Vec2(1.2, -3.4) |> distance(Vec2(1.0, 2.1))
// -> 5.5
pub fn distance_squared(
a: vec2.Vec2(Float),
with b: vec2.Vec2(Float),
) -> Float
Returns the squared distance between two vectors.
Examples
Vec2(1.2, -3.4) |> distance_squared(Vec2(1.0, 2.1))
// -> 30.29
pub fn divide(
dividend: vec2.Vec2(Float),
by divisor: vec2.Vec2(Float),
) -> Result(vec2.Vec2(Float), Nil)
Returns division of the inputs as a Result
.
Examples
Vec2(1.2, -3.4) |> divide(Vec2(2.0, 0.5))
// -> Ok(Vec2(0.6, -6.8))
Vec2(1.2, -3.4) |> divide(Vec2(0.0, 0.5))
// -> Error(Nil)
pub fn dot(a: vec2.Vec2(Float), b: vec2.Vec2(Float)) -> Float
Returns the dot product of two vectors.
Examples
Vec2(1.2, -3.4) |> dot(Vec2(1.0, 2.1))
// -> -5.94
pub fn floor(vector: vec2.Vec2(Float)) -> vec2.Vec2(Float)
Returns a new vector with all elements rounded to the next lowest whole
number as an Float
.
Examples
Vec2(1.2, -3.6) |> floor()
// -> Vec2(1.0, -4.0)
pub fn length(vector: vec2.Vec2(Float)) -> Float
Returns the length (magnitude) of the vector.
Examples
Vec2(1.2, -3.4) |> length()
// -> 3.61
pub fn length_squared(vector: vec2.Vec2(Float)) -> Float
Returns the squared length (squared magnitude) of the vector.
Examples
Vec2(1.2, -3.4) |> length_squared()
// -> 13.0
pub fn loosely_compare_distance(
a: vec2.Vec2(Float),
with b: vec2.Vec2(Float),
to vector: vec2.Vec2(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(
Vec2(1.2, -3.4),
Vec2(1.25, -3.43),
Vec2(-2.5, 6.7),
tolerating: 1.0,
)
// -> Eq
pub fn loosely_compare_length(
a: vec2.Vec2(Float),
with b: vec2.Vec2(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(Vec2(1.2, -3.4), Vec2(-1.25, 3.43), tolerating: 0.5)
// -> Eq
pub fn loosely_equals(
a: vec2.Vec2(Float),
with b: vec2.Vec2(Float),
tolerating tolerance: Float,
) -> Bool
Checks for equality of two vectors within a tolerance, returning an Bool
.
Examples
Vec2(1.2, -3.4)
|> loosely_equals(Vec2(1.25, -3.43), tolerating: 0.1)
// -> True
pub fn max(
a: vec2.Vec2(Float),
b: vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Compares two vectors, returning the larger of the two.
Examples
max(Vec2(1.2, -3.4), Vec2(1.4, -9.3))
// -> Vec2(1.4, -3.4)
pub fn min(
a: vec2.Vec2(Float),
b: vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Compares two vectors, returning the smaller of the two.
Examples
min(Vec2(1.2, -3.4), Vec2(1.0, 2.1))
// -> Vec2(1.0, -3.4)
pub fn mirror(
vector: vec2.Vec2(Float),
through normal: vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Returns the mirror of a vector through a plane defined by the given normal vector.
Examples
Vec2(1.2, -3.4) |> mirror(Vec2(1.0, 2.1))
// -> Vec2(3.34, 1.21)
pub fn modulo(
dividend: vec2.Vec2(Float),
by divisor: vec2.Vec2(Float),
) -> Result(vec2.Vec2(Float), Nil)
Returns the modulo of the inputs as a Result
.
Examples
Vec2(13.3, 13.3) |> modulo(Vec2(3.3, -3.3))
// -> Ok(Vec2(0.1, -3.2))
Vec2(-13.3, -13.3) |> modulo(Vec2(3.3, -3.3))
// -> Ok(Vec2(3.2, -0.1))
pub fn multiply(
a: vec2.Vec2(Float),
b: vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Multiplies two vectors together.
Examples
Vec2(1.2, -3.4) |> multiply(Vec2(2.1, -1.0))
// -> Vec2(2.52, 3.4)
pub fn negate(vector: vec2.Vec2(Float)) -> vec2.Vec2(Float)
Returns a new vector with all elements negated.
Examples
Vec2(1.2, -3.4) |> negate()
// -> Vec2(-1.2, 3.4)
pub fn normalize(vector: vec2.Vec2(Float)) -> vec2.Vec2(Float)
Normalize the vector.
Examples
Vec2(1.2, -3.4) |> normalize()
// -> Vec2(0.33, -0.94)
pub fn product(
vectors: List(vec2.Vec2(Float)),
) -> vec2.Vec2(Float)
Multiplies a list of vectors and returns the product.
Examples
[
Vec2(1.2, -3.4),
Vec2(2.1, -1.0),
Vec2(3.2, 2.0),
]
|> product()
// -> Vec2(8.064, 6.8)
pub fn project(
a: vec2.Vec2(Float),
on b: vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Returns the projection of a vector on another vector.
Examples
Vec2(1.2, -3.4) |> project(Vec2(1.0, 2.1))
// -> Vec2(-1.1, -2.31)
pub fn reflect(
vector: vec2.Vec2(Float),
through normal: vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Returns the reflection of a vector through a plane defined by the given normal vector.
Examples
Vec2(1.2, -3.4) |> reflect(Vec2(1.0, 2.1))
// -> Vec2(-3.4, -1.21)
pub fn rotate(
vector: vec2.Vec2(Float),
by angle: Float,
) -> vec2.Vec2(Float)
Rotate a vector by an angle (in radians).
Examples
Vec2(1.2, -3.4) |> rotate(maths.pi() *. 0.25)
// -> Vec2(3.25, -1.56)
pub fn round(vector: vec2.Vec2(Float)) -> vec2.Vec2(Int)
Returns a new vector with all elements rounded to the nearest whole number
as an Int
.
Examples
Vec2(1.2, -3.6) |> round()
// -> Vec2(1, -4)
pub fn scale(
vector: vec2.Vec2(Float),
by scalar: Float,
) -> vec2.Vec2(Float)
Returns a new vector containing the elements multiplies by scalar
.
Examples
Vec2(1.2, -3.4) |> scale(2.5)
// -> Vec2(3.0, -8.5)
pub fn slide(
a: vec2.Vec2(Float),
on b: vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Returns a new vector resulting from sliding this vector along a plane defined by the given normal vector.
Examples
Vec2(1.2, -3.4) |> slide(Vec2(1.0, 2.1))
// -> Vec2(2.3, -1.09)
pub fn subtract(
a: vec2.Vec2(Float),
b: vec2.Vec2(Float),
) -> vec2.Vec2(Float)
Subtracts one vector from another.
Examples
Vec2(1.2, -3.4) |> subtract(Vec2(0.7, -4.5))
// -> Vec2(0.5, 1.1)
pub fn sum(vectors: List(vec2.Vec2(Float))) -> vec2.Vec2(Float)
Sums a list of vectors.
Examples
[
Vec2(1.2, -3.4),
Vec2(2.1, 4.5),
Vec2(3.3, 0.0),
]
|> sum()
// -> Vec2(6.6, 1.1)
pub fn to_precision(
vector: vec2.Vec2(Float),
precision: Int,
) -> vec2.Vec2(Float)
Returns a new vector with all elements converted to a given precision.
Examples
Vec2(2.43434348473, -3.656565) |> to_precision(2)
// -> Vec2(2.43, -3.66)
Vec2(547_890.453444, -3.656565) |> to_precision(-3)
// -> Vec2(548_000.0, 0.0)