# Float2

A vector with two floating-point components `x` and `y`.

`#include <ply-math/Vector.h>`

Also included from `<ply-math/Base.h>`.

## Data Members

`float x` [code]
`float y` [code]

## Constructors

` Float2::Float2()` [code]

Constructs an uninitialized `Float2`.

` Float2::Float2(float t)` [code]

Constructs a `Float2` with both components set to `t`.

``````Float2 v = {1};
StdOut::text() << v;  // "{1, 1}"
``````
` Float2::Float2(float x, float y)` [code]

Constructs a `Float2` from the given components.

``````Float2 v = {1, 0};
``````

## Assignment

`void Float2::operator=(const Float2& arg) &` [code]

Copy assignment. Declared with an lvalue ref-qualifier so that it's an error to assign to an rvalue.

``````a.normalized() = b;  // error
``````

## Arithmetic Operators

`Float2 operator-(const Float2& a)` [code]

Unary negation.

`Float2 operator+(const Float2& a, const Float2& b)` [code]
`Float2 operator-(const Float2& a, const Float2& b)` [code]
`Float2 operator*(const Float2& a, const Float2& b)` [code]
`Float2 operator/(const Float2& a, const Float2& b)` [code]

Returns a vector whose components are the result of applying the given operation to the corresponding components of `a` and `b`. Each component is acted on independently.

``````StdOut::text() << Float2{2, 3} * Float2{4, 1};  // "{8, 3}"
``````

If you specify a scalar value in place of a `Float2`, it will be promoted to a `Float2` by replicating the value to each component.

``````StdOut::text() << Float2{2, 3} * 2;  // "{4, 6}"
StdOut::text() << 8 / Float2{2, 4};  // "{4, 2}"
``````
`void operator+=(Float2& a, const Float2& b)` [code]
`void operator-=(Float2& a, const Float2& b)` [code]
`void operator*=(Float2& a, const Float2& b)` [code]
`void operator/=(Float2& a, const Float2& b)` [code]

In-place versions of the above operators.

``````Float2 v = {2, 3};
v *= {4, 1};
StdOut::text() << v;  // "{8, 3}"
``````

## Comparison Functions

`bool operator==(const Float2& a, const Float2& b)` [code]
`bool operator!=(const Float2& a, const Float2& b)` [code]

Returns `true` if the vectors are equal (or not equal) using floating-point comparison. In particular, `Float2{0.f} == Float2{-0.f}` is `true`.

`bool isNear(const Float2& a, const Float2& b, float epsilon)` [code]

Returns `true` if `a` is approximately equal to `b`. The tolerance is given by `epsilon`.

``````Float2 v = {0.9999f, 0.0001f};
StdOut::text() << isNear(v, Float2{1, 0}, 1e-3f);  // "true"
``````
`Bool2 operator<(const Float2& a, const Float2& b)` [code]
`Bool2 operator<=(const Float2& a, const Float2& b)` [code]
`Bool2 operator>(const Float2& a, const Float2& b)` [code]
`Bool2 operator>=(const Float2& a, const Float2& b)` [code]

These functions compare each component individually. The result of each comparison is returned in a `Bool2`. Call `all()` to check if the result was `true` for all components, or call `any()` to check if the result was `true` for any component.

``````StdOut::text() << all(Float2{1, 2} > Float2{0, 1});  // "true"
``````

These functions are useful for testing whether a point is inside a box. See the implementation of `Box<>::contains` for an example.

## Geometric Functions

`float dot(const Float2& a, const Float2& b)` [code]

Returns the dot product of two vectors.

``````StdOut::text() << dot(Float2{1, 0}, Float2{3, 4});  // "2"
``````
`float cross(const Float2& a, const Float2& b)` [code]

Returns the cross product of two vectors.

``````StdOut::text() << cross(Float2{1, 0}, Float2{3, 4});  // "4"
``````

## Length Functions

`float Float2::length2() const` [code]

Returns the square of the length of the vector.

`float Float2::length() const` [code]

Returns the length of the vector. Equivalent to `sqrtf(this->length2())`.

`bool Float2::isUnit(float thresh = 0.001f) const` [code]

Returns `true` if the squared length of the vector is sufficiently close to 1.0. The threshold is given by `thresh`.

`Float2 Float2::normalized() const` [code]

Returns a unit-length vector having the same direction as `this`. No safety check is performed.

