Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > vector product

Reply
Thread Tools

vector product

 
 
Bernhard Hidding
Guest
Posts: n/a
 
      04-22-2004
Hello,
I'm a beginner in c++ and I would like to implement some math operations
into my program. Explicitly, would like to perform cross product
calculations. Is there some library or predefined function that is capable
to do this or do I have to write a function that can calculate vector
products for myself? Furthermore, what data structure should I use?
Thanks in advance,
Bernhard Hidding


 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      04-22-2004
Bernhard Hidding wrote:
>
> Hello,
> I'm a beginner in c++ and I would like to implement some math operations
> into my program. Explicitly, would like to perform cross product
> calculations. Is there some library or predefined function that is capable
> to do this or do I have to write a function that can calculate vector
> products for myself? Furthermore, what data structure should I use?
> Thanks in advance,


You have to do your own vector/matrix library.
Alternatively you can search the web for one. That really
depends on what operations you need. A simple 3D vector
class could look like this:

const double EPSILON = 0.000001;

struct CVector
{
public:
CVector() : X(0), Y(0), Z(0) {}
CVector ( double x, double y, double z ) : X(x), Y(y ), Z(z) { }

CVector operator + (const CVector& v ) const
{ return CVector( X + v.X, Y + v.Y, Z + v.Z ); }
CVector operator - (const CVector& v ) const
{ return CVector( X - v.X, Y - v.Y, Z - v.Z ); }
CVector operator -() const
{ return CVector( -X, -Y, -Z ); }
BOOL operator == (const CVector& v) const
{ return (fabs(X - v.X) < EPSILON) &&
(fabs(Y - v.Y) < EPSILON) &&
(fabs(Z - v.Z) < EPSILON); }
BOOL operator != (const CVector& v) const
{ return ! (*this == v); }
const CVector& operator += (const CVector& v)
{ X += v.X; Y += v.Y; Z += v.Z; return *this; }
const CVector& operator -= (const CVector& v)
{ X -= v.X; Y -= v.Y; Z -= v.Z; return *this; }
const CVector& operator *= (const CVector& v)
{ X *= v.X; Y *= v.Y; Z *= v.Z; return *this; }

CVector CrossProd( const CVector& v ) const
{ return CVector( Y * v.Z - Z * v.Y,
Z * v.X - X * v.Z,
X * v.Y - Y * v.X ); }
float DotProd( const CVector& v ) const
{ return X * v.X + Y * v.Y + Z * v.Z; }
void Set( double x, double y, double z ) { X = x; Y = y; Z = z; }
void Get( double& x, double& y, double& z ) const { x = X; y = Y; z = Z; }
double Norm() const
{ return ( X*X ) + ( Y*Y ) + (Z*Z);}
double Length() const
{ return sqrt( Norm() ); }
double Distance( const CVector& v ) const
{ return (v - *this).Length(); }

double X;
double Y;
double Z;
};


--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Product Manager/ Sr. Product Manager padma C++ 0 10-03-2007 12:30 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments