vec/vec2i

Types

A 2-element structure that can be used to represent 2D coordinates or any other pair of integer values.

pub type Vec2i =
  vec2.Vec2(Int)

Values

pub fn absolute_value(vector: vec2.Vec2(Int)) -> vec2.Vec2(Int)

Returns a new vector with all elements in absolute values.

Examples

assert 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

assert 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

assert
  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

assert
  Vec2(12, -34)
  |> anchor_rotation(1, add(_, Vec2(6, 9)))
  == Vec2(3, -28)
pub fn clamp(
  vector: vec2.Vec2(Int),
  start_bound: vec2.Vec2(Int),
  stop_bound: vec2.Vec2(Int),
) -> vec2.Vec2(Int)

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

Examples

assert Vec2(12, -34) |> clamp(Vec2(10, 21), Vec2(14, 18)) == Vec2(12, 18)
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

assert 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

assert 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

assert Vec2(12, -34) |> cross(Vec2(2, 3)) == 104
pub fn distance(
  a: vec2.Vec2(Int),
  with b: vec2.Vec2(Int),
) -> Float

Returns the distance between two vectors.

Examples

assert 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

assert 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

assert Vec2(12, -34) |> divide(Vec2(2, 5)) == Ok(Vec2(6, -6))
assert 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

assert 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

assert Vec2(12, -34) |> floor_divide(Vec2(2, 5)) == Ok(Vec2(6, -7))
assert 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

assert 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

assert 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

assert 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

assert 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

assert 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

assert Vec2(13, 13) |> modulo(Vec2(3, -3)) == Ok(Vec2(1, -2))
assert 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

assert 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

assert Vec2(12, -34) |> negate == Vec2(-12, 34)
pub const negative_x: vec2.Vec2(Int)

Negative X axis unit-vector, a vector with the x component set to -1 and y component set to 0.

pub const negative_y: vec2.Vec2(Int)

Negative Y axis unit-vector, a vector with the y component set to -1 and x component set to 0.

pub const one: vec2.Vec2(Int)

One vector, a vector with all components set to 1.

pub const positive_x: vec2.Vec2(Int)

X axis unit-vector, a vector with the x component set to 1 and y component set to 0.

pub const positive_y: vec2.Vec2(Int)

Y axis unit-vector, a vector with the y component set to 1 and x component set to 0.

pub fn product(vectors: List(vec2.Vec2(Int))) -> vec2.Vec2(Int)

Multiplies a list of vectors and returns the product.

Examples

assert
  [
    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

assert Vec2(12, -34) |> project(Vec2(2, 3)) == Vec2(-12, -18)
pub fn range(
  from start: vec2.Vec2(Int),
  to stop: vec2.Vec2(Int),
  with acc: acc,
  run reducer: fn(acc, vec2.Vec2(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: vec2.splat(3), with: dict.new(), run: fn(acc, v) {
  acc |> dict.insert(v, v.x + v.y)
})
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

assert 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

assert Vec2(13, -13) |> remainder(Vec2(3, 3)) == Ok(Vec2(1, -1))
assert 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

assert 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

assert 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

assert 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

assert Vec2(12, -34) |> subtract(Vec2(7, -45)) == Vec2(5, 11)
pub fn sum(vectors: List(vec2.Vec2(Int))) -> vec2.Vec2(Int)

Sums a list of vectors.

Examples

assert
  [
    Vec2(12, -34),
    Vec2(21, 45),
    Vec2(33, 0),
  ]
  |> sum
  == Vec2(66, 11)
pub fn to_vec2f(vector: vec2.Vec2(Int)) -> vec2.Vec2(Float)

Takes an int vector and returns its value as a float vector.

Examples

assert Vec2(12, -34) |> to_vec2f == Vec2(12.0, -34.0)
pub const zero: vec2.Vec2(Int)

Zero vector, a vector with all components set to 0.

Search Document