|
|
|
Vectors
Vector2, Vector3, and Vector4 provide small fixed-size vectors with these characteristics that make them efficient representations for technical applications:
- Stack-based: no heap allocations.
- Fast, loop-free operations.
- Minimal space overhead.
- Interoperation with std::array, std::vector, std::initializer_list, and Array1.
- Useful functions such as distance, normalization, dot and cross products.
The Vectors are class templates. Shorthand (typedef) names are provided for the common value types in their forward declaration headers so, for example, we use Vector2_double instead of Vector2< double > on this page.
Named Elements
Vector elements can be accessed by name with a few common variants:
Vector3_double v( { 1.0, 2.0, 3.0 } );
...
v.x = 7.0; // Element 1 is x
v.y = 8.0; // Element 2 is y
v.z = 9.0; // Element 3 is z |
Vector3_double v( { 1.0, 2.0, 3.0 } );
...
v.x1 = 7.0; // Element 1 is x1
v.x2 = 8.0; // Element 2 is x2
v.x3 = 9.0; // Element 3 is x3 |
Vector3_double v( { 1.0, 2.0, 3.0 } );
...
v.x_1 = 7.0; // Element 1 is x_1
v.x_2 = 8.0; // Element 2 is x_2
v.x_3 = 9.0; // Element 3 is x_3 |
Subscripting
Vector elements can be accessed by subscripting operations:
Vector3_double v( { 1.0, 2.0, 3.0 } );
v[ 0 ] = 11.0; // [] operations use 0-based indexing
v( 2 ) = 12.0; // () operations use 1-based indexing
// Now elements are { 11.0, 12.0, 3.0 } |
Assignment
The Vectors support whole-vector element-wise assignment with the operators {=, +=, -=, *=, /=}:
Vector3_double v( { 1.0, 2.0, 3.0 } );
Vector3_double u( { 4.0, 5.0, 6.0 } );
...
v += u; |
Assignment of std::array, std::vector, std::initializer_list, brace-enclosed composite initializers, and Arrays are also supported.
Uniform whole-array assignment of any scalar value that is assignment-compatible with the Vector's value type with the operators {=, +=, -=, *=, /=}:
Vector3_double v( { 1.0, 2.0, 3.0 } );
...
v += 10.0; // Now elements are { 11.0, 12.0, 13.0 } |
Comparison
The Vectors provide two kinds of comparison operations:
Vector3_double v( { 1.0, 2.0, 3.0 } );
Vector3_double u( { 4.0, 5.0, 6.0 } );
...
v < u; // Inequalities use lexical order to provide a complete ordering
lt( v, u ); // Named comparison functions use strict (partial) ordering |
Inequalities comparisons use lexical order to provide a complete ordering so that Vectors can be stored in containers like std::set. Named comparison functions like lt and ge use strict ordering of all elements and thus provide only a partial ordering.
Additional Functions
The Vectors provide a number of additional functions:
Vector3_double v( { 1.0, 2.0, 3.0 } );
Vector3_double u( { 4.0, 5.0, 6.0 } );
v + u // Add vectors
mid( v, u ) // Vector with midpoint of each element pair
min( v, u ) // Vector with min of each element pair
max( v, u ) // Vector with max of each element pair
angle( v, u ) // Angle formed by two vectors
dot( v, u ) // Dot product
v.dot( u ) // Same thing
cross( v, u ) // Cross product
v.cross( u ) // Cross product
v.zero() // Set all elements to zero
v.is_zero() // Are all elements zero?
v.normalize() // Scale to unit length
v.normalize( h ) // Scale to specified length
v.normalize_zero() // Normalize but return zero vector if current length is zero
v.normalize_x() // Normalize but return unit x-vector if current length is zero
std::cout << v // Output vector to std::cout
std::cin >> v // Input vector from cin |
|
|
|
|