CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
cmlabs::Vector2D Class Reference

2D vector with the usual linear algebra: dot product, determinant, projection, rotation, angles. More...

#include <MathClasses.h>

Public Member Functions

 Vector2D ()
 Zero vector.
 Vector2D (double x, double y)
 From components.
virtual ~Vector2D ()
double operator[] (int n) const
 Component access: 0=x, 1=y.
bool operator== (const Vector2D &v) const
 Component equality.
Vector2D operator- (const Vector2D &v) const
 Difference.
Vector2D operator+ (const Vector2D &v) const
 Sum.
double operator* (const Vector2D &v) const
 Dot product.
Vector2D operator- (double a) const
 Subtract scalar from each component.
Vector2D operator+ (double a) const
 Add scalar to each component.
Vector2D operator* (double a) const
 Scale.
const Vector2Doperator-= (const Vector2D &v)
 In-place subtract.
const Vector2Doperator+= (const Vector2D &v)
 In-place add.
const Vector2Doperator-= (double a)
 In-place scalar subtract.
const Vector2Doperator+= (double a)
 In-place scalar add.
const Vector2Doperator*= (double a)
 In-place scale.
double getX () const
double getY () const
bool set (const PointFloat &p1, const PointFloat &p2)
 Set as p2-p1.
bool set (double x1, double y1, double x2, double y2)
 Set as (x2-x1, y2-y1).
bool set (double x, double y)
 Set components.
bool setX (double v)
 Set x.
bool setY (double v)
 Set y.
bool setLength (double l)
 Rescale to length l, keeping direction.
bool makeUnitVector ()
 Normalize in place.
double length () const
double det (const Vector2D &v) const
 2D cross product / determinant (x*v.y - y*v.x); sign gives turn direction.
bool isOrthogonalWith (const Vector2D &v) const
bool isParallelWith (const Vector2D &v) const
Vector2D getProjectionOn (const Vector2D &v) const
Vector2D getUnitVector () const
Vector2D getOrthogonalVector () const
double getAngle (const Vector2D &v) const
 Unsigned angle to v via acos of the normalized dot product.
double getArea (const Vector2D &v) const
Vector2D rotate (double angle)
 Rotated copy; this vector is unchanged.
bool rotateIt (double angle)
 Rotate in place by angle radians (same convention as rotate()).
std::string print ()
 Human-readable representation for debugging.

Public Attributes

double x
 X component.
double y
 Y component.

Detailed Description

2D vector with the usual linear algebra: dot product, determinant, projection, rotation, angles.

operator* between vectors is the dot product; use det() for the 2D cross product. Angles are always radians. Most operations return a new vector; the *It / make* / set* variants modify in place. Note that in the screen coordinate system used by Box/Bitmap (y down), a mathematically counter-clockwise rotation appears clockwise on screen.

using namespace cmlabs;
Vector2D v(3, 4);
double len = v.length(); // 5
Vector2D u = v.getUnitVector(); // (0.6, 0.8); v unchanged
v.rotateIt(M_PI / 2); // in place: (3,4) -> (-4,3)
Vector2D w(1, 0);
double dot = v * w; // dot product
double turn = w.det(v); // sign gives turn direction
double ang = w.getAngle(v); // unsigned angle in [0, pi]
Vector2D p = v.getProjectionOn(w); // component of v along w
Vector2D()
Zero vector.

Definition at line 656 of file MathClasses.h.

Constructor & Destructor Documentation

◆ Vector2D() [1/2]

◆ Vector2D() [2/2]

cmlabs::Vector2D::Vector2D ( double x,
double y )

From components.

Definition at line 925 of file MathClasses.cpp.

References x, and y.

◆ ~Vector2D()

cmlabs::Vector2D::~Vector2D ( )
virtual

Definition at line 926 of file MathClasses.cpp.

Member Function Documentation

◆ det()

double cmlabs::Vector2D::det ( const Vector2D & v) const

2D cross product / determinant (x*v.y - y*v.x); sign gives turn direction.

Definition at line 1046 of file MathClasses.cpp.

References Vector2D(), x, and y.

