Skip to content

math.vec #

Constants #

const vec_epsilon = f32(10e-7)

fn vec2 #

fn vec2[T](x T, y T) Vec2[T]

vec2[T] returns a Vec2 of type T, with x and y fields set.

fn vec3 #

fn vec3[T](x T, y T, z T) Vec3[T]

vec3[T] returns a Vec3 of type T, with x,y and z fields set.

fn vec4 #

fn vec4[T](x T, y T, z T, w T) Vec4[T]

vec4[T] returns a Vec4 of type T, with x,y,z and w fields set.

fn (Vec2[T]) zero #

fn (mut v Vec2[T]) zero()

zero sets the x and y fields to 0.

fn (Vec2[T]) one #

fn (mut v Vec2[T]) one()

one sets the x and y fields to 1.

fn (Vec2[T]) copy #

fn (v Vec2[T]) copy() Vec2[T]

copy returns a copy of this vector.

fn (Vec2[T]) from #

fn (mut v Vec2[T]) from(u Vec2[T])

from sets the x and y fields from u.

fn (Vec2[T]) from_vec3 #

fn (mut v Vec2[T]) from_vec3[U](u Vec3[U])

from_vec3 sets the x and y fields from u.

fn (Vec2[T]) as_vec3 #

fn (v Vec2[T]) as_vec3[T]() Vec3[T]

as_vec3 returns a Vec3 with x and y fields set from v, z is set to 0.

fn (Vec2[T]) from_vec4 #

fn (mut v Vec2[T]) from_vec4[U](u Vec4[U])

from_vec4 sets the x and y fields from u.

fn (Vec2[T]) as_vec4 #

fn (v Vec2[T]) as_vec4[T]() Vec4[T]

as_vec4 returns a Vec4 with x and y fields set from v, z and w is set to 0.

fn (Vec2[T]) + #

fn (v Vec2[T]) + (u Vec2[T]) Vec2[T]
  • returns the resulting vector of the addition of v and u.

fn (Vec2[T]) add #

fn (v Vec2[T]) add(u Vec2[T]) Vec2[T]

add returns the resulting vector of the addition of v + u.

fn (Vec2[T]) add_scalar #

fn (v Vec2[T]) add_scalar[U](scalar U) Vec2[T]

add_scalar returns the resulting vector of the addition of the scalar value.

fn (Vec2[T]) plus #

fn (mut v Vec2[T]) plus(u Vec2[T])

plus adds vector u to the vector.

fn (Vec2[T]) plus_scalar #

fn (mut v Vec2[T]) plus_scalar[U](scalar U)

plus_scalar adds the scalar scalar to the vector.

fn (Vec2[T]) - #

fn (v Vec2[T]) - (u Vec2[T]) Vec2[T]
  • returns the resulting vector of the subtraction of v and u.

fn (Vec2[T]) sub #

fn (v Vec2[T]) sub(u Vec2[T]) Vec2[T]

sub returns the resulting vector of the subtraction of v - u.

fn (Vec2[T]) sub_scalar #

fn (v Vec2[T]) sub_scalar[U](scalar U) Vec2[T]

sub_scalar returns the resulting vector of the subtraction of the scalar value.

fn (Vec2[T]) subtract #

fn (mut v Vec2[T]) subtract(u Vec2[T])

subtract subtracts vector u from the vector.

fn (Vec2[T]) subtract_scalar #

fn (mut v Vec2[T]) subtract_scalar[U](scalar U)

subtract_scalar subtracts the scalar scalar from the vector.

fn (Vec2[T]) * #

fn (v Vec2[T]) * (u Vec2[T]) Vec2[T]
  • returns the resulting vector of the multiplication of v and u.

fn (Vec2[T]) mul #

fn (v Vec2[T]) mul(u Vec2[T]) Vec2[T]

mul returns the resulting vector of the multiplication of v * u.

fn (Vec2[T]) mul_scalar #

fn (v Vec2[T]) mul_scalar[U](scalar U) Vec2[T]

mul_scalar returns the resulting vector of the multiplication of the scalar value.

fn (Vec2[T]) multiply #

fn (mut v Vec2[T]) multiply(u Vec2[T])

multiply multiplies the vector with u.

fn (Vec2[T]) multiply_scalar #

fn (mut v Vec2[T]) multiply_scalar[U](scalar U)

multiply_scalar multiplies the vector with scalar.

fn (Vec2[T]) / #

fn (v Vec2[T]) / (u Vec2[T]) Vec2[T]

/ returns the resulting vector of the division of v and u.

fn (Vec2[T]) div #

fn (v Vec2[T]) div(u Vec2[T]) Vec2[T]

div returns the resulting vector of the division of v / u.

fn (Vec2[T]) div_scalar #

fn (v Vec2[T]) div_scalar[U](scalar U) Vec2[T]

div_scalar returns the resulting vector of the division by the scalar value.

fn (Vec2[T]) divide #

fn (mut v Vec2[T]) divide(u Vec2[T])

divide divides the vector by u.

fn (Vec2[T]) divide_scalar #

fn (mut v Vec2[T]) divide_scalar[U](scalar U)

divide_scalar divides the vector by scalar.

fn (Vec2[T]) magnitude #

fn (v Vec2[T]) magnitude() T

magnitude returns the magnitude, also known as the length, of the vector.

fn (Vec2[T]) magnitude_x #

fn (v Vec2[T]) magnitude_x() T

magnitude_x returns the magnitude, also known as the length, of the 1D vector field x, y is ignored.

fn (Vec2[T]) magnitude_y #

fn (v Vec2[T]) magnitude_y() T

magnitude_x returns the magnitude, also known as the length, of the 1D vector field y, x is ignored.

fn (Vec2[T]) dot #

fn (v Vec2[T]) dot(u Vec2[T]) T

dot returns the dot product of v and u. The dot product is a scalar value that represents the magnitude of one vector projected onto another vector. It is calculated by multiplying the corresponding components of the vectors and summing the results. example:

v := vec2[f32](3, 4) //magnitude = 5
u := vec2[f32](5, 6) //magnitude = 7.81
dot := v.dot(u) // 3*5 + 4*6 = 15 + 24 = 39
    (dot) // Output: 39

fn (Vec2[T]) cross #

fn (v Vec2[T]) cross(u Vec2[T]) T

cross returns the cross product of v and u.

fn (Vec2[T]) unit #

fn (v Vec2[T]) unit() Vec2[T]

unit returns the unit vector. unit vectors always have a magnitude, or length, of exactly 1.

fn (Vec2[T]) perp_cw #

fn (v Vec2[T]) perp_cw() Vec2[T]

perp_cw returns the clockwise, or "left-hand", perpendicular vector of this vector.

fn (Vec2[T]) perp_ccw #

fn (v Vec2[T]) perp_ccw() Vec2[T]

perp_ccw returns the counter-clockwise, or "right-hand", perpendicular vector of this vector.

fn (Vec2[T]) perpendicular #

fn (v Vec2[T]) perpendicular(u Vec2[T]) Vec2[T]

perpendicular returns the v projected perpendicular vector to the 'u' vector.

fn (Vec2[T]) project #

fn (v Vec2[T]) project(u Vec2[T]) Vec2[T]

project returns the projected vector. The projection of vector v onto vector u is the orthogonal projection of v onto a straight line parallel to u that passes through the origin. This is equivalent to the vector projection of v onto the unit vector in the direction of u. and is given by the formula: proj_v(u) = (v · u / |u|^2) * u where "·" denotes the dot product and |u| is the magnitude of vector u. If v is a zero vector, the result will also be a zero vector. example:

v := vec2[f32](3, 4)
u := vec2[f32](5, 6)
proj := v.project(u)
println(proj) // Output: vec2[f32](3.1967213, 3.8360658)

fn (Vec2[T]) rotate_around_cw #

fn (v Vec2[T]) rotate_around_cw(o Vec2[T], radians f64) Vec2[T]

rotate_around_cw returns the vector v rotated clockwise radians around an origin vector o in Cartesian space.

fn (Vec2[T]) rotate_around_ccw #

fn (v Vec2[T]) rotate_around_ccw(o Vec2[T], radians f64) Vec2[T]

rotate_around_ccw returns the vector v rotated counter-clockwise radians around an origin vector o in Cartesian space.

fn (Vec2[T]) eq #

fn (v Vec2[T]) eq(u Vec2[T]) bool

eq returns a bool indicating if the two vectors are equal.

fn (Vec2[T]) eq_epsilon #

