vec2

Header file: include/vec2.h

vec2 is a vector with 2 scalars, x and y.

\(vec2 = \begin{bmatrix} x \\ y \end{bmatrix}\)

Class variables

Type

Name

Description

float

x

The first element of the vec2.

float

y

The second element of the vec2.

Class functions

Constructors

Name

Description

vec2()

Construct a (0.0, 0.0) vector.

vec2(float _value)

Construct a (_value, _value) vector.

vec2(float _x, float _y)

Construct a (_x, _y) vector.

vec2(const float* _ptr)

Construct a vector from a pointer.

vec2(const nml::vec3& _xyz)

Construct a (_xyz.x, _xyz.y) vector.

vec2(const nml::vec4& _xyzw)

Construct a (_xyzw.x, _xyzw.y) vector.

Operators

Name

Description

nml::vec2& operator+=(const nml::vec2& other)

Add a vec2 to the current vec2.

nml::vec2& operator-=(const nml::vec2& other)

Substract a vec2 from the current vec2.

nml::vec2& operator*=(const float other)

Multiply the current vec2 by a scalar.

nml::vec2& operator/=(const float other)

Divide the current vec2 by a scalar.

nml::vec2 operator-()

Return the negative of the current vec2.

float& operator[](size_t index)

Return a reference to x if index is 0 or a reference to y if index is 1, else, throw an exception.

const float operator[](size_t index) const

Return the value of x if index is 0 or the value of y if index is 1, else, throw an exception.

Functions

Name

Description

float length()

Return the length of the vector.

float* data()

Return a pointer to the vector’s elements.

Namespace functions

Operators

Name

Description

nml::vec2 operator+(nml::vec2 lhs, const nml::vec2& rhs)

Return a vec2 that is the sum between two vec2.

nml::vec2 operator-(nml::vec2 lhs, const nml::vec2& rhs)

Return a vec2 that is the difference between two vec2.

nml::vec2 operator*(nml::vec2 lhs, const float rhs)

Return a vec2 that is the product between a vec2 and a scalar.

nml::vec2 operator*(float lhs, const nml::vec2& rhs)

Return a vec2 that is the product between a scalar and a vec2.

nml::vec2 operator/(nml::vec2 lhs, const float rhs)

Return a vec2 that is the quotient between a vec2 and a scalar.

bool operator==(const nml::vec2& lhs, const nml::vec2& rhs)

Return true if the two vec2 are identical, else, return false.

bool operator!=(const nml::vec2& lhs, const nml::vec2& rhs)

Return true if the two vec2 are different, else, return false.

Functions

Name

Description

nml::vec2 nml::normalize(const nml::vec2& vec)

Return a vec2 with the same direction as vec but with length 1.

float nml::dot(const nml::vec2& a, const nml::vec2& b)

Return the dot product between two vec2.

nml::vec2 nml::reflect(const nml::vec2& i, const nml::vec2& n)

Return the reflected direction between the incident vector i and the normal n. n should be normalized.

nml::vec2 nml::refract(const nml::vec2& i, const nml::vec2& n, float ior)

Return the refracted direction between the incident vector i, the normal n and the ratio of indices of refraction ior. n should be normalized.

std::string nml::to_string(const nml::vec2& vec)

Return a vec2 as a string under the format “[x, y]”.