Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- class Vector3
- {
- private:
- double _x, _y, _z;
- public:
- Vector3();
- Vector3(double, double, double);
- ~Vector3();
- void set(double, double, double);
- void setX(double);
- void setY(double);
- void setZ(double);
- double getX();
- double getY();
- double getZ();
- void print();
- double length();
- Vector3 normalized();
- Vector3 vproduct(Vector3 v);
- };
- // Operators
- Vector3 operator+(Vector3 a, Vector3 b)
- {
- Vector3 t(a.getX() + b.getX(), a.getY() + b.getY(), a.getZ() + b.getZ());
- return t;
- }
- Vector3 operator-(Vector3 a)
- {
- Vector3 t(-a.getX(), -a.getY(), -a.getZ());
- return t;
- }
- Vector3 operator-(Vector3 a, Vector3 b)
- {
- return a + (-b);
- }
- double operator*(Vector3 a, Vector3 b)
- {
- double t(a.getX()*b.getX() + a.getY()*b.getY() + a.getZ()*b.getZ());
- return t;
- }
- Vector3 operator*(Vector3 a, double b)
- {
- Vector3 t(a.getX()*b, a.getY()*b, a.getZ()*b);
- return t;
- }
- Vector3 operator*(double a, Vector3 b)
- {
- return b*a;
- }
- Vector3 operator/(Vector3 a, double b)
- {
- return a*(1./b);
- }
- bool operator==(Vector3 a, Vector3 b)
- {
- return a.getX() == b.getX() && a.getY() == b.getY() && a.getZ() == b.getZ();
- }
- bool operator!=(Vector3 a, Vector3 b)
- {
- return !(a == b);
- }
- // Constructor/Destructor
- Vector3::Vector3()
- {
- _x = 0.;
- _y = 0.;
- _z = 0.;
- }
- Vector3::Vector3(double x, double y, double z)
- {
- _x = x;
- _y = y;
- _z = z;
- }
- Vector3::~Vector3()
- {
- }
- // Modifiers
- void Vector3::set(double x, double y, double z)
- {
- _x = x;
- _y = y;
- _z = z;
- }
- void Vector3::setX(double x)
- {
- _x = x;
- }
- void Vector3::setY(double y)
- {
- _y = y;
- }
- void Vector3::setZ(double z)
- {
- _z = z;
- }
- // Accessors
- double Vector3::getX()
- {
- return _x;
- }
- double Vector3::getY()
- {
- return _y;
- }
- double Vector3::getZ()
- {
- return _z;
- }
- // Methods
- void Vector3::print()
- {
- std::cout << "Vector3 (" << _x << ", " << _y << ", " << _z << ")\n";
- }
- double Vector3::length()
- {
- return sqrt((*this) * (*this));
- }
- Vector3 Vector3::normalized()
- {
- return *this / this->length();
- }
- Vector3 Vector3::vproduct(Vector3 v)
- {
- Vector3 t(this->_y*v._z - this->_z*v._y,
- this->_z*v._x - this->_x*v._z,
- this->_x*v._y - this->_y*v._x);
- return t;
- }
- // Main
- int main()
- {
- Vector3 v(3, 5, 8.2455);
- v.print();
- v = v*6 ;
- v.print();
- v = v+v;
- v.print();
- v = v.normalized();
- v.print();
- v = v.vproduct(v);
- v.print();
- std::cout << "Done\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment