Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
KatanaEngine::Vector2 Class Reference

Defines a vector with 2 components (x and y). More...

#include <Vector2.h>

Public Member Functions

 Vector2 (const float x=0, const float y=0)
 Instantiates a new Vector2 object.
 
float LengthSquared () const
 Calculates the length of the vector squared.
 
float Length () const
 Calculates the length of the vector.
 
void Set (const float x, const float y)
 Sets the components of the vector.
 
void Set (const Vector2 vector)
 Sets the components of the vector.
 
void Normalize ()
 Resize a vector to a length of one unit. If the starting vector is the zero vector, the call will be ignored.
 
bool IsZero () const
 Determines if the vector is the zero vector.
 
float DotProduct (const Vector2 &vector) const
 Calculates the dot product of two vectors.
 
float CrossProduct (const Vector2 &vector) const
 Calculates the cross product between two vectors.
 
Vector2 Left ()
 Calculates the left-hand orthogonal vector.
 
Vector2 Right ()
 Calculates the right-hand orthogonal vector.
 
const Point ToPoint () const
 Converts the vector into a point.
 
std::string ToString () const
 Gets a string representation of the vector.
 
void Display () const
 Prints the vector to the console.
 
Vector2operator= (const Vector2 &vector)
 Assigns the reference of a vector.
 
Vector2operator+= (const Vector2 &vector)
 Adds a vector.
 
Vector2operator-= (const Vector2 &vector)
 Subtracts a vector.
 
Vector2operator*= (const float scalar)
 Multiplies by a scalar.
 
Vector2operator/= (const float scalar)
 Divides by a scalar.
 
const Vector2 operator- () const
 Negates the vector.
 
const Vector2 operator+ (const Vector2 &vector) const
 Adds two vectors.
 
const Vector2 operator- (const Vector2 &vector) const
 Subtracts a vector from another.
 
const Vector2 operator* (const float scalar) const
 Multiplies a vector by a scalar.
 
const Vector2 operator/ (const float scalar) const
 Divides a vector by a scalar.
 
bool operator== (const Vector2 &vector) const
 Determines if two vectors are equal.
 
bool operator!= (const Vector2 &vector) const
 Determines if two vectors are not equal.
 

Static Public Member Functions

static float Distance (const Vector2 &vector1, const Vector2 &vector2)
 Calculates the distance between two vectors.
 
static float DistanceSquared (const Vector2 &vector1, const Vector2 &vector2)
 Calculates the distance squared between two vectors.
 
static Vector2 Lerp (const Vector2 &start, const Vector2 &end, const float value)
 Linearly interpolate between two vectors.
 
static Vector2 GetRandom (bool normalize=false)
 Creates a random vector.
 

Public Attributes

float X = 0
 The x-coordinate of the vector.
 
float Y = 0
 The y-coordinate of the vector.
 

Static Public Attributes

static const Vector2 ZERO = Vector2(0, 0)
 A vector with both of its components set to zero.
 
static const Vector2 ONE = Vector2(1, 1)
 A vector with both of its components set to one.
 
static const Vector2 UNIT_X = Vector2(1, 0)
 A unit vector on the x-axis.
 
static const Vector2 UNIT_Y = Vector2(0, 1)
 A unit vector on the y-axis.
 

Detailed Description

Defines a vector with 2 components (x and y).

Definition at line 20 of file Vector2.h.

Constructor & Destructor Documentation

◆ Vector2()

KatanaEngine::Vector2::Vector2 ( const float x = 0,
const float y = 0 )

Instantiates a new Vector2 object.

Parameters
xThe X component
yThe Y component

Definition at line 24 of file Vector2.cpp.

Member Function Documentation

◆ CrossProduct()

float KatanaEngine::Vector2::CrossProduct ( const Vector2 & vector) const

Calculates the cross product between two vectors.

Parameters
vectorThe other vector.
Returns
The calculated cross product.

Definition at line 55 of file Vector2.cpp.

◆ Display()

void KatanaEngine::Vector2::Display ( ) const
inline

Prints the vector to the console.

Definition at line 122 of file Vector2.h.

◆ Distance()

