Skip to content

class Vec4 (shared-side)

Available since version: 0.2

This class represents 4d vector.

Constructor


Parameters:

Constructor

float value

Parameters:

  • float value: the initial value for x, y, z, w components.

Constructor

float x, float y, float z, float w

Parameters:

  • float x: the initial value for x component.
  • float y: the initial value for y component.
  • float z: the initial value for z component.
  • float w: the initial value for w component.

Properties

float x

Available since version: 0.2

Represents the x component of the vector.


float y

Available since version: 0.2

Represents the y component of the vector.


float z

Available since version: 0.2

Represents the z component of the vector.


float w

Available since version: 0.2

Represents the w component of the vector.


Methods

len

Available since version: 0.2

This method will return the vector length. The length of the vector is square root of (x*x + y*y + z*z + w*w).

float len()

Returns float:

the squared length of the vector.


len2

Available since version: 0.2

This method will get the squared vector length. The length of the vector is (x*x + y*y + z*z + w*w). Calculating the squared length instead of the len is much faster.

float len2()

Returns float:

the length of the vector.


lenApprox

Available since version: 0.2

This method will get the approximate vector length. The length of the vector is approximation square root of (x*x + y*y + z*z + w*w). This method is faster than len but less accurate.

float lenApprox()

Returns float:

the length of the vector.


normalize

Available since version: 0.2

Note

Avoid normalizing a vector with length = 0.

This method will normalize the vector. Normalizing a vector will cause it's length to be equal to 1. The direction of the vector won't be affected.

Vec4& normalize()

Returns Vec4&:


normalizeSafe

Available since version: 0.2

This method will normalize the vector. It works almost exactly the same as normalize with the difference that zero length vector won't cause any issues.

Vec4& normalizeSafe()

Returns Vec4&:


normalizeApprox

Available since version: 0.2

Note

Avoid normalizing a vector with length = 0.

This method will normalize the vector with small approximation. It works almost the same as normalize, but it's faster and less accurate. It uses the fast inverse square root algorithm as the base of calculating square root.

Vec4& normalizeApprox()

Returns Vec4&:


set

Available since version: 0.2

This method will set all of the vector components.

void set(float x, float y, float z, float w)

Parameters:

  • float x: the new vector x component.
  • float y: the new vector y component.
  • float z: the new vector z component.
  • float w: the new vector z component.

isEqualEps

Available since version: 0.2

This method will return true if vectors are approximately the same. The minimal difference used for comparison is 0.001.

bool isEqualEps(Vec4 vec)

Parameters:

  • Vec4 vec: the vector that will be used in comparision.

Returns bool:

true if vectors are approximately the same, otherwise false.


abs

Available since version: 0.2

This method will return vector with absolute x,y,z,w components.

Vec4 abs()

Returns Vec4:

the new vector result.


static swap

Available since version: 0.2

This method will swap x,y,z,w components between two vectors.

void swap(Vec4 vec1, Vec4 vec2)

Parameters:

  • Vec4 vec1: the vector that will be used in swap operation.
  • Vec4 vec2: the vector that will be used in swap operation.

static min

Available since version: 0.2

This method will return vector with the smallest x,y,z,w components from two vectors.

Vec4 min(Vec4 vec1, Vec4 vec2)

Parameters:

  • Vec4 vec1: the vector that will be used in min operation.
  • Vec4 vec2: the vector that will be used in min operation.

Returns Vec4:

the new vector result.


static max

Available since version: 0.2

This method will return vector with the largest x,y,z,w components from two vectors.

Vec4 max(Vec4 vec1, Vec4 vec2)

Parameters:

  • Vec4 vec1: the vector that will be used in max operation.
  • Vec4 vec2: the vector that will be used in max operation.

Returns Vec4:

the new vector result.


static prod

Available since version: 0.2

This method will multiply two vectors and return the result as a new vector.

Vec4 prod(Vec4 vec1, Vec4 vec2)

Parameters:

  • Vec4 vec1: the vector that will be used in prod operation.
  • Vec4 vec2: the vector that will be used in prod operation.

Returns Vec4:

the new vector result.


static dot

Available since version: 0.2

This method will return the dot product between two vectors. For normalized vectors dot returns: - 1 if they point in exactly the same direction - -1 if they point in completely opposite directions and a number in between for other cases (e.g. Dot returns zero if vectors are perpendicular). For vectors of arbitrary length the Dot return values are similar: they get larger when the angle between vectors decreases.

float dot(Vec4 vec1, Vec4 vec2)

Parameters:

  • Vec4 vec1: the vector that will be used in dot operation.
  • Vec4 vec2: the vector that will be used in dot operation.

Returns float:

the dot product.


static lerp

Available since version: 0.2

This method will return interpolated vector between vectors a and b by ratio t.

Vec4 lerp(float t, Vec4 v1, Vec4 v2)

Parameters:

  • float t: interpolation ratio.
  • Vec4 v1: the vector used in lerp operation.
  • Vec4 v2: the vector used in lerp operation.

Returns Vec4:

the lerp vector result.