Referenced by getArea(), isParallelWith(), and cmlabs::MathClasses_UnitTest().

◆ getAngle()

double cmlabs::Vector2D::getAngle ( const Vector2D & v) const

Unsigned angle to v via acos of the normalized dot product.

Returns
Radians in [0, pi]; 0 when either vector is zero.
Note
Sign/turn direction is lost; combine with det() to recover it.

Definition at line 1098 of file MathClasses.cpp.

References length(), and Vector2D().

◆ getArea()

double cmlabs::Vector2D::getArea ( const Vector2D & v) const
Returns
Area of the parallelogram spanned with v (|det|).

Definition at line 1105 of file MathClasses.cpp.

References det(), and Vector2D().

◆ getOrthogonalVector()

Vector2D cmlabs::Vector2D::getOrthogonalVector ( ) const
Returns
Vector rotated 90 degrees.

Definition at line 1094 of file MathClasses.cpp.

References Vector2D(), x, and y.

Referenced by cmlabs::MathClasses_UnitTest().

◆ getProjectionOn()

Vector2D cmlabs::Vector2D::getProjectionOn ( const Vector2D & v) const
Returns
Orthogonal projection of this vector onto v.

Definition at line 1058 of file MathClasses.cpp.

References getUnitVector(), and Vector2D().

Referenced by cmlabs::MathClasses_UnitTest().

◆ getUnitVector()

Vector2D cmlabs::Vector2D::getUnitVector ( ) const
Returns
Normalized copy.

Definition at line 1067 of file MathClasses.cpp.

References length(), Vector2D(), x, and y.

Referenced by getProjectionOn(), and cmlabs::MathClasses_UnitTest().

◆ getX()

double cmlabs::Vector2D::getX ( ) const
Returns
x.

Definition at line 1015 of file MathClasses.cpp.

References x.

◆ getY()

double cmlabs::Vector2D::getY ( ) const
Returns
y.

Definition at line 1016 of file MathClasses.cpp.

References y.

◆ isOrthogonalWith()

bool cmlabs::Vector2D::isOrthogonalWith ( const Vector2D & v) const
Returns
true when the dot product is (approximately) zero.

Definition at line 1050 of file MathClasses.cpp.

References Vector2D().

◆ isParallelWith()

bool cmlabs::Vector2D::isParallelWith ( const Vector2D & v) const
Returns
true when det() is (approximately) zero.

Definition at line 1054 of file MathClasses.cpp.

References det(), and Vector2D().

◆ length()

double cmlabs::Vector2D::length ( ) const
Returns
Euclidean length.

Definition at line 1042 of file MathClasses.cpp.

References x, and y.

Referenced by getAngle(), getUnitVector(), cmlabs::MathClasses_UnitTest(), and setLength().

◆ makeUnitVector()

bool cmlabs::Vector2D::makeUnitVector ( )

Normalize in place.

Returns
false for the zero vector.

Definition at line 1063 of file MathClasses.cpp.

References setLength().

Referenced by cmlabs::MathClasses_UnitTest().

◆ operator*() [1/2]

double cmlabs::Vector2D::operator* ( const Vector2D & v) const

Dot product.

Definition at line 958 of file MathClasses.cpp.

References Vector2D(), x, and y.

◆ operator*() [2/2]

Vector2D cmlabs::Vector2D::operator* ( double a) const

Scale.

Definition at line 976 of file MathClasses.cpp.

References Vector2D(), x, and y.

◆ operator*=()

const Vector2D & cmlabs::Vector2D::operator*= ( double a)

In-place scale.

Definition at line 1007 of file MathClasses.cpp.

References Vector2D(), x, and y.

◆ operator+() [1/2]

Vector2D cmlabs::Vector2D::operator+ ( const Vector2D & v) const

Sum.

Definition at line 951 of file MathClasses.cpp.

References Vector2D(), x, and y.

◆ operator+() [2/2]

Vector2D cmlabs::Vector2D::operator+ ( double a) const

Add scalar to each component.

Definition at line 969 of file MathClasses.cpp.

References Vector2D(), x, and y.

◆ operator+=() [1/2]