float KatanaEngine::Vector2::Distance ( const Vector2 & vector1,
const Vector2 & vector2 )
static

Calculates the distance between two vectors.

Parameters
vector1The first vector.
vector2The second vector.
Returns
The calculated distance.

Definition at line 60 of file Vector2.cpp.

◆ DistanceSquared()

float KatanaEngine::Vector2::DistanceSquared ( const Vector2 & vector1,
const Vector2 & vector2 )
static

Calculates the distance squared between two vectors.

Parameters
vector1The first vector.
vector2The second vector.
Returns
The calculated distance squared.

Definition at line 65 of file Vector2.cpp.

◆ DotProduct()

float KatanaEngine::Vector2::DotProduct ( const Vector2 & vector) const

Calculates the dot product of two vectors.

Parameters
vectorThe other vector.
Returns
The calculated dot product.
Remarks
If the two vectors are unit vectors, the dot product returns a floating point value between -1 and 1 that can be used to determine some properties of the angle between two vectors. For example, it can show whether the vectors are orthogonal, parallel, or have an acute or obtuse angle between them.

Definition at line 50 of file Vector2.cpp.

◆ GetRandom()

Vector2 KatanaEngine::Vector2::GetRandom ( bool normalize = false)
static

Creates a random vector.

Parameters
normalizeSpecify true to generate a unit vector. False will generate a vector with a random length.
Returns
The random vector.

Definition at line 78 of file Vector2.cpp.

◆ IsZero()

bool KatanaEngine::Vector2::IsZero ( ) const
inline

Determines if the vector is the zero vector.

Returns
Returns true if both components are zero, false otherwise.

Definition at line 63 of file Vector2.h.

◆ Left()

Vector2 KatanaEngine::Vector2::Left ( )
inline

Calculates the left-hand orthogonal vector.

Returns
Returns the left vector.

Definition at line 107 of file Vector2.h.

◆ Length()

float KatanaEngine::Vector2::Length ( ) const

Calculates the length of the vector.

Returns
The length of the vector.

Definition at line 35 of file Vector2.cpp.

◆ LengthSquared()

float KatanaEngine::Vector2::LengthSquared ( ) const

Calculates the length of the vector squared.

Returns
float The length of the vector squared.

Definition at line 30 of file Vector2.cpp.

◆ Lerp()

Vector2 KatanaEngine::Vector2::Lerp ( const Vector2 & start,
const Vector2 & end,
const float value )
static

Linearly interpolate between two vectors.

Parameters
startThe result when the value is zero.
endThe result when the value is one.
valueA value between 0 and 1 that will indicate the resulting weight between start and end.
Returns
Returns the interpolated vector between start and end.

Definition at line 70 of file Vector2.cpp.

◆ Normalize()

void KatanaEngine::Vector2::Normalize ( )

Resize a vector to a length of one unit. If the starting vector is the zero vector, the call will be ignored.

Definition at line 40 of file Vector2.cpp.

◆ operator!=()

bool KatanaEngine::Vector2::operator!= ( const Vector2 & vector) const

Determines if two vectors are not equal.

Parameters
vectorThe vector to compare.
Returns
Returns true if the vectors are not equal, false otherwise.

Definition at line 164 of file Vector2.cpp.

◆ operator*()

const Vector2 KatanaEngine::Vector2::operator* ( const float scalar) const

Multiplies a vector by a scalar.

Parameters
scalarThe scalar to multiply by.
Returns
Returns the resulting vector.

Definition at line 149 of file Vector2.cpp.

◆ operator*=()

Vector2 & KatanaEngine::Vector2::operator*= ( const float scalar)

Multiplies by a scalar.

Parameters
scalarThe scalar to multiply by.
Returns
Returns the resulting vector.

Definition at line 123 of file Vector2.cpp.

◆ operator+()

const Vector2 KatanaEngine::Vector2::operator+ ( const Vector2 & vector) const

Adds two vectors.

Parameters
vectorThe vector to add.
Returns
Returns the resulting vector.

Definition at line 139 of file Vector2.cpp.

◆ operator+=()

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

Adds a vector.

Parameters
vectorThe vector to add.
Returns
Returns the resulting vector.

Definition at line 107 of file Vector2.cpp.

◆ operator-() [1/2]

const Vector2 KatanaEngine::Vector2::operator- ( ) const
inline

Negates the vector.

Returns
Returns the resulting vector.

Definition at line 152 of file Vector2.h.

◆ operator-() [2/2]

const Vector2 KatanaEngine::Vector2::operator- ( const Vector2 & vector) const

Subtracts a vector from another.

Parameters
vectorThe vector to subtract.
Returns
Returns the resulting vector.

Definition at line 144 of file Vector2.cpp.

◆ operator-=()

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

Subtracts a vector.

Parameters
vectorThe vector to subtract.
Returns
Returns the resulting vector.

Definition at line 115 of file Vector2.cpp.

◆ operator/()

const Vector2 KatanaEngine::Vector2::operator/ ( const float scalar) const

Divides a vector by a scalar.

Parameters
scalarThe scalar to divide by.
Returns
Returns the resulting vector.

Definition at line 154 of file Vector2.cpp.

◆ operator/=()

Vector2 & KatanaEngine::Vector2::operator/= ( const float scalar)

Divides by a scalar.

Parameters
scalarThe scalar to divide by.
Returns
Returns the resulting vector.

Definition at line 131 of file Vector2.cpp.

◆ operator=()

Vector2 & KatanaEngine::Vector2::operator= ( const Vector2 & vector)

Assigns the reference of a vector.

Parameters
vectorThe reference vector.
Returns
Returns the resulting vector.

Definition at line 96 of file Vector2.cpp.

◆ operator==()

bool KatanaEngine::Vector2::operator== ( const Vector2 & vector) const

Determines if two vectors are equal.

Parameters
vectorThe vector to compare.
Returns
Returns true if the vectors are equal, false otherwise.

Definition at line 159 of file Vector2.cpp.

◆ Right()

Vector2 KatanaEngine::Vector2::Right ( )
inline

Calculates the right-hand orthogonal vector.

Returns
Returns the right vector.

Definition at line 111 of file Vector2.h.

◆ Set() [1/2]

void KatanaEngine::Vector2::Set ( const float x,
const float y )
inline

Sets the components of the vector.

Parameters
xThe X component.
yThe Y component.

Definition at line 49 of file Vector2.h.

◆ Set() [2/2]

void KatanaEngine::Vector2::Set ( const Vector2 vector)
inline

Sets the components of the vector.

Parameters
vectorThe vector whose components to copy.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 55 of file Vector2.h.

◆ ToPoint()

const Point KatanaEngine::Vector2::ToPoint ( ) const

Converts the vector into a point.

Returns
Returns the closest point to the vector displacement.

Definition at line 169 of file Vector2.cpp.

◆ ToString()

std::string KatanaEngine::Vector2::ToString ( ) const

Gets a string representation of the vector.

Returns
Returns a string displaying the components of the vector.

Definition at line 89 of file Vector2.cpp.

Member Data Documentation

◆ ONE

const Vector2 KatanaEngine::Vector2::ONE = Vector2(1, 1)
static

A vector with both of its components set to one.

Definition at line 32 of file Vector2.h.

◆ UNIT_X

const Vector2 KatanaEngine::Vector2::UNIT_X = Vector2(1, 0)
static

A unit vector on the x-axis.

Definition at line 33 of file Vector2.h.

◆ UNIT_Y

const Vector2 KatanaEngine::Vector2::UNIT_Y = Vector2(0, 1)
static

A unit vector on the y-axis.

Definition at line 34 of file Vector2.h.

◆ X

float KatanaEngine::Vector2::X = 0

The x-coordinate of the vector.

Definition at line 186 of file Vector2.h.

◆ Y

float KatanaEngine::Vector2::Y = 0

The y-coordinate of the vector.

Definition at line 187 of file Vector2.h.

◆ ZERO

const Vector2 KatanaEngine::Vector2::ZERO = Vector2(0, 0)
static

A vector with both of its components set to zero.

Definition at line 31 of file Vector2.h.


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