Math

:= []

Math module - small 2D helpers for examples and simple games.

Coordinates use the same screen-space convention as Draw: x grows to the right, y grows downward, and rectangles are top-left plus width/height.

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.

Rect

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

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

Circle

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

Circle represented by center and radius.