# Unigine::Math::vec2 Struct

This class represents a vector of 2 float components.

## vec2 ( const hvec2 & v ) #

Constructor. Initializes the vector using a given hvec2 source vector.

### Arguments

• const hvec2 & v - Source vector.

## vec2 ( ) #

Default constructor. Produces a zero vector.

## vec2 ( const vec2 & v ) #

Constructor. Initializes the vector by copying a given source vector.

### Arguments

• const vec2 & v - Source vector.

## vec2 ( float x, float y ) #

Constructor. Initializes the vector using given float values.

### Arguments

• float x - X component of the vector.
• float y - Y component of the vector.

### Examples

Source code (UnigineScript)
``````vec2(2.0, 3.0);
/*
Creates a vector (2.0, 3.0)
*/``````

## explicit vec2 ( float v ) #

Constructor. Initializes the vector using a given scalar value: x=v, y=v.

### Arguments

• float v - Scalar value.

### Examples

Source code (UnigineScript)
``````vec2(1.0);
/*
Creates a vector (1.0, 1.0)
*/``````

## explicit vec2 ( const vec3 & v ) #

Constructor. Initializes the vector using a given three-component vec3 source vector: x=v.x, y=v.y.

### Arguments

• const vec3 & v - Three-component source vector.

## explicit vec2 ( const vec4 & v ) #

Constructor. Initializes the vector using a given four-component vec4 source vector: x=v.x, y=v.y.

### Arguments

• const vec4 & v - Four-component source vector.

## explicit vec2 ( const dvec2 & v ) #

Constructor. Initializes the vector using a given dvec2 source vector.

### Arguments

• const dvec2 & v - Source vector.

## explicit vec2 ( const ivec2 & v ) #

Constructor. Initializes the vector using a given ivec2 source vector.

### Arguments

• const ivec2 & v - Source vector.

## explicit vec2 ( const float * v ) #

Constructor. Initializes the vector using a given pointer to the array of float elements: x=v[0], y=v[1].

### Arguments

• const float * v - Pointer to the array of float elements.

## voidset ( float x_, float y_ ) #

Sets the vector by components.

### Arguments

• float x_ - X component of the vector.
• float y_ - Y component of the vector.

## voidset ( const float * val ) #

Sets the vector using the array of float elements: x=val[0], y=val[1].

### Arguments

• const float * val - Pointer to the array of float elements.

## voidset ( const vec2 & val ) #

Sets the vector equal to the specified source vector.

### Arguments

• const vec2 & val - Source vector.

## voidset ( float val ) #

Sets the vector components equal to specified scalar value: x=val, y=val.

### Arguments

• float val - Scalar value.

## voidget ( float * val ) # const

Gets the vector: val[0]=x, val[1]=y.

### Arguments

• float * val - Pointer to the array of float elements.

## float *get ( ) #

Returns the pointer to the vector.

### Return value

Pointer to the vector.

## const float *get ( ) # const

Returns the constant pointer to the vector.

### Return value

Pointer to the vector.

## floatlength ( ) #

Returns the length of the vector.

Vector length.

## floatlength2 ( ) #

Returns the squared length of the vector.

### Return value

Squared length of the vector.

## floatmax ( ) #

Compares the vector components and returns the greater one.

### Return value

The greater out of the two vector components.

## vec2 &normalize ( ) #

Returns normalized vector.

### Return value

Normalized vector.

## vec2 &normalizeFast ( ) #

Returns normalized vector, calculated using the fast inverse square root algorithm.

### Return value

Normalized vector.

## const float *operator const float * ( ) #

Performs type conversion to const float *.

## const void *operator const void * ( ) #

Performs type conversion to const void *.

## float *operator float * ( ) #

Performs type conversion to float *.

## void *operator void * ( ) #

Performs type conversion to void *.

## vec2 &operator*= ( float val ) #

Performs scalar multiplication.

### Arguments

• float val - Scalar value.

### Return value

Resulting vector.

## vec2 &operator*= ( const vec2 & val ) #

Performs vector multiplication.

### Arguments

• const vec2 & val - Vector.

### Return value

Resulting vector.

## vec2 &operator+= ( const vec2 & val ) #

### Arguments

• const vec2 & val - Vector.

### Return value

Resulting vector.

## vec2operator- ( ) # const

Performs vector negation.

### Return value

Resulting vector.

## vec2 &operator-= ( const vec2 & val ) #

Performs vector subtraction.

### Arguments

• const vec2 & val - Vector.

### Return value

Resulting vector.

## vec2 &operator/= ( const vec2 & val ) #

Performs componentwise division of vectors.

### Arguments

• const vec2 & val - Vector.

### Return value

Resulting vector.

### Examples

Source code (UnigineScript)
``````vec2 a, b;
a = vec2(6.0, 10.0);
b = vec2(2.0, 5.0);
a /= b;
/*
Initial values of vectors a and b:
a (6.0, 10.0)
b (2.0, 5.0)

a /= b;
Vector a after operation:
a (3.0, 2.0)
*/``````

## vec2 &operator/= ( float val ) #

Performs componentwise division of the vector by the scalar. Implemented using the calculation of inverse scalar value with subsequent by-component multiplication.

### Arguments

• float val - Scalar value.

### Return value

Resulting vector.

### Examples

Source code (UnigineScript)
``````vec2 a = vec2(6.0, 10.0);
a /= 2.0;
/*
Initial value of vector a:
a (6.0, 10.0)

a /= 2.0;
Vector a after operation:
a (3.0, 5.0)
*/``````

## vec2 &operator= ( const vec2 & val ) #

Performs vector assignment. Destination vector = Source vector.

### Arguments

• const vec2 & val - Source vector.

Result.

## float &operator[] ( int i ) #

Performs array access to the vector item reference by using given item index.

### Arguments

• int i - Vector item index.

### Return value

Vector item reference.

## floatoperator[] ( int i ) # const

Performs array access to the vector item by using given item index.

### Arguments

• int i - Vector item index.

### Return value

Vector item.
Last update: 2020-05-19