modm API documentation
modm::Vector< T, 2 > Class Template Reference

Class for handling common vector operations (2D) More...

#include <modm/math/geometry/vector2.hpp>

Public Typedefs

typedef GeometricTraits< T >::WideType WideType
 
typedef GeometricTraits< T >::FloatType FloatType
 

Public Member Functions

 Vector ()
 Default-Constructor. More...
 
 Vector (const T &inX, const T &inY)
 
 Vector (const Vector< T, 1 > &inX, const Vector< T, 1 > &inY)
 
 Vector (const T &inX, const Vector< T, 1 > &inY)
 
 Vector (const Vector< T, 1 > &inX, const T &inY)
 
 Vector (T inVal)
 
 Vector (const Matrix< T, 2, 1 > &rhs)
 
void setX (const T &value)
 
void setY (const T &value)
 
void set (const T &x, const T &y)
 
const T & getX () const
 
const T & getY () const
 
getLength () const
 Calculate length of the vector.
 
WideType getLengthSquared () const
 Calculate squared length of the vector. More...
 
float getAngle () const
 Calculate the absolute angle. More...
 
Vectornormalize ()
 Normalize length to 1. More...
 
Vector normalized () const
 
Vectorscale (float length)
 Scale the vector to length.
 
Vector scaled (float length) const
 
Vectorrotate (float phi)
 
Vectortranslate (const Vector &vector)
 Move the point in x and y direction.
 
WideType getDistanceTo (const Vector &other) const
 
float getAngleTo (const Vector &other) const
 
WideType dot (const Vector &other) const
 Calculate the dot-product. More...
 
WideType cross (const Vector &other) const
 Calculate the cross-product. More...
 
template<typename U >
Vector< U, 2 > convert () const
 Convert between Point-objects with different base-types.
 
Vector toOrthogonalVector () const
 Returns a perpendicular copy of the vector. More...
 
Vector perpendicular () const
 
Vectoroperator= (const Matrix< T, 2, 1 > &rhs)
 
bool operator== (const Vector &rhs) const
 
bool operator!= (const Vector &rhs) const
 
bool operator< (const Vector &rhs) const
 
bool operator<= (const Vector &rhs) const
 
bool operator> (const Vector &rhs) const
 
bool operator>= (const Vector &rhs) const
 
const T & operator[] (uint8_t index) const
 
T & operator[] (uint8_t index)
 
T * ptr ()
 
const T * ptr () const
 
Vector operator- () const
 
Vector operator- (const Vector &rhs) const
 
Vector operator+ (const Vector &rhs) const
 
operator* (const Vector &rhs) const
 
operator^ (const Vector &rhs) const
 
Vector operator* (float rhs) const
 
Vector operator/ (float rhs) const
 
Vectoroperator+= (const Vector &rhs)
 
Vectoroperator-= (const Vector &rhs)
 
Vectoroperator*= (const T &rhs)
 
Vectoroperator/= (const T &rhs)
 
Vectoroperator~ ()
 
Matrix< T, 2, 1 > & asMatrix ()
 
const Matrix< T, 2, 1 > & asMatrix () const
 
Matrix< T, 1, 2 > & asTransposedMatrix ()
 
const Matrix< T, 1, 2 > & asTransposedMatrix () const
 
bool hasNan () const
 
bool hasInf () const
 

Static Public Member Functions

static int_fast8_t ccw (const Vector &a, const Vector &b, const Vector &c)
 Check if three points are in a counter-clock wise direction. More...
 

Public Attributes

x
 
y
 

Friends

class Location2D< T >
 
template<typename U >
IOStreamoperator<< (IOStream &os, const Vector< U, 2 > &c)
 Stream operator for modm::Vector<U, 2>
 
template<typename U >
Vector< U, 2 > operator* (float scale, const Vector< U, 2 > &vector)
 Scalar multiplication.
 

Detailed Description

template<typename T>
class modm::Vector< T, 2 >

Class for handling common vector operations (2D)

Operations:

+ : addition of points
- : different of points
* : dot product or scalar multiplication
/ : scalar division
^ : cross product (determinant)
~ : perpendicular

Adapted from the implementation of Gaspard Petit (gaspardpetit@gmail.com) and heavily modified.

See also
<a href"http://www-etud.iro.umontreal.ca/~petitg/cpp/point.html">Homepage
Author
Fabian Greif
Niklas Hauser

Constructor & Destructor Documentation

template<typename T >
modm::Vector< T, 2 >::Vector ( )

Default-Constructor.

Creates a Vector with coordinates (0, 0).

Member Function Documentation

template<typename T >
static int_fast8_t modm::Vector< T, 2 >::ccw ( const Vector< T, 2 > &  a,
const Vector< T, 2 > &  b,
const Vector< T, 2 > &  c 
)
static

Check if three points are in a counter-clock wise direction.

Check if we move counter-clock wise if we move from the first point to the second and the third.

If all three points are in a line there are three possibilities: 1) strait line: third point behind the second (returns 1) 2) last point between the other two (returns 0) 3) third point before the first one (returns -1)

This definition is useful for inclusion or intersection testing.

template<typename T >
WideType modm::Vector< T, 2 >::cross ( const Vector< T, 2 > &  other) const

Calculate the cross-product.

In 2D there is no clear definition of this operation.

This implementation is the most common one and will return the magnitude of the vector that would result from a regular 3D cross product of the input vectors, taking their Z values implicitly as 0 (i.e. treating the 2D space as a plane in the 3D space). The 3D cross product will be perpendicular to that plane, and thus have 0 X & Y components (thus the scalar returned is the Z value of the 3D cross product vector).

this.x * other.y - this.y * other.x

Other implementations take no arguments and returns a vector perpendicular to the input vector. This can be reached with the toOrthogonalVector() method, which returns a perpendicular copy of the vector.

template<typename T >
WideType modm::Vector< T, 2 >::dot ( const Vector< T, 2 > &  other) const

Calculate the dot-product.

Also known as the scalar product.

this.x * other.x + this.y * other.y
template<typename T >
float modm::Vector< T, 2 >::getAngle ( ) const

Calculate the absolute angle.

atan2(y, x)
template<typename T >
WideType modm::Vector< T, 2 >::getLengthSquared ( ) const

Calculate squared length of the vector.

This method is considerably faster than getLength() because it doesn't need to calculate the square root.

Returns
squared length (x*x + y*y)
template<typename T >
Vector& modm::Vector< T, 2 >::normalize ( )

Normalize length to 1.

Warning
This method is only useful if T is a floating point type. For integer types the result might be wrong!
template<typename T >
Vector modm::Vector< T, 2 >::toOrthogonalVector ( ) const

Returns a perpendicular copy of the vector.

Returns
(y, -x)

The documentation for this class was generated from the following file: