Isle
Loading...
Searching...
No Matches
Vector2 Class Reference

[AI] Represents a 2D mathematical vector with floating-point coordinates. More...

#include <vector.h>

Inheritance diagram for Vector2:

Public Member Functions

 Vector2 (float *p_data)
 [AI] Construct a 2D vector using an external float buffer. More...
 
 Vector2 (const float *p_data)
 [AI] Construct a 2D vector from a (likely constant) array of floats without copying (just pointer assignment). More...
 
virtual float * GetData ()
 [AI] Retrieves the mutable in-memory data pointer for this vector. More...
 
virtual const float * GetData () const
 [AI] Retrieves the immutable data pointer for this vector. More...
 
virtual void Clear ()
 [AI] Zeros all elements (sets all coordinates to 0.0). More...
 
virtual float Dot (const float *p_a, const float *p_b) const
 [AI] Compute the dot product of the two float arrays interpreted as vectors of 2 elements. More...
 
virtual float Dot (const Vector2 &p_a, const Vector2 &p_b) const
 [AI] Compute the dot product of two Vector2 objects. More...
 
virtual float Dot (const float *p_a, const Vector2 &p_b) const
 [AI] Compute the dot product given a pointer and a Vector2 (second param). More...
 
virtual float Dot (const Vector2 &p_a, const float *p_b) const
 [AI] Compute the dot product given a Vector2 (first param) and a pointer (second param). More...
 
virtual float LenSquared () const
 [AI] Compute the squared length (magnitude^2) of the vector. More...
 
virtual int Unitize ()
 [AI] Scales the vector so its norm is 1 (unit vector). More...
 
virtual void operator+= (float p_value)
 [AI] In-place add a scalar to all coordinates. More...
 
virtual void operator+= (const float *p_other)
 [AI] In-place add vector elements via float pointer. More...
 
virtual void operator+= (const Vector2 &p_other)
 [AI] In-place add another Vector2. More...
 
virtual void operator-= (const float *p_other)
 [AI] In-place subtraction of vector pointed to by p_other. More...
 
virtual void operator-= (const Vector2 &p_other)
 [AI] In-place subtraction of another Vector2. More...
 
virtual void operator*= (const float *p_other)
 [AI] In-place per-element multiplication of this vector by another array. More...
 
virtual void operator*= (const Vector2 &p_other)
 [AI] In-place per-element multiplication by another Vector2. More...
 
virtual void operator*= (const float &p_value)
 [AI] In-place multiplication by a scalar. More...
 
virtual void operator/= (const float &p_value)
 [AI] In-place scalar division. More...
 
virtual void operator= (const float *p_other)
 [AI] Assigns this vector's elements from a pointer to two floats. More...
 
virtual void operator= (const Vector2 &p_other)
 [AI] Assigns this vector's elements from another Vector2. More...
 
float & operator[] (int idx)
 [AI] Accesses the idx-th float in the vector (0 or 1). More...
 
const float & operator[] (int idx) const
 [AI] Const version of the index operator. More...
 

Protected Member Functions

virtual void AddImpl (const float *p_value)
 [AI] Adds the values pointed to by p_value to this vector. More...
 
virtual void AddImpl (float p_value)
 [AI] Adds a scalar value to every element of this vector. More...
 
virtual void SubImpl (const float *p_value)
 [AI] Subtracts the vector specified by p_value from this vector. More...
 
virtual void MulImpl (const float *p_value)
 [AI] Multiplies this vector by another vector (per element). More...
 
virtual void MulImpl (const float &p_value)
 [AI] Multiplies this vector by a scalar value. More...
 
virtual void DivImpl (const float &p_value)
 [AI] Divides this vector by a scalar value. More...
 
virtual float DotImpl (const float *p_a, const float *p_b) const
 [AI] Computes the dot product of two arrays interpreted as vectors. More...
 
virtual void SetData (float *p_data)
 [AI] Set the internal data pointer to external storage. More...
 
virtual void EqualsImpl (const float *p_data)
 [AI] Assigns values from provided array to this vector. More...
 

Protected Attributes

float * m_data
 [AI] Pointer to externally provided float storage (owned elsewhere). More...
 

Detailed Description

[AI] Represents a 2D mathematical vector with floating-point coordinates.

Provides a flexible base class for 2D, 3D, and 4D specialization. Vector data may be shared externally via an internal pointer rather than fixed storage.

