# Vec2¶

```#include <Imath/ImathVec.h>
```

The `Vec2` class template represents a 2D vector, with predefined typedefs for vectors of type `short`, `int`, `int64_t`, `float`, and `double`.

Note that the integer specializations of `Vec2` lack the `length()` and `normalize()` methods that are present in the `float` and `double` versions, because the results don’t fit into integer quantities.

There are also various utility functions that operate on vectors defined in `ImathVecAlgo.h` and described in Vector Functions.

Example:

```#include <Imath/ImathVec.h>

void
vec2_example()
{
Imath::V2f   a (1.0f, 2.0f);
Imath::V2f   b; // b is uninitialized

b.x = a[0];
b.y = a[1];

assert (a == b);

assert (a.length() == sqrt (a ^ a));

a.normalize();
assert (Imath::equalWithAbsError (a.length(), 1.0f, 1e-6f));
}
```
typedef Vec2<short> Imath::V2s

Vec2 of short.

typedef Vec2<int> Imath::V2i

Vec2 of integer.

typedef Vec2<int64_t> Imath::V2i64

Vec2 of int64_t.

typedef Vec2<float> Imath::V2f

Vec2 of float.

typedef Vec2<double> Imath::V2d

Vec2 of double.

template<class T>
class Imath::Vec2

2-element vector

T x
T y

Constructors and Assignment

inline Vec2() noexcept

Uninitialized by default.

inline explicit constexpr Vec2(T a) noexcept

Initialize to a scalar `(a,a)`

inline constexpr Vec2(T a, T b) noexcept

Initialize to given elements `(a,b)`

inline constexpr Vec2(const Vec2 &v) noexcept

Copy constructor.

template<class S>
inline constexpr Vec2(const Vec2<S> &v) noexcept

Construct from Vec2 of another base type.

inline constexpr const Vec2 &operator=(const Vec2 &v) noexcept

Assignment.

~Vec2() noexcept = default

Destructor.

Compatibility with Sb

template<class S>
inline void setValue(S a, S b) noexcept

Set the value.

template<class S>
inline void setValue(const Vec2<S> &v) noexcept

Set the value.

template<class S>
inline void getValue(S &a, S &b) const noexcept

Return the value in `a` and `b`

template<class S>
inline void getValue(Vec2<S> &v) const noexcept

Return the value in `v`

inline T *getValue() noexcept

Return a raw pointer to the array of values.

inline const T *getValue() const noexcept

Return a raw pointer to the array of values.

Arithmetic and Comparison

template<class S>
inline constexpr bool operator==(const Vec2<S> &v) const noexcept

Equality.

template<class S>
inline constexpr bool operator!=(const Vec2<S> &v) const noexcept

Inequality.

inline constexpr bool equalWithAbsError(const Vec2<T> &v, T e) const noexcept

Compare two matrices and test if they are “approximately equal”:

Returns

True if the coefficients of this and `m` are the same with an absolute error of no more than e, i.e., for all i, j:

```abs (this[i][j] - m[i][j]) <= e
```

inline constexpr bool equalWithRelError(const Vec2<T> &v, T e) const noexcept

Compare two matrices and test if they are “approximately equal”:

Returns

True if the coefficients of this and m are the same with a relative error of no more than e, i.e., for all i, j:

```abs (this[i] - v[i][j]) <= e * abs (this[i][j])
```

inline constexpr T dot(const Vec2 &v) const noexcept

Dot product.

inline constexpr T operator^(const Vec2 &v) const noexcept

Dot product.

inline constexpr T cross(const Vec2 &v) const noexcept

Right-handed cross product, i.e.

z component of Vec3 (this->x, this->y, 0) % Vec3 (v.x, v.y, 0)

inline constexpr T operator%(const Vec2 &v) const noexcept

Right-handed cross product, i.e.

z component of Vec3 (this->x, this->y, 0) % Vec3 (v.x, v.y, 0)

inline constexpr const Vec2 &operator+=(const Vec2 &v) noexcept

inline constexpr Vec2 operator+(const Vec2 &v) const noexcept

inline constexpr const Vec2 &operator-=(const Vec2 &v) noexcept

Component-wise subtraction.

inline constexpr Vec2 operator-(const Vec2 &v) const noexcept

