跳转至

cf::ui::math

Mathematical utilities for UI animations and transitions. More...

Functions

Name
float lerp(float a, float b, float t)
Performs linear interpolation between two values.
float clamp(float value, float min, float max)
Clamps a value to the specified range.
float remap(float value, float inMin, float inMax, float outMin, float outMax)
Remaps a value from one range to another.
float cubicBezier(float x1, float y1, float x2, float y2, float t)
Evaluates a cubic Bezier easing curve.
std::pair< float, float > springStep(float position, float velocity, float target, float stiffness, float damping, float dt)
Performs a single step of spring physics simulation.
float lerpAngle(float a, float b, float t)
Performs linear interpolation between two angles.

Detailed Description

Mathematical utilities for UI animations and transitions.

Note: None of these functions perform thread synchronization.

Provides functions for value interpolation, clamping, remapping, cubic Bezier easing curves, spring physics simulation, and angle interpolation with boundary handling.

Functions Documentation

function lerp

float lerp(
    float a,
    float b,
    float t
)

Performs linear interpolation between two values.

Parameters:

  • a Start value.
  • b End value.
  • t Interpolation parameter. Valid range: [0.0, 1.0] for normal interpolation; values outside produce extrapolation.

Exceptions:

  • None

Return: Interpolated value between a and b.

Since: 0.1

Note: Commonly used for smooth transitions and animations.

Warning: No bounds checking on t; extrapolation occurs for t < 0 or t > 1.

Computes the linear interpolation between a and b using parameter t. When t is 0, returns a; when t is 1, returns b. Values outside [0, 1] produce extrapolated results.

function clamp

float clamp(
    float value,
    float min,
    float max
)

Clamps a value to the specified range.

Parameters:

  • value Value to clamp.
  • min Minimum bound (inclusive).
  • max Maximum bound (inclusive).

Exceptions:

  • None

Return: Clamped value in range [min, max].

Since: 0.1

Note: This function does not validate that min <= max.

Warning: Behavior is undefined if min > max.

Constrains value to the inclusive range [min, max]. If value is less than min, returns min; if greater than max, returns max.

function remap

float remap(
    float value,
    float inMin,
    float inMax,
    float outMin,
    float outMax
)

Remaps a value from one range to another.

Parameters:

  • value Input value to remap.
  • inMin Input range minimum.
  • inMax Input range maximum.
  • outMin Output range minimum.
  • outMax Output range maximum.

Exceptions:

  • None

Return: Remapped value in output range.

Since: 0.1

Note: If inMin == inMax, returns outMax when value >= inMin, otherwise returns outMin.

Warning: Behavior is undefined if inMin == inMax and value is NaN.

Transforms value from the input range [inMin, inMax] to the output range [outMin, outMax]. Preserves relative position within each range.

function cubicBezier

float cubicBezier(
    float x1,
    float y1,
    float x2,
    float y2,
    float t
)

Evaluates a cubic Bezier easing curve.

Parameters:

  • x1 First control point X coordinate. Valid range: [0.0, 1.0].
  • y1 First control point Y coordinate. No range restriction.
  • x2 Second control point X coordinate. Valid range: [0.0, 1.0].
  • y2 Second control point Y coordinate. No range restriction.
  • t Curve parameter. Valid range: [0.0, 1.0].

Exceptions:

  • None

Return: Y-value along the Bezier curve at parameter t.

Since: 0.1

Note: This is the explicit formula for y(t), not a solver for x(t). For animation easing, typically assumes x(t) ≈ t.

Warning: Values of t outside [0.0, 1.0] produce extrapolated results.

Computes the y-value at parameter t for a cubic Bezier curve with fixed endpoints P0=(0,0) and P3=(1,1), and control points P1=(x1,y1) and P2=(x2,y2). Used for custom easing functions in animations.

function springStep

std::pair< float, float > springStep(
    float position,
    float velocity,
    float target,
    float stiffness,
    float damping,
    float dt
)

Performs a single step of spring physics simulation.

Parameters:

  • position Current position.
  • velocity Current velocity.
  • target Target position the spring pulls toward.
  • stiffness Spring stiffness coefficient. Higher values create stronger, faster springs. Typical range: 100-500.
  • damping Spring damping coefficient. Lower values create more oscillation. Typical range: 5-30.
  • dt Time step in seconds. Typical value: 1/60 (60 FPS).

Exceptions:

  • None

Return: Pair containing [new_position, new_velocity].

Since: 0.1

Note: Stability requires dt to be small relative to stiffness. Large stiffness * dt may cause instability.

Warning: Negative stiffness creates an unstable (repulsive) spring.

Simulates one time step of a damped spring using semi-implicit Euler integration. Returns the new position and velocity after applying spring forces toward the target.

function lerpAngle

float lerpAngle(
    float a,
    float b,
    float t
)

Performs linear interpolation between two angles.

Parameters:

  • a Start angle in degrees. No range restriction.
  • b End angle in degrees. No range restriction.
  • t Interpolation parameter. Valid range: [0.0, 1.0].

Exceptions:

  • None

Return: Interpolated angle in degrees.

Since: 0.1

Note: The result is normalized to [0, 360) range.

Warning: None

Interpolates between angles a and b using parameter t, handling the wraparound at 0°/360°. Always takes the shortest path around the circle, whether clockwise or counter-clockwise.


Updated on 2026-03-09 at 10:14:00 +0000