[AI] Vector2 supports polymorphic math (addition, subtraction, multiplication, division, dot product, etc.) through its overridden virtual member functions evaluated per element or for the whole structure. All operations can be dispatched either by a scalar, vector, or float pointer. Serves as a base for Vector3 and Vector4 to provide extensible vector algebra. The memory pointed to by m_data is not owned by Vector2; it is provided and managed externally.

Definition at line 16 of file vector.h.

Constructor & Destructor Documentation

◆ Vector2() [1/2]

Vector2::Vector2 ( float *  p_data)
inline

[AI] Construct a 2D vector using an external float buffer.

Parameters
p_dataPointer to two floats that represent initial vector values; the data is used by reference. [AI]

Definition at line 84 of file vector.h.

◆ Vector2() [2/2]

Vector2::Vector2 ( const float *  p_data)
inline

[AI] Construct a 2D vector from a (likely constant) array of floats without copying (just pointer assignment).

Parameters
p_dataPointer to two floats (const) to use for vector data. [AI]
Attention
[AI] The pointer is cast to non-const. Used in legacy code; may allow accidental mutation of const data in practice. [AI]

Definition at line 91 of file vector.h.

Member Function Documentation

◆ AddImpl() [1/2]

void Vector2::AddImpl ( const float *  p_value)
inlineprotectedvirtual

[AI] Adds the values pointed to by p_value to this vector.

[AI] Adds a 2D float array to the vector data in-place.

Parameters
p_valuePointer to an array containing values to add. [AI]

This is used for vector addition with an array.

Parameters
p_valuePointer to a float array containing the values to add.

[AI] Each component of the input array is added to the corresponding component of the vector. The length must be 2.

Reimplemented in Vector3, and Vector4.

◆ AddImpl() [2/2]

void Vector2::AddImpl ( float  p_value)
inlineprotectedvirtual

[AI] Adds a scalar value to every element of this vector.

[AI] Adds a scalar value to both components of the vector.

Parameters
p_valueThe value to add to each coordinate. [AI]
p_valueThe scalar value to add.

[AI] The scalar is added to both x and y components.

Reimplemented in Vector3, and Vector4.

◆ Clear()

void Vector2::Clear ( )
inlinevirtual

[AI] Zeros all elements (sets all coordinates to 0.0).

[AI] Sets both components of the vector to zero.

[AI] Uses memset to zero the vector.

Reimplemented in Vector3, and Vector4.

◆ DivImpl()

void Vector2::DivImpl ( const float &  p_value)
inlineprotectedvirtual

[AI] Divides this vector by a scalar value.

[AI] Divides both components of the vector by a scalar.

Parameters
p_valueScalar divisor. [AI]
p_valueThe scalar value to divide by.

[AI] Both components are divided by the scalar. No zero check.

Reimplemented in Vector3, and Vector4.

◆ Dot() [1/4]

float Vector2::Dot ( const float *  p_a,
const float *  p_b 
) const
inlinevirtual

[AI] Compute the dot product of the two float arrays interpreted as vectors of 2 elements.

[AI] Computes the dot product of two 2D float arrays.

Parameters
p_aPointer to first vector data. [AI]
p_bPointer to second vector data. [AI]
Returns
Dot product. [AI]
Parameters
p_aPointer to first 2D float array.
p_bPointer to second 2D float array.
Returns
Result of dot product calculation.

[AI] Forwards to DotImpl.

◆ Dot() [2/4]

float Vector2::Dot ( const float *  p_a,
const Vector2 p_b 
) const
inlinevirtual

[AI] Compute the dot product given a pointer and a Vector2 (second param).

[AI] Computes the dot product of a float array and a Vector2.

Parameters
p_aFirst array pointer. [AI]
p_bSecond vector. [AI]
Returns
Dot product. [AI]
Parameters
p_aFloat array representing a 2D vector.
p_bVector2 instance.
Returns
Result of dot product.

[AI] Uses input array and vector's data.

◆ Dot() [3/4]

float Vector2::Dot ( const Vector2 p_a,
const float *  p_b 
) const
inlinevirtual

[AI] Compute the dot product given a Vector2 (first param) and a pointer (second param).

[AI] Computes the dot product of a Vector2 and a float array.

Parameters
p_aFirst vector. [AI]
p_bSecond array pointer. [AI]
Returns
Dot product. [AI]
Parameters
p_aVector2 instance.
p_bFloat array representing 2D data.
Returns
Result of dot product.

[AI] Uses vector's data and input array.

◆ Dot() [4/4]

float Vector2::Dot ( const Vector2 p_a,
const Vector2 p_b 
) const
inlinevirtual

[AI] Compute the dot product of two Vector2 objects.

[AI] Computes the dot product between two Vector2 objects.

