G43riko
4/27/2017 - 4:14 PM

Vectors.cpp

#include <stdio.h> 
#include <iostream>  
#include <math.h>  
class Vector2f{
private:
	float _x, _y;
public:
	/*CONSTRUCTORS*/
	inline Vector2f(void) : Vector2f(0.0f, 0.0f){};
	inline Vector2f(const Vector2f& vec) : Vector2f(vec._x, vec._y){};
	inline Vector2f(const float i_x, const float i_y) : _x(i_x), _y(i_y){};

	/*OPERATORS*/
	inline Vector2f& operator *= (const float& v){ _x *= v, _y *= v;return *this; }
	inline Vector2f operator *= (const Vector2f& v){ _x *= v._x, _y *= v._y;return *this; }
	inline Vector2f operator * (const Vector2f &v)const{ return {_x * v._x, _y * v._y}; }
	inline Vector2f operator * (const float& v)const{ return {_x * v, _y * v}; }

	inline Vector2f& operator /= (const float& v){ _x /= v, _y /= v;return *this; }
	inline Vector2f operator /= (const Vector2f& v){ _x /= v._x, _y /= v._y;return *this; }
	inline Vector2f operator / (const Vector2f &v) const{ return {_x / v._x, _y / v._y}; }
	inline Vector2f operator / (const float& v)const{ return {_x / v, _y / v}; }
	
	inline Vector2f& operator += (const float& v){ _x += v, _y += v;return *this; }
	inline Vector2f operator += (const Vector2f& v){ _x += v._x, _y += v._y;return *this; }
	inline Vector2f operator + (const Vector2f &v)const{ return {_x + v._x, _y + v._y}; }
	inline Vector2f operator + (const float& v)const{ return {_x + v, _y + v}; }
	
	inline Vector2f& operator -= (const float& v){ _x -= v, _y -= v;return *this; }
	inline Vector2f operator -= (const Vector2f& v){ _x -= v._x, _y -= v._y;return *this; }
	inline Vector2f operator - (const Vector2f &v)const{ return {_x - v._x, _y - v._y}; }
	inline Vector2f operator - (const float& v)const{ return {_x - v, _y - v}; }

	inline Vector2f& operator = (const Vector2f& v){_x = v._x, _y = v._y;return *this;}

	friend std::ostream& operator << (std::ostream& os, const Vector2f& v){
		os << "[" << v._x << ", " << v._y << "]";
		return os;
	};

	/*OTHERS*/
	inline float lengthSquared() const{return _x * _x + _y * _y;}
	inline float length(void) const{return (float)sqrt(_x * _x + _y * _y);}
	inline Vector2f getNormal(void)const {float l = (float)sqrt(_x * _x + _y * _y); return {_x / l, _y / l};}
	inline Vector2f& normalize(void){float l = (float)sqrt(_x * _x + _y * _y); _x /= l, _y /= l;return *this;}

	inline float dot(const Vector2f& vec) const{return _x * vec._x + _y * vec._y;}

	inline void show(void) const{printf("[%f, %f]\n", _x, _y);}
	/*GETTERS*/
	inline float getX(void) const{return _x;}
	inline float getY(void) const{return _y;}
	inline int getXi(void) const{return (int)_x;}
	inline int getYi(void) const{return (int)_y;}

	/*SETTERS*/
	inline void setX(const float i_x){_x = i_x;}
	inline void setY(const float i_y){_y = i_y;}
};

class Vector3f{
private:
	float _x, _y, _z;
public:
	/*CONSTRUCTORS*/
	inline Vector3f(void) : Vector3f(0.0f, 0.0f, 0.0f){};
	inline Vector3f(const Vector3f& vec) : Vector3f(vec._x, vec._y, vec._z){};
	inline Vector3f(const float i_x, const float i_y, const float i_z) : _x(i_x), _y(i_y), _z(i_z){};

	/*OPERATORS*/
	inline Vector3f& operator *= (const float& v){ _x *= v, _y *= v, _z *= v;return *this; }
	inline Vector3f operator *= (const Vector3f& v){ _x *= v._x, _y *= v._y, _z *= v._z;return *this; }
	inline Vector3f operator * (const Vector3f &v)const{ return {_x * v._x, _y * v._y, _z * v._z}; }
	inline Vector3f operator * (const float& v)const{ return {_x * v, _y * v, _z * v}; }

