I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
get GPL
COST (GBP)
this unit 0.00
sub units 0.00
+
0
maths › geometry › _2d ›

Vector2D

\b Vector2D class is representing cartesian mathematical vector in two dimensional space.
Controller: CodeCogs

Interface

C++

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:

  • Add vector on vector
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

 
yVector2DRealTypeX = 0
RealTypeY = 0 )[constructor]
XDefault Value = 0
YDefault Value = 0

+=

 
void operator+=const Vector2D&op2 )[inline]
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

-=

 
void operator-=const Vector2D&op2 )[inline]
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

==

 
bool operator==const Vector2D&V )[const]
both components of first vector need to match with their pair in second vector

!=

 
bool operator!=const Vector2D&V )[const]
at least one component of first vector needs to be different from it's paired component in second vector

+

 
Vector2D<RealType> operator+const Vector2D&v2 )
MISSING IMAGE!

1/vectoradd.jpg cannot be found in /users/1/vectoradd.jpg. Please contact the submission author.

addition operator known from math

-

 
Vector2D<RealType> operator-const Vector2D&v2 )
MISSING IMAGE!

1/vectorsub.jpg cannot be found in /users/1/vectorsub.jpg. Please contact the submission author.

substraction operator known from math

-

 
Vector2D<RealType> operator- )
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

 
Vector2D<RealType> operator*unknowndoublescalar )
returns vector which length was multiplied by scalar value, Zero Vector stays Zero Vector

/

 
Vector2D<RealType> operator/doublescalar )
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

 
RealType operator^unknownconst Vector2D<RealType>& v2 )
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.