fn (v Vec2[T]) eq_epsilon(u Vec2[T]) bool

eq_epsilon returns a bool indicating if the two vectors are equal within the module vec_epsilon const.

fn (Vec2[T]) eq_approx #

fn (v Vec2[T]) eq_approx[T, U](u Vec2[T], tolerance U) bool

eq_approx returns whether these vectors are approximately equal within tolerance.

fn (Vec2[T]) is_approx_zero #

fn (v Vec2[T]) is_approx_zero(tolerance T) bool

is_approx_zero returns whether this vector is equal to zero within tolerance.

fn (Vec2[T]) eq_scalar #

fn (v Vec2[T]) eq_scalar[U](scalar U) bool

eq_scalar returns a bool indicating if the x and y fields both equals scalar.

fn (Vec2[T]) distance #

fn (v Vec2[T]) distance(u Vec2[T]) T

distance returns the distance to the vector u.

fn (Vec2[T]) manhattan_distance #

fn (v Vec2[T]) manhattan_distance(u Vec2[T]) T

manhattan_distance returns the Manhattan Distance to the vector u.

fn (Vec2[T]) angle_between #

fn (v Vec2[T]) angle_between(u Vec2[T]) T

angle_between returns the angle in radians to the vector u.

fn (Vec2[T]) angle_towards #

fn (p1 Vec2[T]) angle_towards(p2 Vec2[T]) T

angle_towards returns the angle in radians between the horizontal axis, and a line passing through the first and second point, as if the first point was at the center of the coordinate system.

fn (Vec2[T]) angle #

fn (v Vec2[T]) angle() T

angle returns the angle in radians of the vector. example:

v := vec2[f32](3.0, 4.0)
a := v.angle()
assert a == 0.64 (approximate value in radians)
w := vec2[f32](0.0, 1.0)
b := w.angle()
assert b == 1.57 (approximate value in radians)

fn (Vec2[T]) abs #

fn (mut v Vec2[T]) abs()

abs sets x and y field values to their absolute values.

fn (Vec2[T]) clean #

fn (v Vec2[T]) clean[U](tolerance U) Vec2[T]

clean returns a vector with all fields of this vector set to zero (0) if they fall within tolerance.

fn (Vec2[T]) clean_tolerance #

fn (mut v Vec2[T]) clean_tolerance[U](tolerance U)

clean_tolerance sets all fields to zero (0) if they fall within tolerance.

fn (Vec2[T]) inv #

fn (v Vec2[T]) inv() Vec2[T]

inv returns the inverse, or reciprocal, of the vector. If a field is zero, its inverse is also set to zero to avoid division by zero. the direction the vector points is generally not preserved, but the magnitude of each field is inverted. example:

v := vec2[f32](2.0, 4.0)
inv_v := v.inv() // inv_v == vec2[f32](0.5, 0.25)

fn (Vec2[T]) normalize #

fn (v Vec2[T]) normalize() Vec2[T]

normalize normalizes the vector. A normalized vector has the same direction as the original vector but a magnitude of 1. If the vector has a magnitude of 0, a zero vector is returned since we cannot find the direction of a zero-length vector. example:

v := vec2[f32](3.0, 4.0)//magnitude = 5.0
n := v.normalize() // n == vec2[f32](0.6, 0.8) // magnitude = 1.0

fn (Vec2[T]) sum #

fn (v Vec2[T]) sum() T

sum returns a sum of all the fields. example:

v := vec2[f32](3.0, 4.0)
s := v.sum()
assert s == 7.0

fn (Vec3[T]) zero #

fn (mut v Vec3[T]) zero()

zero sets the x,y and z fields to 0.

fn (Vec3[T]) one #

fn (mut v Vec3[T]) one()

one sets the x,y and z fields to 1.

fn (Vec3[T]) copy #

fn (mut v Vec3[T]) copy() Vec3[T]

copy returns a copy of this vector.

fn (Vec3[T]) from #

fn (mut v Vec3[T]) from(u Vec3[T])

from sets the x,y and z fields from u.

fn (Vec3[T]) from_vec2 #

fn (mut v Vec3[T]) from_vec2[U](u Vec2[U])

from_vec2 sets the x and y fields from u.

fn (Vec3[T]) as_vec2 #

fn (mut v Vec3[T]) as_vec2[T]() Vec2[T]

