Skip to content

Instantly share code, notes, and snippets.

@chetanyachopra
Last active February 20, 2018 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chetanyachopra/23e800e08a5a4962453ee98be27b1624 to your computer and use it in GitHub Desktop.
Save chetanyachopra/23e800e08a5a4962453ee98be27b1624 to your computer and use it in GitHub Desktop.
Vector3D class contains basic functionality like adding, subtracting, dot product, cross product
/*
Coded by Chetanya Chopra
github : http://github.com/chetanyachopra
*/
/*A basic 3d vector Class */
#include<iostream>
using namespace std;
//class contians basic functionality of vectors
class Vector3D {
private:
double x, y, z;
public:
// __________defines a basic vector_________________________
Vector3D(double a = 0.0, double b = 0.0, double c = 0.0) : x(a), y(b), z(c){}
// ______Operational functions________________
//add two vectors
friend Vector3D operator + (Vector3D, Vector3D);
//subtract two vectors
friend Vector3D operator - (Vector3D, Vector3D);
//dot product of two vectors
friend double operator * (Vector3D, Vector3D);
Vector3D crossProduct(Vector3D);
//function to display a vectr
friend ostream& operator << (ostream& os, Vector3D);
//check funccc han ya nai
string isEqual(Vector3D);
};
string Vector3D :: isEqual(Vector3D vec) {
return (this->x == vec.x && this->y == vec.y && this->z == vec.z) ? "YES" : "NO";
}
//method to add tow vectors
Vector3D operator + (Vector3D const v1, Vector3D const v2) {
return Vector3D(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}
Vector3D operator - (Vector3D const v1, Vector3D const v2) {
return Vector3D(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}
// dot product of two vectors
double operator * (Vector3D const v1, Vector3D const v2) {
Vector3D vec = Vector3D(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
return vec.x + vec.y + vec.z;
}
//cross product of two vectors
Vector3D Vector3D :: crossProduct(Vector3D vec) {
return Vector3D(this->y * vec.z - this->z * vec.y,
this->x * vec.z - this->z * vec.x,
this->x * vec.x - this->y * vec.x);
}
//method to display vector in readable form
ostream& operator << (ostream& os, Vector3D vec) {
cout<< vec.x << " " << vec.y << " " << vec.z <<endl;
}
int main() {
cout<<"2 vectors are "<< endl;
//Vector3D vec;
Vector3D vec2(1.0, 2.0, 3.0);
Vector3D vec1(3.8, 5.4, 3.2);
cout<<"a = " <<vec1 << endl;
cout<<"b = " << vec2 << endl;
Vector3D res;
//adding 2 vec
res = vec1 + vec2;
cout<< "On Adding 2 vectors : " << res << endl;
//sub 2 vec
res = vec2 - vec1;
cout<<"On Subtracting 2 vectors : "<< res << endl;
// * symbolises simple dot product
cout <<"dot Product of 2 vectors : " << vec1 * vec2 << endl;
//cross product of 2 vectors
res = vec1.crossProduct(vec2);
cout<<"Cross Product of 2 vectors : " << res << endl;
}
@chetanyachopra
Copy link
Author

Basic Functionality of 3D vector class is implemented like

  • adding
  • subtracting
  • dotproduct
  • crossproduct .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment