Math

Small 2D vector and rectangle helpers.

Coordinates follow Draw: x increases rightward and y downward. Rectangles use a top-left position, width, and height.

next = model.position.add(model.velocity.scale(input.time.elapsed_seconds))
vec3 : F32, F32, F32 -> Vec3

Construct a three-dimensional vector.

vec2 : F32, F32 -> Vec2

Construct a two-dimensional vector.

zero : Vec2

The zero vector.

circle : Vec2, F32 -> Circle

Construct a circle.

clamp01 : F32 -> F32

Clamp a value to the inclusive 0-to-1 range.

lerp : F32, F32, F32 -> F32

Linearly interpolate between two scalars; amounts outside 0 to 1 extrapolate.

sqrt : F32 -> F32

Square root for game-scale F32 values. Roc's pinned builtins do not expose sqrt yet, so keep this pure Roc.

add : Vec2, Vec2 -> Vec2

Add two vectors component-wise.

sub : Vec2, Vec2 -> Vec2

Subtract two vectors component-wise.

scale : Vec2, F32 -> Vec2

Multiply both vector components by a scalar.

dot : Vec2, Vec2 -> F32

Compute the vector dot product.

length : Vec2 -> F32

Euclidean vector length.

distance : Vec2, Vec2 -> F32

Euclidean distance between two points.

normalize : Vec2 -> Vec2

Return a unit vector, or zero when the input has zero length.

lerp_vec2 : Vec2, Vec2, F32 -> Vec2

Linearly interpolate between two vectors.

left : Rect -> F32

Left edge of a rectangle.

right : Rect -> F32

Right edge of a rectangle.

top : Rect -> F32

Top edge of a rectangle.

bottom : Rect -> F32

Bottom edge of a rectangle.

center : Rect -> Vec2

Center point of a rectangle.

closest_point : Rect, Vec2 -> Vec2

Closest point in or on a rectangle.

contains : Rect, Vec2 -> Bool

Whether a rectangle contains a point, including its edges.

circle_contains : Circle, Vec2 -> Bool

Whether a circle contains a point, including its edge.

overlaps : Rect, Rect -> Bool

Whether two rectangles overlap or touch.

circle_rect : Circle, Rect -> Bool

Whether a circle and rectangle overlap or touch.

contains_point : Circle, Vec2 -> Bool

Receiver-friendly aliases for circle queries. The longer names avoid ambiguity with the corresponding rectangle methods.

Vec2

:= {
    x : F32,
    y : F32,
}

Two-dimensional floating-point vector, also named Draw.Vector2.

circle : Vec2, F32 -> Circle

Construct a circle.

add : Vec2, Vec2 -> Vec2

Add two vectors component-wise.

sub : Vec2, Vec2 -> Vec2

Subtract two vectors component-wise.

scale : Vec2, F32 -> Vec2

Multiply both vector components by a scalar.

dot : Vec2, Vec2 -> F32

Compute the vector dot product.

length : Vec2 -> F32

Euclidean vector length.

distance : Vec2, Vec2 -> F32

Euclidean distance between two points.

normalize : Vec2 -> Vec2

Return a unit vector, or zero when the input has zero length.

lerp_vec2 : Vec2, Vec2, F32 -> Vec2

Linearly interpolate between two vectors.

encoder_for : encoding -> (Vec2, state -> Try(state, err))
    where [
        encoding.encode_vec2 : Vec2, state -> Try(state, err),
    ]

Encode this vector as one semantic leaf for formats that support 2D vectors. Shader uniform records use this for one GLSL vec2.

is_eq : Vec2, Vec2 -> Bool

Compare two of these values.

Vec3

:= {
    x : F32,
    y : F32,
    z : F32,
}

Three-dimensional floating-point vector.

encoder_for : encoding -> (Vec3, state -> Try(state, err)) where [encoding.encode_vec3 : Vec3, state -> Try(state, err)]
is_eq : Vec3, Vec3 -> Bool

Compare two of these values.

Vec4

:= {
    x : F32,
    y : F32,
    z : F32,
    w : F32,
}

Four-dimensional floating-point vector.

encoder_for : encoding -> (Vec4, state -> Try(state, err)) where [encoding.encode_vec4 : Vec4, state -> Try(state, err)]

Rect

:= {
    x : F32,
    y : F32,
    width : F32,
    height : F32,
}

Axis-aligned rectangle represented by top-left position and size.

left : Rect -> F32

Left edge of a rectangle.

right : Rect -> F32

Right edge of a rectangle.

top : Rect -> F32

Top edge of a rectangle.

bottom : Rect -> F32

Bottom edge of a rectangle.

center : Rect -> Vec2

Center point of a rectangle.

closest_point : Rect, Vec2 -> Vec2

Closest point in or on a rectangle.

contains : Rect, Vec2 -> Bool

Whether a rectangle contains a point, including its edges.

overlaps : Rect, Rect -> Bool

Whether two rectangles overlap or touch.

is_eq : Rect, Rect -> Bool

Compare two of these values.

Circle

:= {
    center : Vec2,
    radius : F32,
}

Circle represented by center and radius.

circle_contains : Circle, Vec2 -> Bool

Whether a circle contains a point, including its edge.

circle_rect : Circle, Rect -> Bool

Whether a circle and rectangle overlap or touch.

contains_point : Circle, Vec2 -> Bool

Receiver-friendly aliases for circle queries. The longer names avoid ambiguity with the corresponding rectangle methods.

is_eq : Circle, Circle -> Bool

Compare two of these values.