as_vec2 returns a Vec2 with x and y fields set from v.

fn (Vec3[T]) from_vec4 #

fn (mut v Vec3[T]) from_vec4[U](u Vec4[U])

from_vec4 sets the x,y and z fields from u.

fn (Vec3[T]) as_vec4 #

fn (mut v Vec3[T]) as_vec4[T]() Vec4[T]

as_vec4 returns a Vec4 with x,y and z fields set from v, w is set to 0.

fn (Vec3[T]) + #

fn (v Vec3[T]) + (u Vec3[T]) Vec3[T]
  • returns the resulting vector of the addition of v and u.

fn (Vec3[T]) add #

fn (v Vec3[T]) add(u Vec3[T]) Vec3[T]

add returns the resulting vector of the addition of v + u.

fn (Vec3[T]) add_vec2 #

fn (v Vec3[T]) add_vec2[U](u Vec2[U]) Vec3[T]

add_vec2 returns the resulting vector of the addition of the x and y fields of u, z is left untouched.

fn (Vec3[T]) add_scalar #

fn (v Vec3[T]) add_scalar[U](scalar U) Vec3[T]

add_scalar returns the resulting vector of the addition of the scalar value.

fn (Vec3[T]) plus #

fn (mut v Vec3[T]) plus(u Vec3[T])

plus adds vector u to the vector.

fn (Vec3[T]) plus_vec2 #

fn (mut v Vec3[T]) plus_vec2[U](u Vec2[U])

plus_vec2 adds x and y fields of vector u to the vector, z is left untouched.

fn (Vec3[T]) plus_scalar #

fn (mut v Vec3[T]) plus_scalar[U](scalar U)

plus_scalar adds the scalar scalar to the vector.

fn (Vec3[T]) - #

fn (v Vec3[T]) - (u Vec3[T]) Vec3[T]
  • returns the resulting vector of the subtraction of v and u.

fn (Vec3[T]) sub #

fn (v Vec3[T]) sub(u Vec3[T]) Vec3[T]

sub returns the resulting vector of the subtraction of v - u.

fn (Vec3[T]) sub_scalar #

fn (v Vec3[T]) sub_scalar[U](scalar U) Vec3[T]

sub_scalar returns the resulting vector of the subtraction of the scalar value.

fn (Vec3[T]) subtract #

fn (mut v Vec3[T]) subtract(u Vec3[T])

subtract subtracts vector u from the vector.

fn (Vec3[T]) subtract_scalar #

fn (mut v Vec3[T]) subtract_scalar[U](scalar U)

subtract_scalar subtracts the scalar scalar from the vector.

fn (Vec3[T]) * #

fn (v Vec3[T]) * (u Vec3[T]) Vec3[T]
  • returns the resulting vector of the multiplication of v and u.

fn (Vec3[T]) mul #

fn (v Vec3[T]) mul(u Vec3[T]) Vec3[T]

mul returns the resulting vector of the multiplication of v * u.

fn (Vec3[T]) mul_scalar #

fn (v Vec3[T]) mul_scalar[U](scalar U) Vec3[T]

mul_scalar returns the resulting vector of the multiplication of the scalar value.

fn (Vec3[T]) multiply #

fn (mut v Vec3[T]) multiply(u Vec3[T])

multiply multiplies the vector with u.

fn (Vec3[T]) multiply_scalar #

fn (mut v Vec3[T]) multiply_scalar[U](scalar U)

multiply_scalar multiplies the vector with scalar.

fn (Vec3[T]) / #

fn (v Vec3[T]) / (u Vec3[T]) Vec3[T]

/ returns the resulting vector of the division of v and u.

fn (Vec3[T]) div #

fn (v Vec3[T]) div(u Vec3[T]) Vec3[T]

div returns the resulting vector of the division of v / u.

fn (Vec3[T]) div_scalar #

fn (v Vec3[T]) div_scalar[U](scalar U) Vec3[T]

div_scalar returns the resulting vector of the division by the scalar value.

fn (Vec3[T]) divide #

fn (mut v Vec3[T]) divide(u Vec3[T])

divide divides the vector by u.

fn (Vec3[T]) divide_scalar #

fn (mut v Vec3[T]) divide_scalar[U](scalar U)

divide_scalar divides the vector by scalar.

fn (Vec3[T]) magnitude #

fn (v Vec3[T]) magnitude() T

