#include "include.h"


Vector3D::Vector3D() {
	x = 0;
	y = 0;
	z = 0;
}

Vector3D::Vector3D(double x, double y, double z) {
	this->x = x;
	this->y = y;
	this->z = z;
}

Vector3D& Vector3D::operator= (Vector3D v) {
	x = v.x;
	y = v.y;
	z = v.z;
	return *this;
}

Vector3D Vector3D::operator+ (Vector3D v)	{
	return Vector3D(x + v.x, y + v.y, z + v.z);
}

Vector3D Vector3D::operator- (Vector3D v)	{
	return Vector3D(x - v.x, y - v.y, z - v.z);
}

Vector3D Vector3D::operator* (double value) {
	return Vector3D(x * value, y * value, z * value);
}

Vector3D Vector3D::operator/ (double value) {
	return Vector3D(x / value, y / value, z / value);
}

Vector3D& Vector3D::operator+= (Vector3D v) {
	x += v.x;
	y += v.y;
	z += v.z;
	return *this;
}

Vector3D& Vector3D::operator-= (Vector3D v) {
	x -= v.x;
	y -= v.y;
	z -= v.z;
	return *this;
}

Vector3D& Vector3D::operator*= (double value) {
	x *= value;
	y *= value;
	z *= value;
	return *this;
}

Vector3D& Vector3D::operator/= (double value) {
	x /= value;
	y /= value;
	z /= value;
	return *this;
}

Vector3D Vector3D::operator- () {
	return Vector3D(-x, -y, -z);
}

double Vector3D::length() {
	return sqrt(x*x + y*y + z*z);
}

double Vector3D::dotProduct(Vector3D v) {
	return x * v.x + y * v.y + z * v.z;
}

Vector3D Vector3D::crossProduct(Vector3D v) {
	return Vector3D(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x);
}

void Vector3D::unitize() {
	double length = this->length();

	if (length == 0)
		return;

	x /= length;
	y /= length;
	z /= length;
}

Vector3D Vector3D::unit() {
	double length = this->length();

	if (length == 0)
		return *this;
	
	return Vector3D(x / length, y / length, z / length);
}

void Vector3D::rotate(Vector3D eje, double angulo) {
	double ux = eje.x * x;
	double uy = eje.x * y;
	double uz = eje.x * z;
	double vx = eje.y * x;
	double vy = eje.y * y;
	double vz = eje.y * z;
	double wx = eje.z * x;
	double wy = eje.z * y;
	double wz = eje.z * z;
	double sa = sin(angulo*PI/180);
	double ca = cos(angulo*PI/180);
	x = eje.x*(ux+vy+wz) + (x*(eje.y*eje.y+eje.z*eje.z) - eje.x*(vy+wz))*ca + (-wy+vz)*sa;
	y = eje.y*(ux+vy+wz) + (y*(eje.x*eje.x+eje.z*eje.z) - eje.y*(ux+wz))*ca + (wx-uz)*sa;
	z = eje.z*(ux+vy+wz) + (z*(eje.x*eje.x+eje.y*eje.y) - eje.z*(ux+vy))*ca + (-vx+uy)*sa;
}