Parameters
p_aFirst vector. [AI]
p_bSecond vector. [AI]
Returns
Dot product. [AI]
Parameters
p_aThe first vector.
p_bThe second vector.
Returns
Result of dot product.

[AI] Uses internal data arrays of the Vector2 instances.

◆ DotImpl()

float Vector2::DotImpl ( const float *  p_a,
const float *  p_b 
) const
inlineprotectedvirtual

[AI] Computes the dot product of two arrays interpreted as vectors.

[AI] Computes the dot product of two 2D float arrays.

Parameters
p_aPointer to the first vector. [AI]
p_bPointer to the second vector. [AI]
Returns
The computed dot product. [AI]
Parameters
p_aPointer to first 2D float array.
p_bPointer to second 2D float array.
Returns
The resulting dot product.

[AI] Calculates and returns p_a[0]*p_b[0] + p_a[1]*p_b[1].

Reimplemented in Vector3, and Vector4.

◆ EqualsImpl()

void Vector2::EqualsImpl ( const float *  p_data)
inlineprotectedvirtual

[AI] Assigns values from provided array to this vector.

[AI] Copies the values from a float array into the vector's data.

Parameters
p_dataPointer to array to copy from. [AI]
p_dataPointer to float array containing the values to copy.

[AI] Uses memcpy for fast assignment of both vector components.

Reimplemented in Vector3, and Vector4.

◆ GetData() [1/2]

float * Vector2::GetData ( )
inlinevirtual

[AI] Retrieves the mutable in-memory data pointer for this vector.

[AI] Returns a modifiable pointer to the underlying vector data.

Returns
Pointer to internal vector storage. [AI]
Pointer to internal float array representing the vector.

[AI] This pointer refers to the vector's storage of x and y.

◆ GetData() [2/2]

const float * Vector2::GetData ( ) const
inlinevirtual

[AI] Retrieves the immutable data pointer for this vector.

[AI] Returns a read-only pointer to the underlying vector data.

Returns
Const pointer to internal storage. [AI]
Constant pointer to internal float array.

[AI] Allows access to internal representation for read-only purposes.

◆ LenSquared()

float Vector2::LenSquared ( ) const
inlinevirtual

[AI] Compute the squared length (magnitude^2) of the vector.

[AI] Returns the squared Euclidean length of the vector.

Returns
Squared length. [AI]
The sum of squares of the x and y values.

[AI] Useful for distance comparisons without sqrt.

Reimplemented in Vector3, and Vector4.

◆ MulImpl() [1/2]

void Vector2::MulImpl ( const float &  p_value)
inlineprotectedvirtual

[AI] Multiplies this vector by a scalar value.

[AI] Multiplies both components of the vector by a scalar.

Parameters
p_valueScalar multiplier. [AI]
p_valueThe scalar value to multiply by.

[AI] Both components are multiplied by the scalar.

Reimplemented in Vector3, and Vector4.

◆ MulImpl() [2/2]

void Vector2::MulImpl ( const float *  p_value)
inlineprotectedvirtual

[AI] Multiplies this vector by another vector (per element).

[AI] Multiplies the vector by another 2D float array component-wise.

Parameters
p_valuePointer to the vector to multiply with. [AI]
p_valuePointer to a float array containing the values to multiply by.

[AI] Each component of the vector is multiplied by the corresponding component in the input array.

Reimplemented in Vector3, and Vector4.

◆ operator*=() [1/3]

void Vector2::operator*= ( const float &  p_value)
inlinevirtual

[AI] In-place multiplication by a scalar.

[AI] Multiplies both components by a scalar value.

Parameters
p_valueScalar multiplier. [AI]
p_valueScalar multiplier.

[AI] In-place scaling.

◆ operator*=() [2/3]

void Vector2::operator*= ( const float *  p_other)
inlinevirtual

[AI] In-place per-element multiplication of this vector by another array.

[AI] Multiplies this vector component-wise by values from an array.

Parameters
p_otherPointer to 2 floats. [AI]
p_otherPointer to float array (length 2).

[AI] In-place multiplication.

◆ operator*=() [3/3]

void Vector2::operator*= ( const Vector2 p_other)
inlinevirtual

[AI] In-place per-element multiplication by another Vector2.

[AI] Multiplies this vector component-wise by another Vector2.

Parameters
p_otherSecond vector (rhs). [AI]
p_otherThe Vector2 instance to multiply by.

[AI] In-place multiplication.

◆ operator+=() [1/3]

void Vector2::operator+= ( const float *  p_other)
inlinevirtual

[AI] In-place add vector elements via float pointer.

