Vector2

Defines a 2D vector class with operators.

[ GAMB | Source | Keywords | Summary | Ancestors | All Members | Descendants ]

Quick Index

AUTHORS
CHANGES LOG
GOALS
USAGE

Class Summary

class Vector2

{
public:
real norm() const;
real norm2() const;
real dist(const Vector2& p) const;
real dist2(const Vector2& p) const;
friend Vector2 operator+(const Vector2 &, const Vector2 &);
friend Vector2 operator-(const Vector2 &, const Vector2 &);
friend Vector2 operator-(const Vector2 &);
friend real operator*(const Vector2 &, const Vector2 &);
friend Vector2 operator*(const Vector2 &, const real &);
friend Vector2 operator*(const real &, const Vector2 &);
friend Vector2 operator/(const Vector2 &, const real &);
friend real operator|(const Vector2 &, const Vector2 &);
friend real operator^(const Vector2 &, const Vector2 &);
Vector2& operator+=(const Vector2 &);
Vector2& operator-=(const Vector2 &);
Vector2& operator*=(const real& m);
Vector2& operator/=(const real &m);
friend ostream& operator<<(ostream& s, const Vector2& v);
protected:
}; // Vector2

Back to the top of Vector2


AUTHORS

Zipi Fligelman. (zipo@math.tau.ac.il)

Copyright: SAMBA group, Tel-Aviv Univ. Israel, 1998.

Back to the top of Vector2


CHANGES LOG

Back to the top of Vector2


GOALS

Though most of the work here is done in 3d, there is sometime need for a 2D reference, for example surfaces in a sense are essentially 2D objects. Also if one is using an affin transformation from 3D to 2D.

Back to the top of Vector2


USAGE

Many 2D vector operations are supplied including vector addition and subtraction, dot-product mult., multiplication by scalar, distance and angular operators.

A new Vector2 may be constructed using 2 floats or an array of 2 floats.

    Vector3 unit1(1.0, 0.0);
    float x[2] = {0.0, 1.0};
    Vector3 unit2(x);

Coordinates may be accessed using the x(int) method which returns the vector's given coordinate (1,2) or using the [] operator (0,1).

    if (unit1[0] == 1.0 && unit1[1] == 0.0)  cout << "the E1 vector\n";
    if (unit2[0] != 0.0) cout << "FATAL Error!!!\n";

Addition, subtraction, multiplication and division by scalar, dot-product distance, angle.

    w = u+v;                           // addition
    w-= v;                             // in-place subtraction w==u
    if ((w|u) == 0) cout << "OK\n";    // distance operator |
    if (w^u == 0) cout << "OK\n";      // angle operator ^
    w=2*w;                             // multiplicatio by scalar
    float f=w*v;                       // dot-product;
Notice: there is no cross product in 2D

Back to the top of Vector2


real norm() const;

Return Vector2's distance from origin.

  real norm() const;

Back to the top of Vector2


real norm2() const;

Return Vector2's squared distance ffrom origin.

  real norm2() const;

Back to the top of Vector2


real dist(const Vector2& p) const;

Returns Vector2's distance from Vector2 p.

  real dist(const Vector2& p) const;

Back to the top of Vector2


real dist2(const Vector2& p) const;

returns Vector2's squared distance from another Vector2 p.

  real dist2(const Vector2& p) const;

Back to the top of Vector2


friend Vector2 operator+(const Vector2 &, const Vector2 &);

Returns Vector2 addition of two Vector2s.

  friend Vector2 operator+(const Vector2 &, const Vector2 &);

Back to the top of Vector2


friend Vector2 operator-(const Vector2 &, const Vector2 &);

Returns Vector2 subtraction of two Vector2s.

  friend Vector2 operator-(const Vector2 &, const Vector2 &);

Back to the top of Vector2


friend Vector2 operator-(const Vector2 &);

Returns Vector2 negative.

  friend Vector2 operator-(const Vector2 &);

Back to the top of Vector2


friend real operator*(const Vector2 &, const Vector2 &);

Returns dot product of two Vector2s.

  friend real operator*(const Vector2 &, const Vector2 &);

Back to the top of Vector2


friend Vector2 operator*(const Vector2 &, const real &);

Returns vector multiplied by scalar m.

  friend Vector2 operator*(const Vector2 &, const real &);

Back to the top of Vector2


friend Vector2 operator*(const real &, const Vector2 &);

Returns vector multiplied by scalar m.

  friend Vector2 operator*(const real &, const Vector2 &);

Back to the top of Vector2


friend Vector2 operator/(const Vector2 &, const real &);

Returns vector divided by scalar m.

  friend Vector2 operator/(const Vector2 &, const real &);

Back to the top of Vector2


friend real operator|(const Vector2 &, const Vector2 &);

Returns distance between 2 Vector2s (similar to dist).

  friend real operator|(const Vector2 &, const Vector2 &);

Back to the top of Vector2


friend real operator^(const Vector2 &, const Vector2 &);

Returns angle between p1 and p2 in radians.

  friend real operator^(const Vector2 &, const Vector2 &);

Back to the top of Vector2


Vector2& operator+=(const Vector2 &);

Vector2 addition of another Vector2's coordinates.

  Vector2& operator+=(const Vector2 &);

Back to the top of Vector2


Vector2& operator-=(const Vector2 &);

Vector2 subtraction of another Vector2's coordinates.

  Vector2& operator-=(const Vector2 &);

Back to the top of Vector2


Vector2& operator*=(const real& m);

Multiplies coordinates by scalar m.

  Vector2& operator*=(const real& m);

Back to the top of Vector2


Vector2& operator/=(const real &m);

Divides coordinates by scalar m.

  Vector2& operator/=(const real &m);

Back to the top of Vector2


friend ostream& operator<<(ostream& s, const Vector2& v);

Outputs coordinates delimited by single space.

  friend ostream& operator<<(ostream& s, const Vector2& v);

Back to the top of Vector2


All Members

public:
real norm() const;
real norm2() const;
real dist(const Vector2& p) const;
real dist2(const Vector2& p) const;
friend Vector2 operator+(const Vector2 &, const Vector2 &);
friend Vector2 operator-(const Vector2 &, const Vector2 &);
friend Vector2 operator-(const Vector2 &);
friend real operator*(const Vector2 &, const Vector2 &);
friend Vector2 operator*(const Vector2 &, const real &);
friend Vector2 operator*(const real &, const Vector2 &);
friend Vector2 operator/(const Vector2 &, const real &);
friend real operator|(const Vector2 &, const Vector2 &);
friend real operator^(const Vector2 &, const Vector2 &);
Vector2& operator+=(const Vector2 &);
Vector2& operator-=(const Vector2 &);
Vector2& operator*=(const real& m);
Vector2& operator/=(const real &m);
friend ostream& operator<<(ostream& s, const Vector2& v);
protected:

Back to the top of Vector2


Ancestors

Class does not inherit from any other class.

Back to the top of Vector2


Descendants

Class is not inherited by any others.

Back to the top of Vector2


Generated from source by the Cocoon utilities on Sun Nov 15 13:35:26 2009 .

Report problems to jkotula@unimax.com