Claire's Game Engine
Full C++ Engine using OpenGL
Loading...
Searching...
No Matches
Vector2 Struct Reference

A class representing a 2D vector. More...

#include <Vector2.h>

Public Member Functions

 Vector2 ()
 Default constructor. Initializes vector to (0, 0).
 
 Vector2 (float x, float y)
 Initializes the vector with specified x and y values.
 
 Vector2 (float x)
 Initializes both x and y with the same value.
 
float Magnitude () const
 Returns the magnitude (length) of the vector.
 
float SqrLength () const
 Returns the squared length of the vector (avoids square root for performance).
 
void Normalize ()
 Normalizes the vector to unit length. If the vector is zero-length, it remains unchanged.
 
Vector2 Normalized ()
 Returns a new unit-length vector in the same direction. If the original vector has zero length, returns Vector2::ZERO.
 
float Dot (const Vector2 &left, const Vector2 &right) const
 Computes the dot product of two vectors.
 
float Cross (const Vector2 &vector) const
 Computes the 2D cross product between this and another vector. The result is a scalar equal to the Z-component of a 3D cross product.
 
std::string ToString () const
 Converts the vector to a readable string format.
 
void operator+= (const Vector2 &vector)
 
void operator-= (const Vector2 &vector)
 
void operator*= (const float value)
 
void operator/= (const float value)
 

Public Attributes

float x = 0
 
float y = 0
 

Static Public Attributes

static const Vector2 ZERO
 
static const Vector2 ONE
 

Friends

bool operator== (const Vector2 &left, const Vector2 &right)
 
Vector2 operator+ (const Vector2 &left, const Vector2 &right)
 
Vector2 operator- (const Vector2 &left, const Vector2 &right)
 
Vector2 operator* (const float value, const Vector2 &vector)
 
Vector2 operator* (const Vector2 &vector, const float value)
 
Vector2 operator/ (const Vector2 &vector, const float value)
 

Detailed Description

A class representing a 2D vector.

It supports vector arithmetic, geometric computations such as magnitude, normalization, dot and cross products, and includes operator overloads for intuitive use.

Constructor & Destructor Documentation

◆ Vector2() [1/3]

Vector2::Vector2 ( )

Default constructor. Initializes vector to (0, 0).

◆ Vector2() [2/3]

Vector2::Vector2 ( float x,
float y )

Initializes the vector with specified x and y values.

Parameters
xThe x component.
yThe y component.

◆ Vector2() [3/3]

Vector2::Vector2 ( float x)

Initializes both x and y with the same value.

Parameters
xThe value to assign to both x and y.

Member Function Documentation

◆ Cross()

float Vector2::Cross ( const Vector2 & vector) const

Computes the 2D cross product between this and another vector. The result is a scalar equal to the Z-component of a 3D cross product.

Parameters
vectorThe other vector.

◆ Dot()

float Vector2::Dot ( const Vector2 & left,
const Vector2 & right ) const

Computes the dot product of two vectors.

Parameters
leftThe first vector.
rightThe second vector.
Returns
A scalar representing how aligned the vectors are.

◆ Magnitude()

float Vector2::Magnitude ( ) const

Returns the magnitude (length) of the vector.

◆ Normalize()

void Vector2::Normalize ( )

Normalizes the vector to unit length. If the vector is zero-length, it remains unchanged.

◆ Normalized()

Vector2 Vector2::Normalized ( )

Returns a new unit-length vector in the same direction. If the original vector has zero length, returns Vector2::ZERO.

◆ operator*=()

void Vector2::operator*= ( const float value)

◆ operator+=()

void Vector2::operator+= ( const Vector2 & vector)

◆ operator-=()

void Vector2::operator-= ( const Vector2 & vector)

◆ operator/=()

void Vector2::operator/= ( const float value)

◆ SqrLength()

float Vector2::SqrLength ( ) const

Returns the squared length of the vector (avoids square root for performance).

◆ ToString()

std::string Vector2::ToString ( ) const

Converts the vector to a readable string format.

Returns
String in format (x , y).

Friends And Related Symbol Documentation

◆ operator* [1/2]

Vector2 operator* ( const float value,
const Vector2 & vector )
friend

◆ operator* [2/2]

Vector2 operator* ( const Vector2 & vector,
const float value )
friend

◆ operator+

Vector2 operator+ ( const Vector2 & left,
const Vector2 & right )
friend

◆ operator-

Vector2 operator- ( const Vector2 & left,
const Vector2 & right )
friend

◆ operator/

Vector2 operator/ ( const Vector2 & vector,
const float value )
friend

◆ operator==

bool operator== ( const Vector2 & left,
const Vector2 & right )
friend

Member Data Documentation

◆ ONE

const Vector2 Vector2::ONE
static

◆ x

float Vector2::x = 0

◆ y

float Vector2::y = 0

◆ ZERO

const Vector2 Vector2::ZERO
static

The documentation for this struct was generated from the following files: