# 3D Vector Dot Product

Page 1 of 1

## 3 Replies - 2747 Views - Last Post: 11 February 2012 - 07:35 AM

### #1 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,235
• Joined: 14-September 07

# 3D Vector Dot Product

Posted 19 March 2009 - 02:17 PM

Description: Implement in a 3D simulation or game perhaps? Vector class is included, but only relevant other functions are as well. Sample main included. Much simpler then cross product, this is the simple multiplication of two vectors. Useful in detecting collision.
#include <iostream>
using std::ostream;

//vector class used in the snippet
class Vector {
private:
double x, y, z;
public:
Vector()										{ x = y = z = 0; };
Vector(double pX, double pY, double pZ)			{ x = pX; y = pY; z = pZ; };
~Vector() {};
void setX(double pX)							{ x = pX; };
void setY(double pY)							{ y = pY; };
void setZ(double pZ)							{ z = pZ; };
double getX()									{ return x; };
double getY()									{ return y; };
double getZ()									{ return z; };
Vector dotProduct(Vector*);
friend ostream& operator<< (ostream& os, Vector& vect)
{
os << "[" << vect.getX() << ", " << vect.getY() << ", " <<
vect.getZ() << "]";
return os;
}
};

//focus of snippet
Vector Vector::dotProduct(Vector* vectTwo)
{
Vector temp;
temp.setX(this->getX()*vectTwo->getX());
temp.setY(this->getY()*vectTwo->getY());
temp.setZ(this->getZ()*vectTwo->getZ());
return temp;
}

//sample main
int main()
{
Vector* vectOne = new Vector(1,1,1);
Vector* vectTwo = new Vector(1,2,3);

cout << vectOne->dotProduct(vectTwo) << endl;
delete vectOne, vectTwo;
return 0;
}

Is This A Good Question/Topic? 0

## Replies To: 3D Vector Dot Product

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,235
• Joined: 14-September 07

## Re: 3D Vector Dot Product

Posted 19 March 2009 - 02:17 PM

Description: Implement in a 3D simulation or game perhaps? Vector class is included, but only relevant other functions are as well. Sample main included. Much simpler then cross product, this is the simple multiplication of two vectors. Useful in detecting collision.
#include <iostream>
using std::ostream;
using std::cout;
using std::endl;

//vector class used in the snippet
class Vector {
private:
double x, y, z;
public:
Vector()										{ x = y = z = 0; };
Vector(double pX, double pY, double pZ)			{ x = pX; y = pY; z = pZ; };
~Vector() {};
void setX(double pX)							{ x = pX; };
void setY(double pY)							{ y = pY; };
void setZ(double pZ)							{ z = pZ; };
double getX()									{ return x; };
double getY()									{ return y; };
double getZ()									{ return z; };
Vector dotProduct(Vector*);
friend ostream& operator<< (ostream& os, Vector& vect)
{
os << "[" << vect.getX() << ", " << vect.getY() << ", " <<
vect.getZ() << "]";
return os;
}
};

//focus of snippet
Vector Vector::dotProduct(Vector* vectTwo)
{
Vector temp;
temp.setX(this->getX()*vectTwo->getX());
temp.setY(this->getY()*vectTwo->getY());
temp.setZ(this->getZ()*vectTwo->getZ());
return temp;
}

//sample main
int main()
{
Vector* vectOne = new Vector(1,1,1);
Vector* vectTwo = new Vector(1,2,3);

cout << vectOne->dotProduct(vectTwo) << endl;
delete vectOne, vectTwo;
return 0;
}

### #3 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,235
• Joined: 14-September 07

## Re: 3D Vector Dot Product

Posted 19 March 2009 - 02:17 PM

Description: Implement in a 3D simulation or game perhaps? Vector class is included, but only relevant other functions are as well. Sample main included. Much simpler then cross product, this is the simple multiplication of two vectors. Useful in detecting collision. This example returns the actual vector rather then the result of the addition of each item.
#include <iostream>
using std::ostream;
using std::cout;
using std::endl;

//vector class used in the snippet
class Vector {
private:
double x, y, z;
public:
Vector()								{ x = y = z = 0; };
Vector(double pX, double pY, double pZ)			                { x = pX; y = pY; z = pZ; };
~Vector() {};
void setX(double pX)							{ x = pX; };
void setY(double pY)							{ y = pY; };
void setZ(double pZ)							{ z = pZ; };
double getX()								{ return x; };
double getY()								{ return y; };
double getZ()								{ return z; };
Vector dotProduct(Vector*);
friend ostream& operator<< (ostream& os, Vector& vect)
{
os << "[" << vect.getX() << ", " << vect.getY() << ", " <<
vect.getZ() << "]";
return os;
}
};

