Vec2:length
methodVec2:length() -> numberMagnitude.
handle40 methods, 2 fields, 6 operators
A 2D vector (Y-up, center-origin). Construct with datumhue.math.vec2(x, y). #v is v:length(), not a component count.
datumhue.math.vec2, datumhue.time.tween (in its callback), datumhue.input.mouse_position, datumhue.input.app_mouse_position , datumhue.input.mouse_delta, datumhue.input.scroll_delta, datumhue.input.touches, datumhue.input.app_touch_position and 38 moredatumhue.time.tween, datumhue.audio.synth, datumhue.audio.sfx, datumhue.audio.play, datumhue.physics.point_query, datumhue.debug.input.gamepad_stick , Scene:sprite, SceneNode:deform and 53 moreVec2 + Vec2 -> Vec2 Component-wise addition.Vec2 - Vec2 -> Vec2 Component-wise subtraction.Vec2 * Vec2 | number -> Vec2 Vector or scalar multiplication.Vec2 / Vec2 | number -> Vec2 Vector or scalar division.-Vec2 -> Vec2 Negation.#Vec2 -> number Magnitude (the # operator).| Field | Type | Access | Description |
|---|---|---|---|
x | number | read-only | X component. |
y | number | read-only | Y component. |
Vec2:length() -> numberMagnitude.
Vec2:length_squared() -> numberSquared magnitude (no sqrt).
Vec2:to_angle() -> numberAngle in radians from the +X axis.
-- Radians counterclockwise from +X, from -pi to pi.
local up = datumhue.math.vec2(0, 1)
local down = datumhue.math.vec2(0, -1)
print(string.format("%.4f", up:to_angle())) --> 1.5708
print(string.format("%.4f", down:to_angle())) --> -1.5708Vec2:min_element() -> numberSmallest component.
Vec2:max_element() -> numberLargest component.
Vec2:normalize() -> Vec2Unit vector. Raises if v is zero-length / non-finite; use normalize_or or try_normalize for a fallback.
Unit vector, or fallback if v is zero-length / non-finite.
Vec2:try_normalize() -> Vec2?Unit vector, or nil if v is zero-length / non-finite.
Vec2:distance_squared(o: Vec2) -> numberSquared Euclidean distance to o (no sqrt).
Vec2:signum() -> Vec2Component-wise sign (-1, 0, or +1).
Vec2:fract() -> Vec2Component-wise fractional part (self - self.trunc()).
Vec2:recip() -> Vec2Component-wise reciprocal (1/x).
Vec2:element_sum() -> numberSum of the components.
Vec2:element_product() -> numberProduct of the components.
Vec2:to_array() -> number[]Components as a {x, y} array.
Vec2:perp() -> Vec2Perpendicular vector (90 deg CCW).
-- A quarter turn counterclockwise.
print(datumhue.math.vec2(3, 4):perp()) --> vec2(-4.0, 3.0)Vec2:abs() -> Vec2Component-wise absolute value.
Vec2:floor() -> Vec2Component-wise floor.
Vec2:ceil() -> Vec2Component-wise ceil.
Vec2:round() -> Vec2Component-wise round.
Vec2:dot(o: Vec2) -> numberDot product.
Vec2:distance(o: Vec2) -> numberEuclidean distance to o.
Vec2:perp_dot(o: Vec2) -> numberPerpendicular dot (2D cross).
Vec2:angle_to(o: Vec2) -> numberSigned angle to o in radians.
-- Signed radians: positive turns counterclockwise.
local east = datumhue.math.vec2(1, 0)
local north = datumhue.math.vec2(0, 1)
print(string.format("%.4f", east:angle_to(north))) --> 1.5708
print(string.format("%.4f", north:angle_to(east))) --> -1.5708Rotate v by the rotation o represents.
-- A rotation is a unit vector at its angle: (0, 1) is a quarter
-- turn counterclockwise.
local v = datumhue.math.vec2(3, 4)
print(v:rotate(datumhue.math.vec2(0, 1))) --> vec2(-4.0, 3.0)
local turn = datumhue.math.vec2.from_angle(math.pi / 2)
print(v:rotate(turn):round()) --> vec2(-4.0, 3.0)Midpoint between v and o.
Reflect v about the normal o.
Component-wise minimum.
Component-wise maximum.
Projection of v onto o. Errors if o is zero-length.
Rejection of v from o. Errors if o is zero-length.
Linear interpolation toward o by t; t outside 0..1 extrapolates past the endpoints (not clamped).
local a = datumhue.math.vec2(0, 0)
local b = datumhue.math.vec2(10, 20)
print(a:lerp(b, 0.25)) --> vec2(2.5, 5.0)
-- t past 1 keeps going.
print(a:lerp(b, 1.5)) --> vec2(15.0, 30.0)Move toward o by at most distance d.
Component-wise clamp to [lo, hi].
In the guide: Draw and animate
Vec2:clamp_length(lo: number, hi: number) -> Vec2Clamp magnitude to [lo, hi].
Vec2:is_normalized() -> booleanWhether the vector is unit length.
Vec2:extend(z: number) -> Vec3Extend to a Vec3 with the given z.
Vec2:with_x(x: number) -> Vec2Copy with x replaced.
In the guide: Draw and animate API conventions
Vec2:with_y(y: number) -> Vec2Copy with y replaced.
Vec2:unpack() -> number, numberReturn the x and y components as two values.
local x, y = datumhue.math.vec2(3, 4):unpack()
print(x, y) --> 3.0 4.0