[AI] Adds values from an array to the vector in-place.

Parameters
p_otherPointer to 2 floats. [AI]
p_otherPointer to float array (length 2).

[AI] Both vector components are incremented.

◆ operator+=() [2/3]

void Vector2::operator+= ( const Vector2 p_other)
inlinevirtual

[AI] In-place add another Vector2.

[AI] Adds another Vector2 to this vector in-place.

Parameters
p_otherOther vector to add. [AI]
p_otherVector2 instance to add.

[AI] Component-wise addition.

◆ operator+=() [3/3]

void Vector2::operator+= ( float  p_value)
inlinevirtual

[AI] In-place add a scalar to all coordinates.

[AI] Adds a scalar to both components of the vector.

Parameters
p_valueScalar to add. [AI]
p_valueThe scalar value to add.

[AI] In-place modification.

◆ operator-=() [1/2]

void Vector2::operator-= ( const float *  p_other)
inlinevirtual

[AI] In-place subtraction of vector pointed to by p_other.

[AI] Subtracts values from an array from the vector in-place.

Parameters
p_otherData to subtract. [AI]
p_otherPointer to float array (length 2).

[AI] Component-wise subtraction.

◆ operator-=() [2/2]

void Vector2::operator-= ( const Vector2 p_other)
inlinevirtual

[AI] In-place subtraction of another Vector2.

[AI] Subtracts another Vector2 from this vector in-place.

Parameters
p_otherVector to subtract. [AI]
p_otherVector2 instance to subtract.

[AI] Component-wise subtraction.

◆ operator/=()

void Vector2::operator/= ( const float &  p_value)
inlinevirtual

[AI] In-place scalar division.

[AI] Divides both components of the vector by a scalar value.

Parameters
p_valueScalar divisor. [AI]
p_valueScalar divisor.

[AI] In-place division, no zero check.

◆ operator=() [1/2]

void Vector2::operator= ( const float *  p_other)
inlinevirtual

[AI] Assigns this vector's elements from a pointer to two floats.

[AI] Assigns vector values from a float array (length 2).

Parameters
p_otherPointer to source array. [AI]
p_otherPointer to the values array.

[AI] Assignment occurs with memcpy.

◆ operator=() [2/2]

void Vector2::operator= ( const Vector2 p_other)
inlinevirtual

[AI] Assigns this vector's elements from another Vector2.

[AI] Assigns vector values from another Vector2.

Parameters
p_otherSource vector. [AI]
p_otherThe Vector2 to copy from.

[AI] Copies both x and y components.

◆ operator[]() [1/2]

float & Vector2::operator[] ( int  idx)
inline

[AI] Accesses the idx-th float in the vector (0 or 1).

Parameters
idxIndex of the float (0 or 1). [AI]
Returns
Reference to the value at index. [AI]

Definition at line 232 of file vector.h.

◆ operator[]() [2/2]

const float & Vector2::operator[] ( int  idx) const
inline

[AI] Const version of the index operator.

Parameters
idxIndex to access (0 or 1). [AI]
Returns
Const reference to value at index. [AI]

Definition at line 239 of file vector.h.

◆ SetData()

void Vector2::SetData ( float *  p_data)
inlineprotectedvirtual

[AI] Set the internal data pointer to external storage.

[AI] Sets the internal data pointer of the vector to the provided memory.

Parameters
p_dataData pointer to use for this vector. [AI]
p_dataPointer to external float array to use as vector data.

[AI] Allows reusing memory or pointing to external data.

◆ SubImpl()

void Vector2::SubImpl ( const float *  p_value)
inlineprotectedvirtual

[AI] Subtracts the vector specified by p_value from this vector.

[AI] Subtracts a 2D float array from the vector data in-place.

Parameters
p_valuePointer to array to subtract. [AI]
p_valuePointer to a float array containing the values to subtract.

[AI] Each component of the input array is subtracted from the corresponding component of the vector. The length must be 2.

Reimplemented in Vector3, and Vector4.

◆ Unitize()

int Vector2::Unitize ( )
inlinevirtual

[AI] Scales the vector so its norm is 1 (unit vector).

[AI] Normalizes the vector to have unit length (if length > 0).

Returns 0 if successful, nonzero on failure (e.g. if length is 0).

Returns
Unitization status. [AI]
0 on success, -1 if length is zero.

[AI] Divides components by the vector's length. Safe against zero division.

Member Data Documentation

◆ m_data

float* Vector2::m_data
protected

[AI] Pointer to externally provided float storage (owned elsewhere).

Definition at line 77 of file vector.h.


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