viva_math/vec2

2-D vector operations.

Companion to viva_math/vector (which is dedicated to PAD/Vec3). Useful for 2-D dynamics, planar geometry, polar coordinates, etc.

Types

pub type Vec2 {
  Vec2(x: Float, y: Float)
}

Constructors

  • Vec2(x: Float, y: Float)

Values

pub fn add(a: Vec2, b: Vec2) -> Vec2
pub fn angle(v: Vec2) -> Float

Angle in radians of v relative to the positive x-axis. Range (-π, π].

pub fn clamp(v: Vec2, lo: Float, hi: Float) -> Vec2
pub fn cross(a: Vec2, b: Vec2) -> Float

2-D “cross” returns the z-component of the 3-D cross product.

pub fn distance(a: Vec2, b: Vec2) -> Float
pub fn dot(a: Vec2, b: Vec2) -> Float
pub fn from_polar(r: Float, theta: Float) -> Vec2
pub fn is_close(a: Vec2, b: Vec2, tol: Float) -> Bool

Component-wise approximate equality.

pub fn length(v: Vec2) -> Float

Length √(x² + y²) — uses hypot to avoid overflow/underflow at extreme scales.

pub fn length_squared(v: Vec2) -> Float
pub fn lerp(a: Vec2, b: Vec2, t: Float) -> Vec2
pub fn negate(v: Vec2) -> Vec2
pub fn normalize(v: Vec2) -> Vec2
pub fn perpendicular(v: Vec2) -> Vec2

Perpendicular vector (90° counter-clockwise rotation).

pub fn rotate(v: Vec2, theta: Float) -> Vec2

Rotate v by theta radians around the origin.

pub fn scale(v: Vec2, s: Float) -> Vec2
pub fn splat(value: Float) -> Vec2
pub fn sub(a: Vec2, b: Vec2) -> Vec2
pub fn zero() -> Vec2
Search Document