\b Vector2D class is representing cartesian mathematical vector in two dimensional space.
Controller:
Interface
#include <codecogs/maths/geometry/_2d/vector2d.h>
using namespace std;
| template<...> class | Vector3D
Vector2D class is representing cartesian mathematical vector in two dimensional space. |
| class | Vector2D
Vector2D class is representing cartesian mathematical vector in two dimensional space. | union | unknown components of 2D vector can be accessed using different synonims or through pointer to all of them | | y[constructor] | Vector2D (RealType X=0, RealType Y=0) 2D vector can be initialized either passing X and Y coordinates of 2D vector, where X=0 and Y=0 are defult values if X and/or Y are not specified | | [constructor] | Vector2D (RealType coords[2]) 2D vector can also be initialized passing a pointer to RealType components (2 of them) | | void operator | = (RealType coords[2])[inline] operator = can be used between two 2D vectors, or 2D vector and pointer to two RealType components (describing a 2D vector) | | double | getLength ()[inline] getLength returns single value describing distance of Vector from Zero Vector in 2D | | void operator | += (const Vector2D <RealType>&op2)[inline] additive translation | | void operator | -= (const Vector2D <RealType>&op2)[inline] substractive translation | | void operator* | = (double scalar) operator *= and /= are provided to modify initial length of vector | | void operator | /= (double scalar) | | bool operator | == (const Vector2D <RealType>&V)[const] comparison of two 2D vectors are performed using standard == operators from C/C++ | | bool operator | != (const Vector2D <RealType>&V)[const] comparison of two 2D vectors are performed using standard != operators from C/C++ | | Vector2D<RealType> operator | + (const Vector2D <RealType>&v2) additive translation | | Vector2D<RealType> operator | - (const Vector2D <RealType>&v2) substractive translation | | Vector2D<RealType> operator | - () invert sign operation (rotation by 180 degrees) | | Vector2D<RealType> operator* | unknown (double scalar) returns 2D vector representing input 2D vector stretched by scalar | | Vector2D<RealType> operator | / (double scalar) distance of input 2D vector is divided by scalar, Zero Vector stays Zero Vector | | double operator* | unknown (const Vector2D <RealType>&v2) operator * provides dot-product for two input 2D vectors | | RealType operator^ | unknown (const Vector2D <RealType> &v2) operator ^ stands for cross product of two vectors returning Vector3D which is perpendicular to both input 3D vectors | | void | normalize () normalize modifies 2D vector so it's length becomes one, keeping same direction | | Vector2D<RealType> | getNormalized () getNormalized returns a 2D unit vector of same direction as input 2D vector | | Vector2D<double> | getDouble () returns 2D vector but with components of double format | | Vector2D<float> | getFloat () returns 2D vector but with components of float format | | ostreamoperator & | << (ostream &os, const Vector2D <RealType> &v) output of input 2D vector on the stream in "X Y" formatstream operations will work with custom data types only if << and >> are overloaded for streams | | istreamoperator & | >> (istream &is, const Vector2D <RealType> &v) | | bool | isColinearTo (const Vector2D <RealType>& point1) | | bool | isZero () return true if this 2D vector is Zero vector in 2D | | bool | isDegenerate () return true if this 2D vector is Zero vector in 2D | | void operator | = (const Vector3D <RealType> &op2) assigns 3D vector to 2D vector ignoring Z component of 3D vector | | Vector3D<RealType> | get3D () get3D method returns 2D vector converted to 3D vector assuming Z=0 | | RealType | getPoweredLength ()[inline] returns a length of the vector on the power of 2 (eg. before using sqrt which is slow) |
|
Use the following HTML code to embed the calculators within other websites:
Overview
Authors
- Vedran Radivojkov (January 2006)
Class Vector2D
Math Background
Math says that Vectors have:
- orientation (direction)
- length (magnitude)
each vector is described by N-paired group of coordinates along N axises where N-th component is
corresponding to distance from 0 in N-th dimension.
Vectors have components of
Real type in strict order, permutations of components are
transforming vectors.
in each N-dimensional space there is one unique Vector having each component equal to 0
it will be refered as
Zero-vector and in 2D space it is equal to
(0,
0)
Zero-vector is both
coplanar and
perpendicular to any other vector in the same time.
direction of vector is describing direction of point given by coordinates in appropriate space from
Zero-vector in that same coordinate space.
Unit vector is special type of
vector being of length equal to
1.
Operations On Vectors
Scalar is number of
Real type (one dimensional vector)
operations which can be performed on
two vectors or
vector and scalar regardless of
dimension are:
geometric meaning is translation of one Vector by second Vector
- Substract vector from vector
mathematicaly same as addition of vectors, second vector having oposite sign
geometric meaning is translation of first Vector by second Vector
- Multiply vector by scalar:
by multiplying vector with scalar you get the effect of scaling vector by shortening or extending
vector's length
- Divide vector by scalar : can cause exception if scalar is 0 (divide by zero exception)
in terms of mathematics divide vector by scalar is same as multiply vector by 1/scalar
but it's implemented here for convenience and probably efficiency of code.
- Dot product of two vectors : returns single Real number
General case:
a * b = |a|*|b|*cos(angle) where a and b are vectors, first * is dot product between a and b, and
angle is angle formed between vectors a and b
Special cases:
- one Vector is of unit length
result's interpretation can be: projection length of second vector onto first
vector
- both Vector's are of unit length
result is equal to cos(angle) where angle is angle formed between those vectors
- Cross product of two vectors
results in obtaining vector that is perpendicular to both input vectors
it is not defined in two dimensions, however for conveniency in code it can be used even between
two dimensional vectors in such way that it can be interpreted as converting 2D vectors to 3D
vectors before actuall calculation is done. Keeping in mind that resulting vector should be
perpendicular to both input vectors we know that resulting vector would have first two components
equal to 0 and only third component is valuable, so cross product will result in only signle value
when performing dot product between two 2D vectors
- Calculate Length of vector
results in single value, absolute distance from Zero-vector to point described by vector's
components (eg. length of line segment drawn between Zero-vector and points's position in
coordinate space)
\b Conventions:
- First dimension's coordinate is refered as distance of vector from Zero-vector along
X axis and will be used as
x component
- Second dimension's coordinate is refered as distance of vector from Zero-vector along
Y axis and will be used as
y component
\b Code \b Rules, \b Conveniency And \b Efficiency:
- Vector classes are built up in such way that they can take any data type for components using
template mechanisms and thus providing least memory usage or greater precision mechanism.
- Custom data type provided to our template data type is required to be compatible with
float and
double data types
More dimensional and less dimensional vectors will describe vector A having more components then
vector B (vector B having less less components then vector A) relation
- Conversion of vector of less components to vector of more components will be done
treating
missing components to be of value
0
- conversion of vector of more components to vector of less components will be done
clipping
remaining components and should be used cautiously because it could cause wrong
results
assuming additional components to be equal to
0
- for conveniency in code each component might use different names (synonims) to improve code
readability where vector is used to represent some specific type of calculations
1-st component in 2D space: x or u
2-nd component in 2D space: y or v
Additionaly pointer to all components together can be used with simply merging names of each
component in a single name:
it is performed without function call, so it is not making any overhead and still providing proper
readability
both dimensions of 2D space: xy or uv
u and v are used as names in texture maps and rgb scheme can be used when image pixels are treated
as vectors (can be of good use in 2D/3D software, and same mechanism can be used to add more
synonims if usefull)
- Conversions between dimensions are performed automaticaly whenever possible, keeping
in mind treatment of additional components of more dimensional vector
template class Vector2D is optimized for speed module, using general interface to vector
class regardles of dimensions, also designed in such way that oprators are same as used in
vector
geometry math on paper, with exception of cross_product "^" instead of
"x" which is not provided as possible operator in C++ language definition.
- Can be combined with other module Vector3D or used indenpendantly, providing least
possible memory usage either way.
- Provides custom storage type, usefull when combining with other library data types (like
OpenGL's GLfloat and GLdouble)
- Can be easily used even in classic C functions requiring or providing pointers to
group of
components.
- All suitable operators are overloaded in order to improve readability of code, including
input from stream and output to stream which can be used to load or same various 2D/3D textual data
files
Authors
- Vedran Radivojkov (January 2006)
Source Code
Source code is available when you agree to a GP Licence or buy a Commercial Licence.
Not a member, then Register with CodeCogs. Already a Member, then Login.
Members of Vector2D
Vector2D
| X | Default Value = 0 |
| Y | Default Value = 0 |
+=
operators += is provided to accumulate 2D vector on the right side of operator on 2D vector on the
left side of operator refered as translation of left side vector by right side vector
-=
operators += is provided to accumulate 2D vector on the right side of operator on 2D vector on the
left side of operator refered as translation of left side vector by right side vector
==
both components of first vector need to match with their pair in second vector
!=
at least one component of first vector needs to be different from it's paired component in second vector
+
MISSING IMAGE!
1/vectoradd.jpg cannot be found in /users/1/vectoradd.jpg. Please contact the submission author.
addition operator known from math
-
MISSING IMAGE!
1/vectorsub.jpg cannot be found in /users/1/vectorsub.jpg. Please contact the submission author.
substraction operator known from math
-
unary operator - parsed before 2D vector at input, inverts his sign keeping orientation so it's
result is same as: "Zero Vector" - "input 3D vector at input"
Unknown
returns vector which length was multiplied by scalar value, Zero Vector stays Zero Vector
/
Zero Vector stays Zero Vector
can cast an exception if scalar is equal to 0
same operation can be done using * with 1/scalar value
Unknown
MISSING IMAGE!
1/vectorcross.jpg cannot be found in /users/1/vectorcross.jpg. Please contact the submission author.
both 2D vectors we know X and Y will be equal to 0 so only Z component is important
Other Documentation
Conclusion
Vector classes represent foundations of all Vector geometric functions,
and so they were designed in such way that they are both as fast and as flexible as possible
making source code look as close as possible to mathematics calculations on written on paper
(improving readability, flexibility and beuty of code).
Source Code
Source code is available when you agree to a GP Licence or buy a Commercial Licence.
Not a member, then Register with CodeCogs. Already a Member, then Login.