	inline Vector3f& operator /= (const float& v){ _x /= v, _y /= v, _z /= v;return *this; }
	inline Vector3f operator /= (const Vector3f& v){ _x /= v._x, _y /= v._y, _z /= v._z;return *this; }
	inline Vector3f operator / (const Vector3f &v) const{ return {_x / v._x, _y / v._y, _z / v._z}; }
	inline Vector3f operator / (const float& v)const{ return {_x / v, _y / v, _z / v}; }
	
	inline Vector3f& operator += (const float& v){ _x += v, _y += v, _z += v;return *this; }
	inline Vector3f operator += (const Vector3f& v){ _x += v._x, _y += v._y, _z += v._z;return *this; }
	inline Vector3f operator + (const Vector3f &v)const{ return {_x + v._x, _y + v._y, _z + v._z}; }
	inline Vector3f operator + (const float& v)const{ return {_x + v, _y + v, _z + v}; }
	
	inline Vector3f& operator -= (const float& v){ _x -= v, _y -= v, _z -= v;return *this; }
	inline Vector3f operator -= (const Vector3f& v){ _x -= v._x, _y -= v._y, _z -= v._z;return *this; }
	inline Vector3f operator - (const Vector3f &v)const{ return {_x - v._x, _y - v._y, _z - v._z}; }
	inline Vector3f operator - (const float& v)const{ return {_x - v, _y - v, _z - v}; }

	inline Vector3f& operator = (const Vector3f& v){_x = v._x, _y = v._y, _z = v._z;return *this;}

	friend std::ostream& operator << (std::ostream& os, const Vector3f& v){
		os << "[" << v._x << ", " << v._y << ", " << v._z << "]";
		return os;
	};

	
	/*OTHERS*/
	inline float lengthSquared() const{return _x * _x + _y * _y + _z * _z;}
	inline float length(void) const{return (float)sqrt(_x * _x + _y * _y + _z * _z);}
	inline Vector3f getNormal(void)const {float l = (float)sqrt(_x * _x + _y * _y + _z * _z); return {_x / l, _y / l, _z / l};}
	inline Vector3f& normalize(void){float l = (float)sqrt(_x * _x + _y * _y + _z * _z); _x /= l, _y /= l, _z /= l;return *this;}

	inline float dot(const Vector3f* vec) const{return _x * vec -> _x + _y * vec -> _y + _z * vec -> _z;}
	inline float dot(const Vector3f& vec) const{return _x * vec._x + _y * vec._y + _z * vec._z;}

	inline Vector3f getCross(const Vector3f& v) const{return {_y * v._z - _z * v._y, _z * v._x - _x * v._z, _x * v._y - _y * v._x};}
	inline Vector3f& cross(const Vector3f& v){
		float x = _y * v._z - _z * v._y;
        float y = _z * v._x - _x * v._z;
        float z = _x * v._y - _y * v._x;
        _x = x, _y = y, _z = z;
        return *this;
	}
	inline void show(void) const{printf("[%f, %f, %f]\n", _x, _y, _z);}
	/*GETTERS*/
	inline float getX(void) const{return _x;}
	inline float getY(void) const{return _y;}
	inline float getZ(void) const{return _z;}
	inline int getXi(void) const{return (int)_x;}
	inline int getYi(void) const{return (int)_y;}
	inline int getZi(void) const{return (int)_z;}

	inline Vector2f getXY(void) const {return {_x, _y};}
	inline Vector2f getXZ(void) const {return {_x, _z};}
	inline Vector2f getYZ(void) const {return {_y, _z};}
	inline Vector2f getYX(void) const {return {_y, _x};}
	inline Vector2f getZX(void) const {return {_z, _x};}
	inline Vector2f getZY(void) const {return {_z, _y};}

	/*SETTERS*/
	inline void setX(const float i_x){_x = i_x;}
	inline void setY(const float i_y){_y = i_y;}
	inline void setZ(const float i_z){_z = i_z;}
};
int main(){
   	Vector3f a;
   	Vector3f b(1, 2, 3);
   	Vector3f c = Vector3f(2, 3, 4);
   	Vector3f d = {3, 4, 5};
   	Vector3f * e = new Vector3f(4, 5, 6);

   	a.show();
   	b.show();
   	c.show();
   	d.show();
   	e -> show();
   	std::cout << d << "\n";

   	printf("dot: %f\n", a.dot(*e));
   	printf("dot: %f\n", a.dot(e));
	return 1;
}