Vector2
/3
FunctionsVector2
classVector3
classconst
const
and non-const
. Assigning a value to the non-const
version will change the Vector2
/3
static
or does not operate on a class instanceThe Vector2
and Vector3
classes were designed to have the same functions with the same names whenever possible. Sometimes they even have idenitical implementations (except for for Vector2
/Vector3
). In theory, this allows the same algorithms to be used for both. It should also allow complex algoithms to be developed and debugged in 2D and then easily converted to 3D. The downside is that a few of the functions names in 2D sound a bit strange.
This document is intended as a summary of the functions available. For more detailed information, including preconditions, see the function documentation in Vector2.h
and Vector3.h
.
Last updated October 11, 2017.
These member variables can be accessed and changed directly using dot notation. All values that can be implicitly typecast to double
(including NaN
) are legal.
x
y
z
These create Vector2
/3
s.
Vector3()
: Default constructorVector3(double x, double y)
: Initializing constructorVector3(double x, double y, double z)
: Initializing constructorVector3(const double a_elements)
: Constructor that creates a Vector2
/3
from the first 2/3 elements of an arrayVector3(const double a_elements, unsigned int count)
: Constructor that creates a Vector2
/3
from the first count
elements of an array. Any reamaining elements are initialized to 0.0
Vector3(const Vector3& original)
: Copy constructorIf you are using C++11
, the ones that do not take an array are defined as constexpr
. If you have glm
interaction enabled (OBJ_LIBRARY_GLM_INTERACTION
is #define
d in ObjSettings.h
), there is also a constructor to create a Vector2
/3
from a glm::vec2
/glm::vec3
.
Vector3(const glm::vec3& original)
: Constructor that creates a Vector2
/3
from the glm
equivilent.There is also an assignment operator:
Vector3& operator= (const Vector3& original)
: Assignment operatorThere are no constructors to convert directly between Vector2
and Vector3
. This allows the Vector2
and Vector3
classes to each be used independantly. A constructor with a single value parameter for all components was also deliberatlely not impleneted so as to cut down on errors.
void setZero()
: Sets all components of the Vector2
/3
to 0.0
void set(double x, double y)
: Set the components of the Vector2
to (x
, y
)void set(double x, double y, double z)
: Set the components of the Vector3
to (x
, y
, z
)void setAll(double v)
: Sets all components of the Vector2
/3
to v
void addComponents(double x, double y)
: Increases the respective components of the Vector2
by x
and y
void addComponents(double x, double y, double z)
: Increases the respective components of the Vector3
by x
, y
, and z
void addComponentsAll(double v)
: Increases all components of the Vector2
/3
by v
ostream& operator<<(ostream& r_os, const Vector3& vector)
: Stream insertion operatorA Vector2
is printed in the format (x, y)
, with the elements separated by a single space and no line break at the end. The format for a Vector3
is similar: (x, y, z)
.
These functions return the result of the calculation. Neither argument is changed.
Vector3 Vector3::operator-()
: NegationVector3 Vector3::operator+(const Vector3& right)
: AdditionVector3 Vector3::operator-(const Vector3& right)
: SubtractionVector3 Vector3::operator*(double factor)
: Scalar multiplicationVector3 operator*(double scalar, const Vector3& vector)
: Scalar multiplicationVector3 Vector3::operator/(double divisor)
: Scalar divisionThese functions modify the left argument to contain the calculated result. A non-const
reference to the left argument is returned.
Vector3& Vector3::operator+=(const Vector3& right)
: AdditionVector3& Vector3::operator-=(const Vector3& right)
: SubtractionVector3& Vector3::operator*=(double factor)
: Scalar multiplicationVector3& Vector3::operator/=(double divisor)
: Scalar divisionThese operations are applied between each corresponding pair of components.
Vector3 Vector3::getComponentProduct(const Vector3& other)
: Returns a vector containing the products of the corresponding componentsVector3 Vector3::getComponentRatio(const Vector3& other)
: Returns a vector containing the ratios of the corresponding components. If any of the components of other
is 0.0
, an assert
error is generated.Vector3 Vector3::getComponentRatioSafe(const Vector3& other)
: Returns a vector containing the ratios of the corresponding components. If any of the components of other
is 0.0
, the component of the left vector is used.Clamping variables refers to limiting their values to some range.
Vector3 Vector3::getMinComponents(const Vector3& other)
: Returns a vector containing the smaller (closer to negative infinity) of each of the corresponding components of this vector and other
Vector3 Vector3::getMaxComponents(const Vector3& other)
: Returns a vector containing the larger (closer to positive infinity) of each of the corresponding components of this vector and other
Vector3 Vector3::getSaturated()
: Returns a modified copy of the vector. All components less than 0.0
are replaced by 0.0
. Similarly, all components greater than 1.0
are replaced by 1.0
.Vector3 Vector3::getClampedComponents(const Vector3& min, const Vector3& max)
: Returns a modified copy of the vector. All components less than the corresponding component of min
are replaced by the corresponding component of min
. Similarly, all components greater than the corresponding component of max
are replaced by the corresponding component of max
.Vector3 Vector3::getClampedComponents(double min, double max)
: Returns a modified copy of the vector. All components less than min
are replaced by min
. Similarly, all components greater than max
are replaced by max
.Unless otherwise stated, no tolerance is used for any of these functions.
Finiteness:bool Vector3::isFinite()
: Tests whether all components are finite valuesThis function returns false
if any component is positive infinity, negative infinity, or NaN
, and true
if all components are real values.
bool Vector3::isZero()
: Tests whether the all components of a Vector2
/3
are zero. This function allows for a very small tolerance (+/- 1.0e-100
) to ensure that the norm of the vector is also non-zero.bool Vector3::isZeroStrict()
: Tests whether the all components of a Vector2
/3
are exactly zero. There is no tolerance.The isZero
function is used as a precondition for many normalization-related functions.
bool Vector3::operator==(const Vector3& other)
: Equality Testbool Vector3::operator!=(const Vector3& other)
: Inequality TestisZeroStrict()
functionbool Vector3::isAllComponentsNonZero()
: Tests whether the all components of a Vector2
/3
are non-zerobool Vector3::isAllComponentsPositive()
: Tests whether the all components of a Vector2
/3
are strictly greater than zerobool Vector3::isAllComponentsNegative()
: Tests whether the all components of a Vector2
/3
are strictly less than zerobool Vector3::isAllComponentsNonNegative()
: Tests whether the all components of a Vector2
/3
are greater than or equal to zerobool Vector3::isAllComponentsNonPositive()
: Tests whether the all components of a Vector2
/3
are less than or equal to zerobool Vector3::isAllComponentsEqualTo(double value)
: Tests whether the all components of a Vector2
/3
are equal to value
bool Vector3::isAllComponentsNotEqualTo(double value)
: Tests whether the all components of a Vector2
/3
are not equal to value
bool Vector3::isAllComponentsLessThan(double value)
: Tests whether the all components of a Vector2
/3
are strictly less than value
bool Vector3::isAllComponentsGreaterThan(double value)
: Tests whether the all components of a Vector2
/3
are strictly greater than value
bool Vector3::isAllComponentsLessThanOrEqual(double value)
: Tests whether the all components of a Vector2
/3
are less than or equal to value
bool Vector3::isAllComponentsGreaterThanOrEqual(double value)
: Tests whether the all components of a Vector2
/3
are greater than or equal to value
Vector2
/3
s):
Vector2
/3
, use the equality test operator ( ==
)bool Vector3::isAllComponentsNotEqualTo(const Vector3& other)
: Tests whether the all components of a Vector2
/3
are different than those of other
bool Vector3::isAllComponentsLessThan(const Vector3& other)
: Tests whether the all components of a Vector2
/3
are strictly less than those of other
bool Vector3::isAllComponentsGreaterThan(const Vector3& other)
: Tests whether the all components of a Vector2
/3
are strictly greater than those of other
bool Vector3::isAllComponentsLessThanOrEqual(const Vector3& other)
: Tests whether the all components of a Vector2
/3
are less than or equal to those of other
bool Vector3::isAllComponentsGreaterThanOrEqual(const Vector3& other)
: Tests whether the all components of a Vector2
/3
are greater than or equal to those of other
VECTOR2_NORM_TOLERANCE
: The tolerance used when comparing normalsVECTOR2_NORM_TOLERANCE_SQUARED
: The square of the tolerance used when comparing normals. This value is used in intermediate calculationsVECTOR2_NORM_TOLERANCE_PLUS_ONE_SQUARED
: The square of the one plus the tolerance used when comparing normals. This value is used in intermediate calculationsThere are also similar constants declared for the Vector3
class.
VECTOR3_NORM_TOLERANCE
: The tolerance used when comparing normalsVECTOR3_NORM_TOLERANCE_SQUARED
: The square of the tolerance used when comparing normals. This value is used in intermediate calculationsVECTOR3_NORM_TOLERANCE_PLUS_ONE_SQUARED
: The square of the one plus the tolerance used when comparing normals. This value is used in intermediate calculationsZERO
: A Vector2
/3
with all components equal to 0.0
ONE
: A Vector2
/3
with all components equal to 1.0
UNIT_X_PLUS
: A Vector2
/3
with the X component equal to 1.0
and the other components equal to 0.0
UNIT_X_MINUS
: A Vector2
/3
with the X component equal to -1.0
and the other components equal to 0.0
UNIT_Y_PLUS
: A Vector2
/3
with the Y component equal to 1.0
and the other components equal to 0.0
UNIT_Y_MINUS
: A Vector2
/3
with the Y component equal to -1.0
and the other components equal to 0.0
UNIT_Z_PLUS
: A Vector2
/3
with the Z component equal to 1.0
and the other components equal to 0.0
UNIT_Z_MINUS
: A Vector2
/3
with the Z component equal to -1.0
and the other components equal to 0.0
Warning: These values are initialized as static
constant members. They are initialized when the program starts, just as if they were global variables. This means that, when initializing other global variables, they may not have their values yet. This applies even inside constructors for global variables. The exception is ZERO
, which has the correct value even before it is initialized.
The only truly safe thing to do is not initialize global variables using other global variables. This includes not using global variables inside constructors for types that will be used as global variables. This is a general rule for C++, not just for Vector2
/3
s. Times when a global variables can be used to initialize other global variables safely (e.g. copy = original
) include:
original
is a global variable instead of a static const
class memberoriginal
is an integer type (e.g. bool
, char
, int
, etc.) that is declared in-place (as opposed to seperately as for static const
class members)original
is assigned its value farther up in the same file (or its #include
tree) and copy
is a global variable (not in a constructor)original
is declared as constexpr
instead of const
(C++11
or later)The norm of a vector is the distance along it. Most people call this the length of the vector. However, in mathematics, the length of a vector is the number of components it contains (e.g. 2 for a Vector2
, 3 for a Vector3
). I have chosen to adopt the mathematical convention for these classes.
bool Vector3::isNormal()
: Determines whether this Vector2
/3
is a normal/unit vector. A tolerance is used.bool Vector3::isUnit()
: Indentical to abovedouble Vector3::getNorm()
: Determines the norm of a Vector2
/3
double Vector3::getNormSquared()
: Determines the square of the norm of a Vector2
/3
. This is significantly faster than calculating the norm itself.Calculating the norm of a Vector2
/3
involves a call to sqrt
, and is thus relatively slow. These functions compare the squares of the norms, and thus should be faster than calcuating the norms.
bool Vector3::isNormEqualTo(double length)
: Determines the norm of the vector is equal to length
bool Vector3::isNormLessThan(double length)
: Determines the norm of the vector is less than length
bool Vector3::isNormGreaterThan(double length)
: Determines the norm of the vector is greater than length
bool Vector3::isNormEqualTo(const Vector3& other)
: Determines the norm of the vector is equal to the norm of other
bool Vector3::isNormLessThan(const Vector3& other)
: Determines the norm of the vector is less than the norm of other
bool Vector3::isNormGreaterThan(const Vector3& other)
: Determines the norm of the vector is greater than the norm of other
Warning: A tolerance is used for all of these functions. This means that a "less than" comparison is equivilent to a "less than or equals" comparison. It can also lead to incorrect results in some cases. For example, a norm of 1.0e-10
is simulataneously less than, equal to, and greater than a norm of 0.0
.
Vector3 Vector3::getNormalized()
: Creates a vector with the same direction and a norm of 1.0
Vector3 Vector3::getCopyWithNorm(double norm)
: Creates a vector with the same direction and a norm of norm
void Vector3::normalize()
: Changes the norm of this vector to 1.0
void Vector3::setNorm(double norm)
: Changes the norm of this vector to norm
void Vector3::getNormRatio(const Vector3& other)
: Returns the norm of this vector divided by the norm of other
All of these functions will generate an assert
error if the original vector is zero. If there is a possibility that the original vector is zero, alternate "safe" forms of the functions can be used. These safe forms are slightly slower, but will return a default value instead of crashing if the original vector is zero.
Vector3 Vector3::getNormalizedSafe()
: Creates a vector with the same direction and a norm of 1.0
Vector3 Vector3::getCopyWithNormSafe(double norm)
: Creates a vector with the same direction and a norm of norm
void Vector3::normalizeSafe()
: Changes the norm of the vector to 1.0
void Vector3::setNormSafe(double norm)
: Changes the norm of the vector to norm
void Vector3::getNormRatioSafe(const Vector3& other)
: Returns the norm of this vector divided by the norm of other
Truncating a vector refers to reducing its norm to a maximum value. If the norm is already less than or equal to that value, there is no effect.
Vector3 Vector3::getTruncated(double norm)
: Creates a truncated copy of the vector a maximum norm of norm
Vector3 Vector3::truncate(double norm)
: Reduces the norm of the vector to a maximum of norm
The planar norm of a Vector3
is what its norm would be if one of its components were excluded. This is equivilent to what the norm would be if the Vector3
was projected onto a coordinate plane. The planes used in the calculations are indicated by the function name.
As a Vector2
is always on the XY plane, there are no planar norm functions for the Vector2
class. The standard functions for norms provide the same functionality.
double Vector3::getNormXY()
: Determines the norm of the X and Y components of a Vector3
double Vector3::getNormXZ()
: Determines the norm of the X and Z components of a Vector3
double Vector3::getNormYZ()
: Determines the norm of the Y and Z components of a Vector3
double Vector3::getNormXYSquared()
: Determines the square of the norm of the X and Y components of a Vector3
double Vector3::getNormXZSquared()
: Determines the square of the norm of the X and Z components of a Vector3
double Vector3::getNormYZSquared()
: Determines the square of the norm of the Y and Z components of a Vector3
bool Vector3::isNormXYEqualTo(double length)
: Determines the norm of the X and Y components of a Vector3
is equal to length
bool Vector3::isNormXZEqualTo(double length)
: Determines the norm of the X and Z components of a Vector3
is equal to length
bool Vector3::isNormYZEqualTo(double length)
: Determines the norm of the Y and Z components of a Vector3
is equal to length
bool Vector3::isNormXYLessThan(double length)
: Determines the norm of the X and Y components of a Vector3
is less than length
bool Vector3::isNormXZLessThan(double length)
: Determines the norm of the X and Z components of a Vector3
is less than length
bool Vector3::isNormYZLessThan(double length)
: Determines the norm of the Y and Z components of a Vector3
is less than length
bool Vector3::isNormXYGreaterThan(double length)
: Determines the norm of the X and Y components of a Vector3
is greater than length
bool Vector3::isNormXZGreaterThan(double length)
: Determines the norm of the X and Z components of a Vector3
is greater than length
bool Vector3::isNormYZGreaterThan(double length)
: Determines the norm of the Y and Z components of a Vector3
is greater than length
Vector3
s):
bool Vector3::isNormXYEqualTo(const Vector3& other)
: Determines the norm of the X and Y components of a Vector3
is equal to that of of other
bool Vector3::isNormXZEqualTo(const Vector3& other)
: Determines the norm of the X and Z components of a Vector3
is equal to that of of other
bool Vector3::isNormYZEqualTo(const Vector3& other)
: Determines the norm of the Y and Z components of a Vector3
is equal to that of of other
bool Vector3::isNormXYLessThan(const Vector3& other)
: Determines the norm of the X and Y components of a Vector3
is less than that of of other
bool Vector3::isNormXZLessThan(const Vector3& other)
: Determines the norm of the X and Z components of a Vector3
is less than that of of other
bool Vector3::isNormYZLessThan(const Vector3& other)
: Determines the norm of the Y and Z components of a Vector3
is less than that of of other
bool Vector3::isNormXYGreaterThan(const Vector3& other)
: Determines the norm of the X and Y components of a Vector3
is greater than that of other
bool Vector3::isNormXZGreaterThan(const Vector3& other)
: Determines the norm of the X and Z components of a Vector3
is greater than that of other
bool Vector3::isNormYZGreaterThan(const Vector3& other)
: Determines the norm of the Y and Z components of a Vector3
is greater than that of other
The Euclidean (ordinary) distance between 2 Vector2
/3
s is the norm of the difference between them.
double Vector3::getDistance(const Vector3& other)
: Determines the Euclidean distance between the vector and other
double Vector3::getDistanceSquared(const Vector3& other)
: Determines the square of the Euclidean distance between the vector and other
. This is significantly faster than calculating the distance itself.double Vector3::getManhattenDistance(const Vector3& other)
: Determines the Manhatten distance between the vector and other
. This is the sum of the differences between the corresponding components.double Vector3::getChessboardDistance(const Vector3& other)
: Determines the chessboard distance between the vector and other
. This is the largest differences between a corresponding pair of components.These functions are significantly faster than calculating the distances themselves. A tolerance is used in all cases.
bool Vector3::isDistanceEqualTo(const Vector3& other, double distance)
: To determines if the Euclidean distance between the vector and other
is equal to distance
bool Vector3::isDistanceLessThan(const Vector3& other, double distance)
: To determines if the Euclidean distance between the vector and other
is less than distance
bool Vector3::isDistanceGreaterThan(const Vector3& other, double distance)
: To determines if the Euclidean distance between the vector and other
is greater than distance
All components can be accessed directly using dot notation. These functions allow the components to be conveniantly accessed in different forms.
Vectors With Zeroed Components:These functions return copies of the Vector2
/3
s with some components set to 0.0
.
Vector3 Vector3::getComponentX()
: Returns a copy with only the X component non-zeroVector3 Vector3::getComponentY()
: Returns a copy with only the Y component non-zeroVector3 Vector3::getComponentZ()
: Returns a copy with only the Z component non-zeroVector3 Vector3::getComponentXY()
: Returns a copy with only the X and Y components non-zeroVector3 Vector3::getComponentXZ()
: Returns a copy with only the X and Z components non-zeroVector3 Vector3::getComponentYZ()
: Returns a copy with only the Y and Z components non-zerodouble* Vector3::getAsArray()
: Returns an array containing the vector components as elements. The result is const
if and only if the vector it was called on was const
.This function assumes that the Vector2
/3
member fields are layed out sequentially. If the compiler lays them out in some other way, an assert
error will be generated at runtime when this function is called.
The function can be used to pass values to OpenGL calls more efficiently. For example,
glVertex3d(longAndComplexCodeYeildingAVector3().x, longAndComplexCodeYeildingAVector3().y, longAndComplexCodeYeildingAVector3().z);can become
glVertex3dv(longAndComplexCodeYeildingAVector3().getAsArray());
In addition, this function can also be used to convert between Vector2
s and Vector3
s. To do this, pass the array as an argument to an array-based constructor. For example,
Vector2(my_vec3.getAsArray()); // enough array elements Vector3(my_vec2.getAsArray(), 2); // only 2 of 3 array elements availableNote that the array size is necessary when converting to a "larger" vector. Otherwise the constructor would look beyond the bounds of the array.
Warning: Careless use of this function can result in invalid pointers or array out-of-bounds. This is especially a problem when the code that uses the array is being adapted from handling Vector2
s to Vector3
s or vice versa.
glm
Vectors:
A Vector2
/3
can be typecast to a glm::vec2
/glm::vec3
.
Vector3::operator glm::vec3()
: Returns the vector as a glm
vector.The typecast must be made explicitly, or the compiler will not be able to resolve it.
Vector3 obj3(1.0, 2.0, 3.0); glm::vec3 glm3 = (glm::vec3)(obj3);
I think this has something to do with how glm
uses templates.
double Vector3::dotProduct(const Vector3& other)
: The dot/scalar/inner product of the vector and other
Vector2 Vector2::getPerpendicular()
: A perpendicular Vector2
with the same lengthVector3 Vector3::crossProduct(const Vector3& other)
: The cross/vector product of the vector and other
bool Vector3::isParallel(const Vector3& other)
: Whether the vector and other
are parallel. A tolerance is used.bool Vector3::isSameDirection(const Vector3& other)
: Whether the vector and other
are pointing the same direction. A tolerance is used.bool Vector3::isSameHemisphere(const Vector3& other)
: Whether the vector and other
are pointing withing 90 degrees of the same direction. If this function returns true
for other
, it will normally return false
for -other
. However, this is not guarenteed in borderline cases when the Vector2
/3
s are perpendicular.bool Vector3::isOthogonal(const Vector3& other)
: Whether the vector and other
are orthogonal (perpendicular). A tolerance is used.Vector3 Vector3::getProjection(const Vector3& project_onto)
: The projection of the vector onto project_onto
Vector3 Vector3::getAntiProjection(const Vector3& project_onto)
: The vector minus its projection onto project_onto
. This will always be perpendicular to the projection.Both of these functions will generate an assert
error at runtime if project_onto
is zero. If there is a possibility that project_onto
is zero, alternate "safe" forms of the functions can be used. These safe forms are slightly slower, but will return a default value instead of crashing if project_onto
is zero.
Vector3 Vector3::getProjectionSafe(const Vector3& project_onto)
: The projection of the vector onto project_onto
Vector3 Vector3::getAntiProjectionSafe(const Vector3& project_onto)
: The vector minus its projection onto project_onto
Alternately, if project_onto
is known to be a normal vector, the faster "normal" forms of the functions can be used. These will generate an assert
error if either vector is not normal.
Vector3 Vector3::getProjectionNormal(const Vector3& project_onto)
: The projection of the vector onto project_onto
Vector3 Vector3::getAntiProjectionNormal(const Vector3& project_onto)
: The vector minus its projection onto project_onto
These functions are for calculating the direction of a Vector2
/3
after it has been reflected off a surface with the specified surface normal.
Vector3 Vector3::getReflection(const Vector3& surface_normal)
: The direction of the vector after it reflects off a surface with surface normal surface_normal
. If surface_normal
is zero, an assert
error is generated.Vector3 Vector3::getReflectionSafe(const Vector3& surface_normal)
: The direction of the vector after it reflects off a surface with surface normal surface_normal
. If surface_normal
is zero, the current vector is returned.Vector3 Vector3::getReflectionNormal(const Vector3& surface_normal)
: The direction of the vector after it reflects off a surface with surface normal surface_normal
. If surface_normal
is not a normal vector, an assert
error is generated.Vector3 Vector3::getCosAngle(const Vector3& other)
: The cosine of the angle between the vector and other
. This is significantly faster than calculating the angle itself.Vector3 Vector3::getAngle(const Vector3& other)
: The angle in radians between the vector and other
.Both of these functions will generate an assert
error at runtime if either vector is zero. If there is a possibility that the vectors might be zero, alternate "safe" forms of the functions can be used. These safe forms are slightly slower, but will return a default value instead of crashing if one of the vectors is zero.
Vector3 Vector3::getCosAngleSafe(const Vector3& other)
: The cosine of the angle between the vector and other
.Vector3 Vector3::getAngleSafe(const Vector3& other)
: The angle in radians between the vector and other
.Alternately, if the vectors are both known to be normal vectors, the faster "normal" forms of the functions can be used. These will generate an assert
error if either vector is not normal.
Vector3 Vector3::getCosAngleNormal(const Vector3& other)
: The cosine of the angle between the vector and other
.Vector3 Vector3::getAngleNormal(const Vector3& other)
: The angle in radians between the vector and other
.These functions all defines a 3x3 matrix that is multiplied by the Vector3
. The vector is always interpreted as a column vector and multiplied on the right of the matrix.
Vector3 Vector3::matrixProduct(double e11, double e12, double e13, double e21, ... double e33)
: The nine parameters define a 3x3 matrix. The product of the matrix times the vector is returned.Vector3 Vector3::matrixProductRows(const Vector3& r1, const Vector3& r2, const Vector3& r3)
: The three parameters define the rows of a 3x3 matrix. The product of the matrix times the vector is returned.Vector3 Vector3::matrixProductColumns(const Vector3& c1, const Vector3& c2, const Vector3& c3)
: The three parameters define the columns of a 3x3 matrix. The product of the matrix times the vector is returned. c1
, c2
, and c3
can be interpreted as basis vectors for a basis matrix.Vector3 getClosestPointOnLine(const Vector3& l1, const Vector3& l2, const Vector3& p, bool bounded)
: Returns the closest point on the line or line segment passing through li
and l2
to point p
. If bounded
is true
, the closest point on the line segement is returned. Otherwise, the closest point on the entire line is returned.double Vector2::getRotation()
: Determines the rotation angle in radians of the vector from the positive X axisdouble Vector2::getRotationX()
: Determines the rotation angle in radians of the vector around the X axis from the half of the XY plane where Y is positvedouble Vector2::getRotationY()
: Determines the rotation angle in radians of the vector around the Y axis from the half of the YZ plane where Z is positvedouble Vector2::getRotationZ()
: Determines the rotation angle in radians of the vector around the Z axis from the half of the XZ plane where X is positveVector2 Vector2::getRotated(double radians)
: Returns a copy of Vector2
rotated radians
radians. A positive rotation turns the positive X axis towards the positive Y axis.Vector3 Vector3::getRotatedX(double radians)
: Returns a copy of Vector3
rotated radians
radians around the X axis. A positive rotation turns the positive Y axis towards the positive Z axis.Vector3 Vector3::getRotatedY(double radians)
: Returns a copy of Vector3
rotated radians
radians around the Y axis. A positive rotation turns the positive Z axis towards the positive X axis.Vector3 Vector3::getRotatedZ(double radians)
: Returns a copy of Vector3
rotated radians
radians around the Z axis. A positive rotation turns the positive X axis towards the positive Y axis.Vector3 Vector3::getRotatedXZAxes(double radians_x, double radians_z)
: Returns a copy of Vector3
rotated radians_x
radians around the X axis, followed by radians_z
radians around the Z axisVector3 Vector3::getRotatedArbitrary(const Vector3& axis, double radians)
: Returns a copy of Vector3
rotated radians
radians around axis axis
. If axis
is zero, an assert
error is generated.Vector3 Vector3::getRotatedArbitrarySafe(const Vector3& axis, double radians)
: Returns a copy of Vector3
rotated radians
radians around axis axis
. If axis
is zero, the current vector is returned.Vector3 Vector3::getRotatedArbitraryNormal(const Vector3& axis, double radians)
: Returns a copy of Vector3
rotated radians
radians around axis axis
. If axis
is not a normal vector, an assert
error is generated.It is also possible to rotate a Vector2
/3
towards a target direction with a maximum change in angle. This is useful when rotating something smoothly over several frames. The rotation can be "free" or restricted to be around a single axis.
Vector3 Vector3::getRotatedTowards(const Vector3& desired, double radians)
: Returns a copy of Vector3
rotated up to radians
radians towards direction desired
.Vector3 Vector3::getRotatedTowardsAroundAxis(const Vector3& desired, double radians, const Vector3& axis)
: Returns a copy of Vector3
rotated up to radians
radians towards direction desired
around axis axis
.Both of these functions will generate an assert
error at runtime if either vector is zero. If there is a possibility that the vectors might be zero, alternate "safe" forms of the functions can be used. These safe forms are slightly slower, but will return a default value instead of crashing if one of the vectors is zero.
Vector3 Vector3::getRotatedTowardsSafe(const Vector3& desired, double radians)
: Returns a copy of Vector3
rotated up to radians
radians towards direction desired
.Vector3 Vector3::getRotatedTowardsAroundAxisSafe(const Vector3& desired, double radians, const Vector3& axis)
: Returns a copy of Vector3
rotated up to radians
radians towards direction desired
around axis axis
.Alternately, if the vectors are both known to be normal vectors, the faster "normal" forms of the functions can be used. These will generate an assert
error if axis
is not normal.
Vector3 Vector3::getRotatedTowardsNormal(const Vector3& desired, double radians)
: Returns a copy of Vector3
rotated up to radians
radians towards direction desired
.Vector3 Vector3::getRotatedTowardsAroundAxisNormal(const Vector3& desired, double radians, const Vector3& axis)
: Returns a copy of Vector3
rotated up to radians
radians towards direction desired
around axis axis
.void Vector2::rotate(double radians)
: Rotates the Vector2
by radians
radians. A positive rotation turns the positive X axis towards the positive Y axis.void Vector3::rotateX(double radians)
: Rotates the Vector3
by radians
radians around the X axis. A positive rotation turns the positive Y axis towards the positive Z axis.void Vector3::rotateY(double radians)
: Rotates the Vector3
by radians
radians around the Y axis. A positive rotation turns the positive Z axis towards the positive X axis.void Vector3::rotateZ(double radians)
: Rotates the Vector3
by radians
radians around the Z axis. A positive rotation turns the positive X axis towards the positive Y axis.void Vector3::rotateXZAxes(double radians_x, double radians_z)
: Rotates the Vector3
by radians_x
radians around the X axis, followed by radians_z
radians around the Z axisvoid Vector3::rotateArbitrary(const Vector3& axis, double radians)
: Rotates the Vector3
by radians
radians around axis axis
. If axis
is zero, an assert
error is generated.void Vector3::rotateArbitrarySafe(const Vector3& axis, double radians)
: Rotates the Vector3
by radians
radians around axis axis
. If axis
is zero, there is no effect.void Vector3::rotateArbitraryNormal(const Vector3& axis, double radians)
: Rotates the Vector3
by radians
radians around axis axis
. If axis
is not a normal vector, an assert
error is generated.These functions rotate a Vector2
/3
towards a target direction with a maximum change in angle.
void Vector3::rotateTowards(const Vector3& desired, double radians)
: Rotates the Vector3
by up to radians
radians towards direction desired
.void Vector3::rotateTowardsAroundAxis(const Vector3& desired, double radians, const Vector3& axis)
: Rotates the Vector3
by up to radians
radians towards direction desired
around axis axis
.Both of these functions will generate an assert
error at runtime if either vector is zero. If there is a possibility that the vectors might be zero, alternate "safe" forms of the functions can be used. These safe forms are slightly slower, but will return a default value instead of crashing if one of the vectors is zero.
void Vector3::rotateTowardsSafe(const Vector3& desired, double radians)
: Rotates the Vector3
by up to radians
radians towards direction desired
.void Vector3::rotateTowardsAroundAxisSafe(const Vector3& desired, double radians, const Vector3& axis)
: Rotates the Vector3
by up to radians
radians towards direction desired
around axis axis
.Alternately, if the vectors are both known to be normal vectors, the faster "normal" forms of the functions can be used. These will generate an assert
error if axis
is not normal.
void Vector3::rotateTowardsNormal(const Vector3& desired, double radians)
: Rotates the Vector3
by up to radians
radians towards direction desired
.void Vector3::rotateTowardsAroundAxisNormal(const Vector3& desired, double radians, const Vector3& axis)
: Rotates the Vector3
by up to radians
radians towards direction desired
around axis axis
.All of these functions return Vector2
/3
s with a uniform distribution.
Vector3 Vector3::getRandomUnitVector()
: Returns a random unit vector with a norm of 1.0
Vector3 Vector3::getRandomUnitVectorXY()
: Returns a random unit vector on the XY plane with a norm of 1.0
Vector3 Vector3::getRandomUnitVectorXZ()
: Returns a random unit vector on the XZ plane with a norm of 1.0
Vector3 Vector3::getRandomUnitVectorYZ()
: Returns a random unit vector on the YZ plane with a norm of 1.0
Vector3 Vector3::getRandomSphereVector()
: Returns a random unit vector with a norm of no more than 1.0
These functions all call rand()
to generate random numbers. There are also versions that allow the "random" number to be specifed as seeds. The seeds should be double
values in the range [0.0
, 1.0
).
Vector2 Vector2::getPseudorandomUnitVector(double seed)
: Returns a pseudorandom unit vector with a norm of 1.0
Vector3 Vector3::getPseudorandomUnitVector(double seed1, double seed2)
: Returns a pseudorandom unit vector with a norm of 1.0
Vector3 Vector3::getPseudorandomUnitVectorXY(double seed)
: Returns a pseudorandom unit vector on the XY plane with a norm of 1.0
Vector3 Vector3::getPseudorandomUnitVectorXZ(double seed)
: Returns a pseudorandom unit vector on the XZ plane with a norm of 1.0
Vector3 Vector3::getPseudorandomUnitVectorYZ(double seed)
: Returns a pseudorandom unit vector on the YZ plane with a norm of 1.0
Vector3 Vector3::getPseudorandomSphereVector(double seed1, double seed2)
: Returns a pseudorandom unit vector with a norm of no more than 1.0
Vector3 Vector3::getPseudorandomSphereVector(double seed1, double seed2, double seed3)
: Returns a pseudorandom unit vector with a norm of no more than 1.0
Vector3 Vector3::getRandomInRange()
: Returns a random vector with each component in the range [0.0
, 1.0
)Vector3 Vector3::getRandomInRange(const Vector3& max)
: Returns a random vector with each component between 0.0
(inclusive) and the correspondong element of max
(exclusive)Vector3 Vector3::getRandomInRange(const Vector3& min, const Vector3& max)
: Returns a random vector with each component between the corresponding elements of min
(inclusive) and max
(exclusive)Vector3 Vector3::getRandomInRangeInclusive()
: Returns a random vector with each component in the range [0.0
, 1.0
]Vector3 Vector3::getRandomInRangeInclusive(const Vector3& max)
: Returns a random vector with each component between 0.0
(inclusive) and the correspondong element of max
(inclusive)Vector3 Vector3::getRandomInRangeInclusive(const Vector3& min, const Vector3& max)
: Returns a random vector with each component between the corresponding elements of min
(inclusive) and max
(inclusive)These functions all call rand()
to generate random numbers. There are also versions that allow the "random" number to be specifed as seeds. The seeds should be double
values in the range [0.0
, 1.0
).
Vector3 Vector3::getPseudorandomInRange()
: Returns a random vector with each component in the range [0.0
, 1.0
)Vector3 Vector3::getPseudorandomInRange(const Vector3& max)
: Returns a random vector with each component between 0.0
(inclusive) and the correspondong element of max
(exclusive)Vector3 Vector3::getPseudorandomInRange(const Vector3& min, const Vector3& max)
: Returns a random vector with each component between the corresponding elements of min
(inclusive) and max
(exclusive)