Skip to content

Instantly share code, notes, and snippets.

@naveen17797
Created November 12, 2018 03:52
Show Gist options
  • Save naveen17797/d5a0cebd875f49834b0747def0cceb22 to your computer and use it in GitHub Desktop.
Save naveen17797/d5a0cebd875f49834b0747def0cceb22 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<cmath>
using namespace std;
typedef float f;
class vector3D {
f x=0,y=0,z=0;
f get_square();
public:
vector3D(f x, f y, f z);
vector3D(const vector3D& vect);
f get_x() const;
f get_y() const;
f get_z() const;
void set_x(f x);
void set_y(f y);
void set_z(f z);
f get_magnitude();
vector3D get_normalised_vector();
void displayVector();
f get_dot_product(const vector3D& vect);
vector3D get_cross_product(const vector3D& vect);
//IMPLEMENT ASMD operators (Addition, subtraction, multiplication, division)
vector3D operator+(const vector3D vect);
vector3D operator-(const vector3D vect);
vector3D operator*(f val);
vector3D operator/(f val);
//IMPLEMENT ASMD OPERATORS with assigning
vector3D &operator+=(const vector3D &vect);
vector3D &operator-=(const vector3D &vect);
vector3D &operator*=(f val);
vector3D &operator/=(f val);
};
vector3D::vector3D(f x, f y, f z):x(x), y(y), z(z) {
}
vector3D::vector3D(const vector3D& vect)
{
x=vect.get_x();
y=vect.get_y();
z=vect.get_z();
}
f vector3D::get_x() const {
return this->x;
}
f vector3D::get_y() const {
return this->y;
}
f vector3D::get_z() const {
return this->z;
}
void vector3D::set_x(f x) {
this->x = x;
}
void vector3D::set_y(f y) {
this->y = y;
}
void vector3D::set_z(f z) {
this->z = z;
}
f vector3D::get_magnitude() {
return sqrt(get_square());
}
f vector3D::get_square() {
return x*x + y*y + z*z;
}
void vector3D::displayVector() {
cout << "(" << x << "," << y << "," << z << ")" << endl;
}
vector3D vector3D::get_normalised_vector() {
if (get_magnitude() != 0) {
f magnitude = get_magnitude();
x = x / magnitude;
y = y / magnitude;
z = z / magnitude;
}
return *this;
}
f vector3D::get_dot_product(const vector3D& vect) {
return x*vect.get_x() + y*vect.get_y() + z*vect.get_z();
}
vector3D vector3D::get_cross_product(const vector3D& vect) {
f ni=y*vect.get_z()-z*vect.get_y();
f nj=z*vect.get_x()-x*vect.get_z();
f nk=x*vect.get_y()-y*vect.get_x();
return vector3D(ni,nj,nk);
}
vector3D vector3D::operator +(const vector3D vect) {
return vector3D(x+vect.get_x(),y+vect.get_y(),z+vect.get_z());
}
vector3D vector3D::operator -(const vector3D vect) {
return vector3D(x-vect.get_x(),y-vect.get_y(),z-vect.get_z());
}
vector3D vector3D::operator *(f val) {
return vector3D(x*val, y*val, z*val);
}
vector3D vector3D::operator /(f val) {
if (val != 0)
{
return vector3D(x/val, y/val, z/val);
}
else {
return *this;
}
}
vector3D &vector3D::operator +=(const vector3D& vect) {
x = x + vect.get_x();
y = y + vect.get_y();
z = z + vect.get_z();
return *this;
}
vector3D &vector3D::operator -=(const vector3D& vect) {
x = x - vect.get_x();
y = y - vect.get_y();
z = z - vect.get_z();
return *this;
}
vector3D &vector3D::operator*=(f val)
{
x*=val;
y*=val;
z*=val;
return *this;
}
vector3D &vector3D::operator/=(f val)
{
if (val != 0) {
x/=val;
y/=val;
z/=val;
}
return *this;
}
int main() {
vector3D v1(1,1,1);
vector3D v2(1,1,1);
//addition
v1 += v2;
v1.displayVector();
//subtraction
v1 -= v2;
v1.displayVector();
//multiply
v1 *= 3;
v1.displayVector();
//division
v1 /= 2;
v1.displayVector();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment