473,761 Members | 4,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector product

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
Jul 22 '05 #1
1 6054
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
kb******@gascad .at
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3206
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII "stereolithography" file (*.STL) into my program. This has the following syntax... Begin STL Snippet **********
7
1559
by: wutongjoe | last post by:
Hi,I had a problem while I was doing the assignment .the code is like below(not exact): //====================================== class Buyer { private: int _product;
14
2666
by: LumisROB | last post by:
Is it possible to create matrixes with vector <vector <double >> ? If it is possible which is the element m23 ? You excuse but I am not an expert Thanks ROB
14
4001
by: Michael Sgier | last post by:
Hello If someone could explain the code below to me would be great. // return angle between two vectors const float inline Angle(const CVector& normal) const { return acosf(*this % normal); } // reflect this vector off surface with normal vector
5
9999
by: Martin Vilcans | last post by:
Hi, I'm new to this mailing list and fairly new to Python as well. I'm working on a prototype for a 3D game using OpenGL, and take this opportunity to learn Python better. I'm looking for a good library for vector math. I need to do vector addition, cross products, dot products etc. and probably in the future I'll need matrix math as well. So far I've used the Scientific library, which is very nice, but unfortunately it crashes when I...
16
4439
by: Martin Jørgensen | last post by:
Hi, I get this using g++: main.cpp:9: error: new types may not be defined in a return type main.cpp:9: note: (perhaps a semicolon is missing after the definition of 'vector') main.cpp:9: error: two or more data types in declaration of 'set' I don't really see the problem... Here's the code:
1
1797
by: raylegendkiller | last post by:
NEED TO MAKE A PROGRAM which computes the current value of the vectors {x} based on the following forward iterations: this >>> {x}(n+1) = {x}(n), n = 0,1,2, ... ,8,9. In other words, the next vector {x} is equal to the product of and the current vector {x}. Perform the matrix multiplication by using the function:
4
3040
by: helge | last post by:
What is the best way to implement a vector in space R3, i.e a vector holding three floats, supporting arithmetic operations, dot and cross product etc in c++? is there a standard library class for this?
1
7816
jwwicks
by: jwwicks | last post by:
Hello All, This is a student assignment. So I don't want the complete answer just a hint or maybe a bumb on the head cause I'm doing it the wrong way. Assume I haven't done anything braindead like not include a header etc... I can post the whole code if you like/need it but I'm trying to spare the forum :) Got a product structure... struct product { string id; string description; int quantity;
0
9336
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.