![]() |
ProtoTracer
1.0
Real-time 3D rendering and animation engine
|
Represents a 2D vector (X, Y) and provides methods for vector arithmetic. More...
#include <Vector2D.h>
Public Member Functions | |
Vector2D () | |
Constructs a default Vector2D with X = 0 and Y = 0. | |
Vector2D (const Vector2D &vector) | |
Copy constructor. Initializes this vector with the same values as another Vector2D . | |
Vector2D (const float &X, const float &Y) | |
Constructs a Vector2D using specified float components. | |
Vector2D (const Vector3D &vector) | |
Constructs a Vector2D from a Vector3D , discarding the Z-component. | |
Vector2D | Absolute () const |
Returns a vector with the absolute value of each component. | |
Vector2D | Normal () const |
Computes the squared magnitude of the vector (X^2 + Y^2). | |
Vector2D | Add (const Vector2D &vector) const |
Adds this vector to another Vector2D component-wise. | |
Vector2D | Subtract (const Vector2D &vector) const |
Subtracts another Vector2D from this vector component-wise. | |
Vector2D | Multiply (const Vector2D &vector) const |
Multiplies this vector by another Vector2D component-wise. | |
Vector2D | Divide (const Vector2D &vector) const |
Divides this vector by another Vector2D component-wise. | |
Vector2D | Multiply (const float &scalar) const |
Scales this vector by a float. | |
Vector2D | Divide (const float &scalar) const |
Divides this vector by a scalar. | |
float | CrossProduct (const Vector2D &vector) const |
Calculates the 2D cross product of this vector with another. | |
Vector2D | UnitCircle () const |
Normalizes this vector such that its magnitude is 1 (if non-zero). | |
Vector2D | Constrain (const float &minimum, const float &maximum) const |
Constrains each component of this vector between the specified minimum and maximum. | |
Vector2D | Constrain (const Vector2D &minimum, const Vector2D &maximum) const |
Constrains each component of this vector between the corresponding components of two other Vector2D objects. | |
Vector2D | Minimum (const Vector2D &v) const |
Computes the minimum components between this vector and another Vector2D . | |
Vector2D | Maximum (const Vector2D &v) const |
Computes the maximum components between this vector and another Vector2D . | |
Vector2D | Rotate (const float &angle, const Vector2D &offset) const |
Rotates this vector by a specified angle (in degrees or radians) around a given offset. | |
bool | CheckBounds (const Vector2D &minimum, const Vector2D &maximum) const |
Checks if this vector lies within the bounds of two other Vector2D objects. | |
float | Magnitude () const |
Computes the magnitude (length) of this vector using the formula sqrt(X^2 + Y^2). | |
float | DotProduct (const Vector2D &vector) const |
Computes the dot product of this vector with another Vector2D . | |
float | CalculateEuclideanDistance (const Vector2D &vector) const |
Calculates the Euclidean distance between this vector and another Vector2D . | |
bool | IsEqual (const Vector2D &vector) const |
Checks if this vector is equal to another Vector2D component-wise. | |
String | ToString () const |
Converts the vector to a string representation. | |
bool | operator== (const Vector2D &vector) const |
Equality operator. Checks if two Vector2D s are equal (component-wise). | |
bool | operator!= (const Vector2D &vector) const |
Inequality operator. Checks if two Vector2D s differ (component-wise). | |
Vector2D | operator= (const Vector2D &vector) |
Assignment operator. Copies another Vector2D into this one. | |
Vector2D | operator= (const Vector3D &vector) |
Assignment operator. Copies a Vector3D into this Vector2D , discarding Z. | |
Vector2D | operator+ (const Vector2D &vector) const |
Addition operator. Adds two vectors component-wise. | |
Vector2D | operator- (const Vector2D &vector) const |
Subtraction operator. Subtracts two vectors component-wise. | |
Vector2D | operator* (const Vector2D &vector) const |
Multiplication operator. Multiplies two vectors component-wise. | |
Vector2D | operator/ (const Vector2D &vector) const |
Division operator. Divides two vectors component-wise. | |
Vector2D | operator* (const float &value) const |
Multiplication operator by a float scalar (on the right). | |
Vector2D | operator/ (const float &value) const |
Division operator by a float scalar. | |
Static Public Member Functions | |
static Vector2D | Normal (const Vector2D &vector) |
Returns the squared magnitude of a given vector (X^2 + Y^2) as a Vector2D. | |
static Vector2D | Add (const Vector2D &v1, const Vector2D &v2) |
Adds two vectors (component-wise). | |
static Vector2D | Subtract (const Vector2D &v1, const Vector2D &v2) |
Subtracts one vector from another (component-wise). | |
static Vector2D | Multiply (const Vector2D &v1, const Vector2D &v2) |
Multiplies two vectors component-wise. | |
static Vector2D | Divide (const Vector2D &v1, const Vector2D &v2) |
Divides two vectors component-wise. | |
static Vector2D | Multiply (const Vector2D &vector, const float &scalar) |
Scales a Vector2D by a float, component-wise. | |
static Vector2D | Multiply (const float &scalar, const Vector2D &vector) |
Scales a Vector2D by a float, component-wise (scalar on the left). | |
static Vector2D | Divide (const Vector2D &vector, const float &scalar) |
Divides a Vector2D by a scalar, component-wise. | |
static float | CrossProduct (const Vector2D &v1, const Vector2D &v2) |
Computes the 2D cross product of two vectors, returning a scalar (Z-component in 3D). | |
static float | DotProduct (const Vector2D &v1, const Vector2D &v2) |
Computes the dot product of two Vector2D s. | |
static float | CalculateEuclideanDistance (const Vector2D &v1, const Vector2D &v2) |
Calculates the Euclidean distance between two Vector2D s. | |
static bool | IsEqual (const Vector2D &v1, const Vector2D &v2) |
Checks if two Vector2D s are equal component-wise. | |
static Vector2D | Minimum (const Vector2D &v1, const Vector2D &v2) |
Returns a new vector with the minimum components of two Vector2D s. | |
static Vector2D | Maximum (const Vector2D &v1, const Vector2D &v2) |
Returns a new vector with the maximum components of two Vector2D s. | |
static Vector2D | LERP (const Vector2D &start, const Vector2D &finish, const float &ratio) |
Performs linear interpolation between two Vector2D s. | |
static Vector2D | DegreesToRadians (const Vector2D °rees) |
Converts a vector of degrees to radians (component-wise). | |
static Vector2D | RadiansToDegrees (const Vector2D &radians) |
Converts a vector of radians to degrees (component-wise). | |
static bool | LineSegmentsIntersect (const Vector2D &p1, const Vector2D &p2, const Vector2D &q1, const Vector2D &q2) |
Checks if two line segments defined by (p1, p2) and (q1, q2) intersect in 2D space. | |
Public Attributes | |
float | X |
The X-component of the 2D vector. | |
float | Y |
The Y-component of the 2D vector. | |
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
The Vector2D
class defines basic 2D vector operations such as addition, subtraction, multiplication, division, dot product, cross product, and geometric queries. It also includes static functions to perform operations on multiple Vector2D
objects without requiring an instance.
Definition at line 27 of file Vector2D.h.
Vector2D | ( | ) |
Constructs a default Vector2D
with X = 0 and Y = 0.
Definition at line 3 of file Vector2D.cpp.
Referenced by Add(), CalculateEuclideanDistance(), CalculateEuclideanDistance(), DegreesToRadians(), Divide(), Divide(), Multiply(), Multiply(), Multiply(), RadiansToDegrees(), Rotate(), Subtract(), and UnitCircle().
Copy constructor. Initializes this vector with the same values as another Vector2D
.
vector | The Vector2D to copy from. |
Definition at line 5 of file Vector2D.cpp.
Constructs a Vector2D
using specified float components.
X | The X-component of the vector. |
Y | The Y-component of the vector. |
Definition at line 7 of file Vector2D.cpp.
Constructs a Vector2D
from a Vector3D
, discarding the Z-component.
vector | The Vector3D to convert (Z is ignored). |
Definition at line 9 of file Vector2D.cpp.
Vector2D Absolute | ( | ) | const |
Returns a vector with the absolute value of each component.
Vector2D
where each component is abs(X)
and abs(Y)
. Definition at line 11 of file Vector2D.cpp.
Adds two vectors (component-wise).
Vector2D
representing the sum. Definition at line 203 of file Vector2D.cpp.
References Vector2D().
Adds this vector to another Vector2D
component-wise.
vector | The vector to add. |
Vector2D
representing the sum. Definition at line 32 of file Vector2D.cpp.
Referenced by operator+().
Calculates the Euclidean distance between two Vector2D
s.
v1
and v2
. Definition at line 239 of file Vector2D.cpp.
References Magnitude(), and Vector2D().
Calculates the Euclidean distance between this vector and another Vector2D
.
vector | The other Vector2D . |
Definition at line 150 of file Vector2D.cpp.
References Magnitude(), Vector2D(), X, and Y.
Referenced by Fisheye::ApplyEffect(), and Magnet::ApplyEffect().
Constrains each component of this vector between the specified minimum and maximum.
minimum | The lower bound. |
maximum | The upper bound. |
Vector2D
with each component constrained. Definition at line 96 of file Vector2D.cpp.
References Mathematics::Constrain(), X, and Y.
Constrains each component of this vector between the corresponding components of two other Vector2D
objects.
minimum | The lower bound vector. |
maximum | The upper bound vector. |
Vector2D
with each component constrained. Definition at line 103 of file Vector2D.cpp.
References Mathematics::Constrain(), X, and Y.
Calculates the 2D cross product of this vector with another.
For 2D vectors, the "cross product" reduces to a scalar, which is conceptually the Z-component of the 3D cross product.
vector | The other Vector2D . |
Definition at line 80 of file Vector2D.cpp.
Converts a vector of degrees to radians (component-wise).
degrees | The vector in degrees. |
Vector2D
in radians. Definition at line 189 of file Vector2D.cpp.
References Mathematics::MPID180, and Vector2D().
Divides this vector by a scalar.
scalar | The scalar divisor. |
Vector2D
after division by scalar
. Definition at line 70 of file Vector2D.cpp.
References Mathematics::EPSILON, Mathematics::IsClose(), X, and Y.
Divides two vectors component-wise.
Vector2D
representing the quotient. Definition at line 215 of file Vector2D.cpp.
References Vector2D().
Divides this vector by another Vector2D
component-wise.
vector | The vector to divide by. |
Vector2D
representing the quotient. Definition at line 53 of file Vector2D.cpp.
Referenced by AudioReactiveGradient::AudioReactiveGradient(), operator/(), operator/(), Oscilloscope::Oscilloscope(), AudioReactiveGradient::SetSize(), Oscilloscope::SetSize(), SpectrumAnalyzer::SetSize(), and SpectrumAnalyzer::SpectrumAnalyzer().
Divides a Vector2D
by a scalar, component-wise.
vector | The Vector2D . |
scalar | The scalar divisor. |
Vector2D
after the division. Definition at line 227 of file Vector2D.cpp.
References Vector2D().
Checks if two Vector2D
s are equal component-wise.
true
if equal, otherwise false
. Definition at line 245 of file Vector2D.cpp.
Checks if this vector is equal to another Vector2D
component-wise.
vector | The Vector2D to compare. |
true
if equal, otherwise false
. Definition at line 156 of file Vector2D.cpp.
Referenced by operator!=(), and operator==().
|
static |
Checks if two line segments defined by (p1, p2) and (q1, q2) intersect in 2D space.
p1 | Start of the first segment. |
p2 | End of the first segment. |
q1 | Start of the second segment. |
q2 | End of the second segment. |
true
if the segments intersect, otherwise false
. Definition at line 249 of file Vector2D.cpp.
References Mathematics::EPSILON.
float Magnitude | ( | ) | const |
Computes the magnitude (length) of this vector using the formula sqrt(X^2 + Y^2).
Definition at line 142 of file Vector2D.cpp.
References Mathematics::Sqrt(), X, and Y.
Referenced by CalculateEuclideanDistance(), CalculateEuclideanDistance(), SpiralMaterial::GetRGB(), Normal(), and UnitCircle().
Computes the maximum components between this vector and another Vector2D
.
v | The other Vector2D . |
Vector2D
taking the maximum of X and Y components. Definition at line 117 of file Vector2D.cpp.
Referenced by BoundingBox2D::UpdateBounds().
Computes the minimum components between this vector and another Vector2D
.
v | The other Vector2D . |
Vector2D
taking the minimum of X and Y components. Definition at line 110 of file Vector2D.cpp.
Referenced by BoundingBox2D::UpdateBounds().
Scales this vector by a float.
scalar | The scalar value. |
Vector2D
scaled by scalar
. Definition at line 60 of file Vector2D.cpp.
References Mathematics::EPSILON, Mathematics::IsClose(), X, and Y.
Scales a Vector2D
by a float, component-wise (scalar on the left).
scalar | The scaling factor. |
vector | The Vector2D . |
Vector2D
scaled by scalar
. Definition at line 223 of file Vector2D.cpp.
References Vector2D().
Multiplies two vectors component-wise.
Vector2D
representing the product. Definition at line 211 of file Vector2D.cpp.
References Vector2D().
Multiplies this vector by another Vector2D
component-wise.
vector | The vector to multiply by. |
Vector2D
representing the product. Definition at line 46 of file Vector2D.cpp.
Referenced by Normal(), operator*(), operator*(), and ProtogenProject::UpdateFace().
Scales a Vector2D
by a float, component-wise.
vector | The Vector2D . |
scalar | The scaling factor. |
Vector2D
scaled by scalar
. Definition at line 219 of file Vector2D.cpp.
References Vector2D().
Vector2D Normal | ( | ) | const |
Computes the squared magnitude of the vector (X^2 + Y^2).
Vector2D
whose components are squared. (Typically used as an internal helper.) Definition at line 18 of file Vector2D.cpp.
References Mathematics::EPSILON, Mathematics::IsClose(), Magnitude(), and Multiply().
Referenced by Normal().
Returns the squared magnitude of a given vector (X^2 + Y^2) as a Vector2D.
Note: This function name can be misleading since "Normal" typically implies a normalized vector. Here, it is used as in "square of the magnitude" or "norm".
vector | The input Vector2D . |
Vector2D
containing squared values of each component. Definition at line 197 of file Vector2D.cpp.
References Normal().
Inequality operator. Checks if two Vector2D
s differ (component-wise).
vector | The vector to compare with. |
true
if not equal, otherwise false
. Definition at line 273 of file Vector2D.cpp.
References IsEqual().
Multiplication operator by a float scalar (on the right).
value | The scalar factor. |
Vector2D
scaled by value
. Definition at line 307 of file Vector2D.cpp.
References Multiply().
Multiplication operator. Multiplies two vectors component-wise.
vector | The right-hand side Vector2D . |
Vector2D
representing the product. Definition at line 299 of file Vector2D.cpp.
References Multiply().
Addition operator. Adds two vectors component-wise.
vector | The right-hand side Vector2D . |
Vector2D
representing the sum. Definition at line 291 of file Vector2D.cpp.
References Add().
Subtraction operator. Subtracts two vectors component-wise.
vector | The right-hand side Vector2D . |
Vector2D
representing the difference. Definition at line 295 of file Vector2D.cpp.
References Subtract().
Division operator by a float scalar.
value | The scalar divisor. |
Vector2D
after division by value
. Definition at line 311 of file Vector2D.cpp.
References Divide().
Division operator. Divides two vectors component-wise.
vector | The right-hand side Vector2D (divisor). |
Vector2D
representing the quotient. Definition at line 303 of file Vector2D.cpp.
References Divide().
Assignment operator. Copies a Vector3D
into this Vector2D
, discarding Z.
vector | The Vector3D to copy from. |
Vector2D
. Definition at line 284 of file Vector2D.cpp.
References X, Vector3D::X, Y, and Vector3D::Y.
Equality operator. Checks if two Vector2D
s are equal (component-wise).
vector | The vector to compare with. |
true
if equal, otherwise false
. Definition at line 269 of file Vector2D.cpp.
References IsEqual().
Converts a vector of radians to degrees (component-wise).
radians | The vector in radians. |
Vector2D
in degrees. Definition at line 193 of file Vector2D.cpp.
References Mathematics::M180DPI, and Vector2D().
Rotates this vector by a specified angle (in degrees or radians) around a given offset.
angle | The angle of rotation. |
offset | The origin about which to rotate. |
Vector2D
representing the rotated vector. Definition at line 124 of file Vector2D.cpp.
References Mathematics::MPID180, Vector2D(), X, and Y.
Referenced by AudioReactiveGradient::GetRGB(), Oscilloscope::GetRGB(), SpectrumAnalyzer::GetRGB(), Image::GetRGB(), and VectorField2D::GetVectorAtPosition().
Subtracts one vector from another (component-wise).
Vector2D
representing the difference. Definition at line 207 of file Vector2D.cpp.
References Vector2D().
Subtracts another Vector2D
from this vector component-wise.
vector | The vector to subtract. |
Vector2D
representing the difference. Definition at line 39 of file Vector2D.cpp.
Referenced by operator-().
String ToString | ( | ) | const |
Converts the vector to a string representation.
String
in the format "(X, Y)". Definition at line 160 of file Vector2D.cpp.
References Mathematics::DoubleToCleanString(), X, and Y.
Referenced by Triangle2D::ToString().
Vector2D UnitCircle | ( | ) | const |
Normalizes this vector such that its magnitude is 1 (if non-zero).
Definition at line 84 of file Vector2D.cpp.
References Mathematics::EPSILON, Mathematics::IsClose(), Magnitude(), Vector2D(), X, and Y.
float X |
The X-component of the 2D vector.
Definition at line 29 of file Vector2D.h.
Referenced by Absolute(), Add(), ObjectAlign::AlignObjects(), ObjectAlign::AlignObjectsNoScale(), HeadsUpDisplay::ApplyEffect(), Fisheye::ApplyEffect(), Magnet::ApplyEffect(), PhaseOffsetR::ApplyEffect(), PhaseOffsetY::ApplyEffect(), BoundingBox2D::BoundingBox2D(), CalculateEuclideanDistance(), CheckBounds(), Rasterizer::CheckRasterPixel(), Rasterizer::CheckRasterPixelAccel(), Constrain(), Constrain(), BoundingBox2D::Contains(), Node::CreateChildNodes(), CrossProduct(), Triangle2D::DidIntersectSAT(), Divide(), Divide(), DotProduct(), HeadsUpDisplay::EnableBitFaceRender(), AudioReactiveGradient::GetRGB(), Oscilloscope::GetRGB(), SpectrumAnalyzer::GetRGB(), Image::GetRGB(), SpiralMaterial::GetRGB(), UVMap::GetRGB(), VectorField2D::GetRGB(), ObjectAlign::GetTransform(), VectorField2D::GetVectorAtPosition(), IsEqual(), Circle::IsInShape(), Ellipse::IsInShape(), Rectangle::IsInShape(), Magnitude(), Maximum(), Minimum(), VectorField2D::MovingSquareField(), Multiply(), Multiply(), VectorField2D::ObjectField(), operator=(), operator=(), BoundingBox2D::Overlaps(), BoundingBox2D::Overlaps(), Rasterizer::Rasterize(), Rotate(), VectorField2D::SetPosition(), VectorField2D::SetSize(), Menu::SetSize(), VectorField2D::SineField(), VectorField2D::SpiralField(), VectorField2D::StepField(), Subtract(), ToString(), Triangle2D::Triangle2D(), Triangle2D::Triangle2D(), Triangle2D::Triangle2D(), UnitCircle(), Menu::Update(), BoundingBox2D::UpdateBounds(), and ProtogenProject::UpdateFace().
float Y |
The Y-component of the 2D vector.
Definition at line 30 of file Vector2D.h.
Referenced by Absolute(), Add(), ObjectAlign::AlignObjects(), ObjectAlign::AlignObjectsNoScale(), HeadsUpDisplay::ApplyEffect(), Fisheye::ApplyEffect(), Magnet::ApplyEffect(), PhaseOffsetR::ApplyEffect(), PhaseOffsetX::ApplyEffect(), BoundingBox2D::BoundingBox2D(), CalculateEuclideanDistance(), CheckBounds(), Rasterizer::CheckRasterPixel(), Rasterizer::CheckRasterPixelAccel(), Constrain(), Constrain(), BoundingBox2D::Contains(), Node::CreateChildNodes(), CrossProduct(), Triangle2D::DidIntersectSAT(), Divide(), Divide(), DotProduct(), HeadsUpDisplay::EnableBitFaceRender(), AudioReactiveGradient::GetRGB(), Oscilloscope::GetRGB(), SpectrumAnalyzer::GetRGB(), Image::GetRGB(), SpiralMaterial::GetRGB(), UVMap::GetRGB(), VectorField2D::GetRGB(), ObjectAlign::GetTransform(), VectorField2D::GetVectorAtPosition(), IsEqual(), Circle::IsInShape(), Ellipse::IsInShape(), Rectangle::IsInShape(), Magnitude(), Maximum(), Minimum(), VectorField2D::MovingSquareField(), Multiply(), Multiply(), VectorField2D::ObjectField(), operator=(), operator=(), BoundingBox2D::Overlaps(), BoundingBox2D::Overlaps(), Rasterizer::Rasterize(), Rotate(), VectorField2D::SetPosition(), VectorField2D::SetSize(), VectorField2D::SineField(), VectorField2D::SpiralField(), VectorField2D::StepField(), Subtract(), ToString(), Triangle2D::Triangle2D(), Triangle2D::Triangle2D(), Triangle2D::Triangle2D(), UnitCircle(), BoundingBox2D::UpdateBounds(), and ProtogenProject::UpdateFace().