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

#1 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • 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   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#3 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#5 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#6 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1586
  • View blog
  • Posts: 8,405
  • Joined: 07-September 06

Re: 3D Vector Dot Product

Posted 19 March 2009 - 03:38 PM

Don't forget that it requires std::cout
Was This Post Helpful? 0
  • +
  • -

#7 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

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

Re: 3D Vector Dot Product

Posted 11 February 2012 - 07:35 AM

edited for correctness
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1