Component-wise subtraction.

inline constexpr Vec2 operator-() const noexcept

Component-wise multiplication by -1.

inline constexpr const Vec2 &negate() noexcept

Component-wise multiplication by -1.

inline constexpr const Vec2 &operator*=(const Vec2 &v) noexcept

Component-wise multiplication.

inline constexpr const Vec2 &operator*=(T a) noexcept

Component-wise multiplication.

inline constexpr Vec2 operator*(const Vec2 &v) const noexcept

Component-wise multiplication.

inline constexpr Vec2 operator*(T a) const noexcept

Component-wise multiplication.

inline constexpr const Vec2 &operator/=(const Vec2 &v) noexcept

Component-wise division.

inline constexpr const Vec2 &operator/=(T a) noexcept

Component-wise division.

inline constexpr Vec2 operator/(const Vec2 &v) const noexcept

Component-wise division.

inline constexpr Vec2 operator/(T a) const noexcept

Component-wise division.

Query and Manipulation

inline T length() const noexcept

Return the Euclidean norm.

inline constexpr T length2() const noexcept

Return the square of the Euclidean norm, i.e.

the dot product with itself.

inline const Vec2 &normalize() noexcept

Normalize in place. If length()==0, return a null vector.

inline const Vec2 &normalizeExc()

Normalize in place. If length()==0, throw an exception.

inline const Vec2 &normalizeNonNull() noexcept

Normalize without any checks for length()==0.

Slightly faster than the other normalization routines, but if v.length() is 0.0, the result is undefined.

inline Vec2<T> normalized() const noexcept

Return a normalized vector. Does not modify *this.

inline Vec2<T> normalizedExc() const

Return a normalized vector.

Does not modify *this. Throw an exception if length()==0.

inline Vec2<T> normalizedNonNull() const noexcept

Return a normalized vector.

Does not modify *this, and does not check for length()==0. Slightly faster than the other normalization routines, but if v.length() is 0.0, the result is undefined.

Numeric Limits

static inline constexpr T baseTypeLowest() noexcept

Largest possible negative value.

static inline constexpr T baseTypeMax() noexcept

Largest possible positive value.

static inline constexpr T baseTypeSmallest() noexcept

Smallest possible positive value.

static inline constexpr T baseTypeEpsilon() noexcept

Smallest possible e for which 1+e != 1.

Public Types

typedef T BaseType

The base type: In templates that accept a parameter `V`, you can refer to `T` as `V::BaseType`

Public Functions

inline constexpr T &operator[](int i) noexcept

Element access by index.

inline constexpr const T &operator[](int i) const noexcept

Element access by index.

template<>
short length() const noexcept = delete
template<>
const Vec2<short> &normalize() noexcept = delete
template<>
const Vec2<short> &normalizeExc() = delete
template<>
const Vec2<short> &normalizeNonNull() noexcept = delete
template<>
Vec2<short> normalized() const noexcept = delete
template<>
Vec2<short> normalizedExc() const = delete
template<>
Vec2<short> normalizedNonNull() const noexcept = delete
template<>
int length() const noexcept = delete
template<>
const Vec2<int> &normalize() noexcept = delete
template<>
const Vec2<int> &normalizeExc() = delete
template<>
const Vec2<int> &normalizeNonNull() noexcept = delete
template<>
Vec2<int> normalized() const noexcept = delete
template<>
Vec2<int> normalizedExc() const = delete
template<>
Vec2<int> normalizedNonNull() const noexcept = delete
template<>
int64_t length() const noexcept = delete
template<>
const Vec2<int64_t> &normalize() noexcept = delete
template<>
const Vec2<int64_t> &normalizeExc() = delete
template<>
const Vec2<int64_t> &normalizeNonNull() noexcept = delete
template<>
Vec2<int64_t> normalized() const noexcept = delete
template<>
Vec2<int64_t> normalizedExc() const = delete
template<>
Vec2<int64_t> normalizedNonNull() const noexcept = delete

Public Static Functions

static inline constexpr unsigned int dimensions() noexcept

Return the number of dimensions, i.e. 2.

template<class T>
std::ostream &Imath::operator<<(std::ostream &s, const Vec2<T> &v)

Stream output, as “(x y)”.