# Vec2<T> class¶

<oceansdk/math/vec2.h>

## Description¶

This class allows manipulating 2D vectors and passing them through the SDK Interface functions

## Namespace¶

This class is in the root namespace

## Public members¶

template<typename T>
class Vec2
Template Parameters

T – The underlying numeric type, should be float or double

Vec2()

Constructs a Vec2 with uninitialized values

Vec2(T x1, T x2)

Constructs a Vec2 and initialize it with provided coordinates

Vec2(const T *data)

Constructs a Vec2 and initialize it with coordinates data[0] and data[1]

T &operator[](int i)

Returns a reference to the ith coordinate. i should be 0 or 1

T &at(int i)

Same as above

const T &operator[](int i) const

Returns a const reference to the ith coordinate. i should be 0, 1 or 2

const T &at(int i) const

Same as above

Vec2<T> operator+(const Vec2<T> &other) const

Returns the sum of this vector and other

Vec2<T> operator-(const Vec2<T> &other) const

Returns the difference of this vector and other

Vec2<T> operator*(T scalar) const

Returns the product of this vector and scalar

Vec2<T> operator-() const

Returns the opposite of this (negates all coordinates)

Vec2<T> &operator+=(const Vec2<T> &other) const

Adds other to this vector and returns a reference to this vector

Vec2<T> &operator*=(T scalar) const

Multiplies this vector by scalar and returns a reference to this vector

bool operator!=(const Vec2<T> &other) const

Returns true if this and other have not all coordinates equal, false otherwise

bool operator==(const Vec2<T> &other) const

Returns true if this and other have all coordinates equal, false otherwise

bool isFinite() const

Returns true if this vector coordinates are all finite (not NaN or inf)

T squareLength() const

Returns the square length of this

T length() const

Returns the length of this

const T *data() const

Returns a const pointer to the vector coordinates

T *data()

Returns a pointer to the vector coordinates

template<typename U>
Vec2<U> toVec2() const

Converts a Vec2<T> to a Vec2<U> by casting coordinates from T to U

std::string toString() const

Converts the vector to a string with space separated values

static Vec2<T> fromString(const std::string &str, bool *ok = 0)
Parameters
• str – The string to convert to a vector

• ok – If non null, the pointed value will be set to true if successful, false otherwise

Creates a vector from a string with space separated values