`Float2 Float2::safeNormalized(const Float2& fallback = {1, 0}, float epsilon = 1e-20f) const` [code]

Returns a unit-length vector having the same direction as `this` with safety checks.

## Conversion Functions

`template <typename OtherVec2>`
`OtherVec2 Float2::to() const` [code]

Converts to another 2D vector type such as `IntVec2` or `Int2<s16>`.

``````Float2 a = {4, 5};
IntVec2 b = a.to<IntVec2>();
``````

## Color Functions

`float& Float2::r()` [code]
`float Float2::r() const` [code]
`float& Float2::g()` [code]
`float Float2::g() const` [code]

Convenience functions for interpreting the vector as a color. The `r()` and `g()` functions are aliases for the `x` and `y` components respectively.

``````Float4 c = {1.0f, 0.8f};
StdOut::text().format("{}, {}", c.r(), c.g());  // "1.0, 0.8"
``````

## Swizzle Functions

`Float2 Float2::swizzle(u32 i0, u32 i1) const` [code]
`Float3 Float2::swizzle(u32 i0, u32 i1, u32 i2) const` [code]
`Float4 Float2::swizzle(u32 i0, u32 i1, u32 i2, u32 i3) const` [code]

Returns a new vector whose components are taken from the given indices. `x` and `y` are at indices 0 and 1 respectively. Similar to GLSL swizzling except that the components are specified by numeric index, and you can't use it to modify the original vector; only to read from it.

``````Float2 v = {4, 5};
StdOut::text() << v.swizzle(1, 0);        // "{5, 4}"
StdOut::text() << v.swizzle(0, 1, 1, 0);  // "{4, 5, 5, 4}"
``````

These functions work correctly in the current version of all major compilers even though they use type punning, which is undefined behavior in standard C++.

## Componentwise Functions

`Float2 clamp(const Float2& v, const Float2& mins, const Float2& maxs)` [code]

Returns a copy of `v` with each component constrained to lie within the range determined by the corresponding components of `mins` and `maxs`.

``````Float2 v = {3, 1.5f};
StdOut::text() << clamp(v, Float2{0, 1}, Float2{1, 2});  // "{1, 1.5}"
StdOut::text() << clamp(v, 0, 1);                        // "{1, 1}"
``````
`Float2 abs(const Float2& a)` [code]

Returns a vector with each component set to the absolute value of the corresponding component of `a`.

``````StdOut::text() << abs(Float2{-2, 3});  // "{2, 3}"
``````
`Float2 pow(const Float2& a, const Float2& b)` [code]

Returns a vector with each component set to the corresponding component of `a` raised to the power of the corresponding component of `b`.

``````StdOut::text() << pow(Float2{1, 2}, Float2{2, 3});  // "{1, 8}"
StdOut::text() << pow(Float2{1, 2}, 2);             // "{1, 4}"
``````
`Float2 min(const Float2& a, const Float2& b)` [code]

Returns a vector with each component set to minimum of the corresponding components of `a` and `b`.

``````StdOut::text() << min(Float2{0, 1}, Float2{1, 0});  // "{0, 0}"
``````
`Float2 max(const Float2& a, const Float2& b)` [code]

Returns a vector with each component set to maximum of the corresponding components of `a` and `b`.

``````StdOut::text() << max(Float2{0, 1}, Float2{1, 0});  // "{1, 1}"
``````

## Rounding Functions

`Float2 roundUp(const Float2& value, float spacing = 1)` [code]
`Float2 roundDown(const Float2& value, float spacing = 1)` [code]
`Float2 roundNearest(const Float2& value, float spacing = 1)` [code]

Returns a vector with each component set to the rounded result of the corresponding component of `vec`. The optional `spacing` argument can be used to round to arbitrary spacings. Most precise when `spacing` is a power of 2.

``````StdOut::text() << roundUp(Float2{-0.3f, 1.4f});   // "{0, 2}"
StdOut::text() << roundDown(Float2{1.8f}, 0.5f);  // "{1.5, 1.5}"
``````
`bool isRounded(const Float2& value, float spacing = 1)` [code]

Returns `true` if every component of `vec` is already rounded. The optional `spacing` argument can be used to round to arbitrary spacings. Most precise when `spacing` is a power of 2.

``````StdOut::text() << isRounded(Float2{1.5f, 2.5f}, 0.5f);  // "true"
``````