const Vector2D & cmlabs::Vector2D::operator+= ( const Vector2D & v)

In-place add.

Definition at line 989 of file MathClasses.cpp.

References Vector2D(), x, and y.

◆ operator+=() [2/2]

const Vector2D & cmlabs::Vector2D::operator+= ( double a)

In-place scalar add.

Definition at line 1001 of file MathClasses.cpp.

References Vector2D(), x, and y.

◆ operator-() [1/2]

Vector2D cmlabs::Vector2D::operator- ( const Vector2D & v) const

Difference.

Definition at line 944 of file MathClasses.cpp.

References Vector2D(), x, and y.

◆ operator-() [2/2]

Vector2D cmlabs::Vector2D::operator- ( double a) const

Subtract scalar from each component.

Definition at line 962 of file MathClasses.cpp.

References Vector2D(), x, and y.

◆ operator-=() [1/2]

const Vector2D & cmlabs::Vector2D::operator-= ( const Vector2D & v)

In-place subtract.

Definition at line 983 of file MathClasses.cpp.

References Vector2D(), x, and y.

◆ operator-=() [2/2]

const Vector2D & cmlabs::Vector2D::operator-= ( double a)

In-place scalar subtract.

Definition at line 995 of file MathClasses.cpp.

References Vector2D(), x, and y.

◆ operator==()

bool cmlabs::Vector2D::operator== ( const Vector2D & v) const

Component equality.

Definition at line 938 of file MathClasses.cpp.

References Vector2D(), x, and y.

◆ operator[]()

double cmlabs::Vector2D::operator[] ( int n) const

Component access: 0=x, 1=y.

Definition at line 928 of file MathClasses.cpp.

References x, and y.

◆ print()

std::string cmlabs::Vector2D::print ( )

Human-readable representation for debugging.

Definition at line 1038 of file MathClasses.cpp.

References cmlabs::utils::StringFormat(), x, and y.

◆ rotate()

Vector2D cmlabs::Vector2D::rotate ( double angle)

Rotated copy; this vector is unchanged.

Parameters
angleRadians, counter-clockwise in a y-up frame (appears clockwise with screen y-down coordinates).
Returns
Rotated copy; the zero vector rotates to the zero vector.

Definition at line 1077 of file MathClasses.cpp.

References Vector2D(), x, and y.

Referenced by cmlabs::MathClasses_UnitTest().

◆ rotateIt()

bool cmlabs::Vector2D::rotateIt ( double angle)

Rotate in place by angle radians (same convention as rotate()).

Returns
false for the zero vector (nothing to rotate), true otherwise.

Definition at line 1085 of file MathClasses.cpp.

References x, and y.

◆ set() [1/3]

bool cmlabs::Vector2D::set ( const PointFloat & p1,
const PointFloat & p2 )

Set as p2-p1.

Returns
true.

Definition at line 1018 of file MathClasses.cpp.

References set(), cmlabs::PointFloat::x, and cmlabs::PointFloat::y.

Referenced by set(), and set().

◆ set() [2/3]

bool cmlabs::Vector2D::set ( double x,
double y )

Set components.

Returns
true.

Definition at line 1026 of file MathClasses.cpp.

References x, and y.

◆ set() [3/3]

bool cmlabs::Vector2D::set ( double x1,
double y1,
double x2,
double y2 )

Set as (x2-x1, y2-y1).

Returns
true.

Definition at line 1022 of file MathClasses.cpp.

References set().

◆ setLength()

bool cmlabs::Vector2D::setLength ( double l)

Rescale to length l, keeping direction.

Returns
false for the zero vector.

Definition at line 1029 of file MathClasses.cpp.

References length(), x, and y.

Referenced by makeUnitVector(), and cmlabs::MathClasses_UnitTest().

◆ setX()

bool cmlabs::Vector2D::setX ( double v)

Set x.

Returns
true.

Definition at line 1027 of file MathClasses.cpp.

References x.

◆ setY()

bool cmlabs::Vector2D::setY ( double v)

Set y.

Returns
true.

Definition at line 1028 of file MathClasses.cpp.

References y.

Member Data Documentation

◆ x

◆ y


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