magnitude returns the magnitude, also known as the length, of the vector.

fn (Vec3[T]) dot #

fn (v Vec3[T]) dot(u Vec3[T]) T

dot returns the dot product of v and u.

fn (Vec3[T]) cross #

fn (v Vec3[T]) cross(u Vec3[T]) Vec3[T]

cross returns the cross product of v and u.

fn (Vec3[T]) unit #

fn (v Vec3[T]) unit() Vec3[T]

unit returns the unit vector. unit vectors always have a magnitude, or length, of exactly 1.

fn (Vec3[T]) perpendicular #

fn (v Vec3[T]) perpendicular(u Vec3[T]) Vec3[T]

perpendicular returns the v projected perpendicular vector to the 'u' vector.

fn (Vec3[T]) project #

fn (v Vec3[T]) project(u Vec3[T]) Vec3[T]

project returns the projected vector. The projection of vector v onto vector u is the orthogonal projection of v onto a straight line parallel to u that passes through the origin. This is equivalent to the vector projection of v onto the unit vector in the direction of u. and is given by the formula: proj_v(u) = (v · u / |u|^2) * u where "·" denotes the dot product and |u| is the magnitude of vector u. If v is a zero vector, the result will also be a zero vector. example:

Todo: add examples

fn (Vec3[T]) eq #

fn (v Vec3[T]) eq(u Vec3[T]) bool

eq returns a bool indicating if the two vectors are equal.

fn (Vec3[T]) eq_epsilon #

fn (v Vec3[T]) eq_epsilon(u Vec3[T]) bool

eq_epsilon returns a bool indicating if the two vectors are equal within the module vec_epsilon const.

fn (Vec3[T]) eq_approx #

fn (v Vec3[T]) eq_approx[T, U](u Vec3[T], tolerance U) bool

eq_approx returns whether these vectors are approximately equal within tolerance.

fn (Vec3[T]) is_approx_zero #

fn (v Vec3[T]) is_approx_zero(tolerance f64) bool

is_approx_zero returns whether this vector is equal to zero within tolerance.

fn (Vec3[T]) eq_scalar #

fn (v Vec3[T]) eq_scalar[U](scalar U) bool

eq_scalar returns a bool indicating if the x,y and z fields all equals scalar.

fn (Vec3[T]) distance #

fn (v Vec3[T]) distance(u Vec3[T]) f64

distance returns the distance to the vector u.

fn (Vec3[T]) manhattan_distance #

fn (v Vec3[T]) manhattan_distance(u Vec3[T]) f64

manhattan_distance returns the Manhattan distance to the vector u.

fn (Vec3[T]) angle_between #

fn (v Vec3[T]) angle_between(u Vec3[T]) T

angle_between returns the angle in radians to the vector u.

fn (Vec3[T]) abs #

fn (mut v Vec3[T]) abs()

abs sets x, y and z field values to their absolute values.

fn (Vec3[T]) clean #

fn (v Vec3[T]) clean[U](tolerance U) Vec3[T]

clean returns a vector with all fields of this vector set to zero (0) if they fall within tolerance.

fn (Vec3[T]) clean_tolerance #

fn (mut v Vec3[T]) clean_tolerance[U](tolerance U)

clean_tolerance sets all fields to zero (0) if they fall within tolerance.

fn (Vec3[T]) inv #

fn (v Vec3[T]) inv() Vec3[T]

inv returns the inverse, or reciprocal, of the vector.

fn (Vec3[T]) normalize #

fn (v Vec3[T]) normalize() Vec3[T]

normalize normalizes the vector.

fn (Vec3[T]) sum #

fn (v Vec3[T]) sum() T

sum returns a sum of all the fields.

fn (Vec4[T]) zero #

fn (mut v Vec4[T]) zero()

zero sets the x,y,z and w fields to 0.

fn (Vec4[T]) one #

fn (mut v Vec4[T]) one()

one sets the x,y,z and w fields to 1.

fn (Vec4[T]) copy #

fn (v Vec4[T]) copy() Vec4[T]

copy returns a copy of this vector.

fn (Vec4[T]) from #

fn (mut v Vec4[T]) from(u Vec4[T])

from sets the x,y,z and w fields from u.

fn (Vec4[T]) from_vec2 #

fn (mut v Vec4[T]) from_vec2(u Vec2[T])

from_vec2 sets the x and y fields from u.

fn (Vec4[T]) as_vec2 #

fn (v Vec4[T]) as_vec2[U]() Vec2[U]

as_vec2 returns a Vec2 with x and y fields set from v.

fn (Vec4[T]) from_vec3 #

fn (mut v Vec4[T]) from_vec3[U](u Vec3[U])

from_vec3 sets the x,y and z fields from u.

fn (Vec4[T]) as_vec3 #

fn (v Vec4[T]) as_vec3[U]() Vec3[U]

as_vec3 returns a Vec3 with x,y and z fields set from v.

fn (Vec4[T]) + #

fn (v Vec4[T]) + (u Vec4[T]) Vec4[T]
  • returns the resulting vector of the addition of v and u.

fn (Vec4[T]) add #

fn (v Vec4[T]) add(u Vec4[T]) Vec4[T]

add returns the resulting vector of the addition of v + u.

fn (Vec4[T]) add_vec2 #

fn (v Vec4[T]) add_vec2[U](u Vec2[U]) Vec4[T]

add_vec2 returns the resulting vector of the addition of the x and y fields of u, z is left untouched.

fn (Vec4[T]) add_vec3 #

fn (v Vec4[T]) add_vec3[U](u Vec3[U]) Vec4[T]

add_vec3 returns the resulting vector of the addition of the x,y and z fields of u, w is left untouched.

fn (Vec4[T]) add_scalar #

fn (v Vec4[T]) add_scalar[U](scalar U) Vec4[T]

add_scalar returns the resulting vector of the addition of the scalar value.

fn (Vec4[T]) plus #

fn (mut v Vec4[T]) plus(u Vec4[T])

plus adds vector u to the vector.

fn (Vec4[T]) plus_scalar #

fn (mut v Vec4[T]) plus_scalar[U](scalar U)

plus_scalar adds the scalar scalar to the vector.

fn (Vec4[T]) - #

fn (v Vec4[T]) - (u Vec4[T]) Vec4[T]
  • returns the resulting vector of the subtraction of v and u.

fn (Vec4[T]) sub #

fn (v Vec4[T]) sub(u Vec4[T]) Vec4[T]

sub returns the resulting vector of the subtraction of v - u.

fn (Vec4[T]) sub_scalar #

fn (v Vec4[T]) sub_scalar[U](scalar U) Vec4[T]

sub_scalar returns the resulting vector of the subtraction of the scalar value.

fn (Vec4[T]) subtract #

fn (mut v Vec4[T]) subtract(u Vec4[T])

subtract subtracts vector u from the vector.

fn (Vec4[T]) subtract_scalar #

fn (mut v Vec4[T]) subtract_scalar[U](scalar U)

subtract_scalar subtracts the scalar scalar from the vector.

fn (Vec4[T]) * #

fn (v Vec4[T]) * (u Vec4[T]) Vec4[T]
  • returns the resulting vector of the multiplication of v and u.

fn (Vec4[T]) mul #

fn (v Vec4[T]) mul(u Vec4[T]) Vec4[T]

mul returns the resulting vector of the multiplication of v * u.

fn (Vec4[T]) mul_scalar #

fn (v Vec4[T]) mul_scalar[U](scalar U) Vec4[T]

mul_scalar returns the resulting vector of the multiplication of the scalar value.

fn (Vec4[T]) multiply #

fn (mut v Vec4[T]) multiply(u Vec4[T])

multiply multiplies the vector with u.

fn (Vec4[T]) multiply_scalar #

fn (mut v Vec4[T]) multiply_scalar[U](scalar U)

multiply_scalar multiplies the vector with scalar.

fn (Vec4[T]) / #

fn (v Vec4[T]) / (u Vec4[T]) Vec4[T]

/ returns the resulting vector of the division of v and u.

fn (Vec4[T]) div #

fn (v Vec4[T]) div(u Vec4[T]) Vec4[T]

div returns the resulting vector of the division of v / u.

fn (Vec4[T]) div_scalar #

fn (v Vec4[T]) div_scalar[U](scalar U) Vec4[T]

div_scalar returns the resulting vector of the division by the scalar value.

fn (Vec4[T]) divide #

fn (mut v Vec4[T]) divide(u Vec4[T])

divide divides the vector by u.

fn (Vec4[T]) divide_scalar #

fn (mut v Vec4[T]) divide_scalar[U](scalar U)

divide_scalar divides the vector by scalar.

fn (Vec4[T]) magnitude #

fn (v Vec4[T]) magnitude() T

magnitude returns the magnitude, also known as the length, of the vector.

fn (Vec4[T]) dot #

fn (v Vec4[T]) dot(u Vec4[T]) T

dot returns the dot product of v and u.

fn (Vec4[T]) cross_xyz #

fn (v Vec4[T]) cross_xyz(u Vec4[T]) Vec4[T]

cross_xyz returns the cross product of v and u's x,y and z fields.

fn (Vec4[T]) unit #

fn (v Vec4[T]) unit() Vec4[T]

unit returns the unit vector. unit vectors always have a magnitude, or length, of exactly 1.

fn (Vec4[T]) perpendicular #

fn (v Vec4[T]) perpendicular(u Vec4[T]) Vec4[T]

perpendicular returns the v projected perpendicular vector to the 'u' vector.

fn (Vec4[T]) project #

fn (v Vec4[T]) project(u Vec4[T]) Vec4[T]

project returns the projected vector. The projection of vector v onto vector u is the orthogonal projection of v onto a straight line parallel to u that passes through the origin. This is equivalent to the vector projection of v onto the unit vector in the direction of u. and is given by the formula: proj_v(u) = (v · u / |u|^2) * u where "·" denotes the dot product and |u| is the magnitude of vector u. If v is a zero vector, the result will also be a zero vector. example:

Todo: add examples

fn (Vec4[T]) eq #

fn (v Vec4[T]) eq(u Vec4[T]) bool

eq returns a bool indicating if the two vectors are equal.

fn (Vec4[T]) eq_epsilon #

fn (v Vec4[T]) eq_epsilon(u Vec4[T]) bool

eq_epsilon returns a bool indicating if the two vectors are equal within the module vec_epsilon const.

fn (Vec4[T]) eq_approx #

fn (v Vec4[T]) eq_approx[T, U](u Vec4[T], tolerance U) bool

eq_approx returns whether these vectors are approximately equal within tolerance.

fn (Vec4[T]) is_approx_zero #

fn (v Vec4[T]) is_approx_zero(tolerance f64) bool

is_approx_zero returns whether this vector is equal to zero within tolerance.

fn (Vec4[T]) eq_scalar #

fn (v Vec4[T]) eq_scalar[U](scalar U) bool

eq_scalar returns a bool indicating if the x,y,z and w fields all equals scalar.

fn (Vec4[T]) distance #

fn (v Vec4[T]) distance(u Vec4[T]) f64

distance returns the distance to the vector u.

fn (Vec4[T]) manhattan_distance #

fn (v Vec4[T]) manhattan_distance(u Vec4[T]) f64

manhattan_distance returns the Manhattan distance to the vector u.

fn (Vec4[T]) abs #

fn (mut v Vec4[T]) abs()

abs sets x, y, z and w field values to their absolute values.

fn (Vec4[T]) clean #

fn (v Vec4[T]) clean[U](tolerance U) Vec4[T]

clean returns a vector with all fields of this vector set to zero (0) if they fall within tolerance.

fn (Vec4[T]) clean_tolerance #

fn (mut v Vec4[T]) clean_tolerance[U](tolerance U)

clean_tolerance sets all fields to zero (0) if they fall within tolerance.

fn (Vec4[T]) inv #

fn (v Vec4[T]) inv() Vec4[T]

inv returns the inverse, or reciprocal, of the vector.

fn (Vec4[T]) normalize #

fn (v Vec4[T]) normalize() Vec4[T]

normalize normalizes the vector.

fn (Vec4[T]) sum #

fn (v Vec4[T]) sum() T

sum returns a sum of all the fields.

struct Vec2 #

struct Vec2[T] {
pub mut:
	x T
	y T
}

Vec2[T] is a generic struct representing a vector in 2D space.

struct Vec3 #

struct Vec3[T] {
pub mut:
	x T
	y T
	z T
}

Vec3[T] is a generic struct representing a vector in 3D space.

struct Vec4 #

struct Vec4[T] {
pub mut:
	x T
	y T
	z T
	w T
}

Vec4[T] is a generic struct representing a vector in 4D space.