//focus of snippet
Vector Vector::dotProduct(Vector* vectTwo)
{
Vector temp;
temp.setX(this->getX()*vectTwo->getX());
temp.setY(this->getY()*vectTwo->getY());
temp.setZ(this->getZ()*vectTwo->getZ());
return temp;
}

//sample main
int main()
{
Vector* vectOne = new Vector(1,1,1);
Vector* vectTwo = new Vector(1,2,3);

cout << vectOne->dotProduct(vectTwo) << endl;
delete vectOne, vectTwo;
return 0;
}

### #4 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,235
• Joined: 14-September 07

## Re: 3D Vector Dot Product

Posted 19 March 2009 - 02:17 PM

Description: Implement in a 3D simulation or game perhaps? Vector class is included, but only relevant other functions are as well. Sample main included. Much simpler then cross product, this is the simple multiplication of two vectors. Useful in detecting collision. This example returns the actual vector rather then the result of the addition of each item.
#include <iostream>
using std::ostream;
using std::cout;
using std::endl;

//vector class used in the snippet
class Vector {
private:
double x, y, z;
public:
Vector()								{ x = y = z = 0; };
Vector(double pX, double pY, double pZ)			                { x = pX; y = pY; z = pZ; };
~Vector() {};
void setX(double pX)							{ x = pX; };
void setY(double pY)							{ y = pY; };
void setZ(double pZ)							{ z = pZ; };
double getX()								{ return x; };
double getY()								{ return y; };
double getZ()								{ return z; };
double dotProduct(Vector*);
friend ostream& operator<< (ostream& os, Vector& vect)
{
os << "[" << vect.getX() << ", " << vect.getY() << ", " <<
vect.getZ() << "]";
return os;
}
};

//focus of snippet
double Vector::dotProduct(Vector* vectTwo)
{
return (this->getX()*vectTwo->getX() +
this->getY()*vectTwo->getY() +
this->getZ()*vectTwo->getZ());
}

//sample main
int main()
{
Vector* vectOne = new Vector(1,1,1);
Vector* vectTwo = new Vector(1,2,3);

cout << vectOne->dotProduct(vectTwo) << endl;
delete vectOne, vectTwo;
return 0;
}

### #5 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,235
• Joined: 14-September 07

## Re: 3D Vector Dot Product

Posted 19 March 2009 - 02:17 PM

Description: Implement in a 3D simulation or game perhaps? Vector class is included, but only relevant other functions are as well. Sample main included. Much simpler then cross product, this is the simple multiplication of two vectors. Useful in detecting collision.
#include <iostream>
using std::ostream;
using std::cout;
using std::endl;

//vector class used in the snippet
class Vector {
private:
double x, y, z;
public:
Vector()								{ x = y = z = 0; };
Vector(double pX, double pY, double pZ)			                { x = pX; y = pY; z = pZ; };
~Vector() {};
void setX(double pX)							{ x = pX; };
void setY(double pY)							{ y = pY; };
void setZ(double pZ)							{ z = pZ; };
double getX()								{ return x; };
double getY()								{ return y; };
double getZ()								{ return z; };
double dotProduct(Vector*);
friend ostream& operator<< (ostream& os, Vector& vect)
{
os << "[" << vect.getX() << ", " << vect.getY() << ", " <<
vect.getZ() << "]";
return os;
}
};

//focus of snippet
double Vector::dotProduct(Vector* vectTwo)
{
return (this->getX()*vectTwo->getX() +
this->getY()*vectTwo->getY() +
this->getZ()*vectTwo->getZ());
}

//sample main
int main()
{
Vector* vectOne = new Vector(1,1,1);
Vector* vectTwo = new Vector(1,2,3);

cout << vectOne->dotProduct(vectTwo) << endl;
delete vectOne, vectTwo;
return 0;
}

### #6 BetaWar

• #include "soul.h"

Reputation: 1620
• Posts: 8,473
• Joined: 07-September 06

## Re: 3D Vector Dot Product

Posted 19 March 2009 - 03:38 PM

Don't forget that it requires std::cout

### #7 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,235
• Joined: 14-September 07

## Re: 3D Vector Dot Product

Posted 19 March 2009 - 04:21 PM

I forgot to copy/paste that part, there were separate files :) I'll edit.

### #8 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,235
• Joined: 14-September 07

## Re: 3D Vector Dot Product

Posted 11 February 2012 - 07:35 AM

edited for correctness

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }