lib.bend source
lib.bend on the hub · documented module
# Vec2 — foundational two-dimensional vectors over Base F32.## Coordinates are F32. Constructors intentionally do not validate or normalize# values. This layer avoids sqrt, trig, and all non-Base dependencies.import Basetype Vec2 is Data: Vec2{x: F32, y: F32}# Constructors and axes.def Vec2.make(x: F32, y: F32) -> Vec2: Vec2{x, y}def Vec2.zero() -> Vec2: Vec2{0.0, 0.0}def Vec2.unit_x() -> Vec2: Vec2{1.0, 0.0}def Vec2.unit_y() -> Vec2: Vec2{0.0, 1.0}# Field projections.def Vec2.x(v: Vec2) -> F32: match v: case Vec2{x, y}: xdef Vec2.y(v: Vec2) -> F32: match v: case Vec2{x, y}: y# Component-wise arithmetic.def Vec2.add(a: Vec2, b: Vec2) -> Vec2: match a b: case Vec2{ax, ay} Vec2{bx, by}: Vec2{F32.add(ax, bx), F32.add(ay, by)}def Vec2.sub(a: Vec2, b: Vec2) -> Vec2: match a b: case Vec2{ax, ay} Vec2{bx, by}: Vec2{F32.sub(ax, bx), F32.sub(ay, by)}def Vec2.scale(v: Vec2, +s: F32) -> Vec2: match v: case Vec2{x, y}: Vec2{F32.mul(x, s), F32.mul(y, s)}# Dot product and squared length. length_sq deliberately avoids sqrt.def Vec2.dot(a: Vec2, b: Vec2) -> F32: match a b: case Vec2{ax, ay} Vec2{bx, by}: F32.add(F32.mul(ax, bx), F32.mul(ay, by))def Vec2.length_sq(+v: Vec2) -> F32: Vec2.dot(v, v)# Affine interpolation: a + (b - a) * t.def Vec2.lerp(+a: Vec2, b: Vec2, t: F32) -> Vec2: Vec2.add(a, Vec2.scale(Vec2.sub(b, a), t))