Skip to content
Snippets Groups Projects
Commit 145b5495 authored by haquin elouan's avatar haquin elouan
Browse files

[Wip] Ajout de la classe vecteur

parent e61f99ae
Branches
No related merge requests found
#pragma once
#include <cmath>
#define PI 3.14159f
template <typename T>
class Vector2D
{
private:
T x;
T y;
T z;
public:
//BUILD FONCTION
Vector2D(bool isMadeWithAngle, T m_x, T m_y, T m_z)
{
if (isMadeWithAngle)
{
this->x = m_x*cos(m_y);
this->y = m_x*cos(90 - m_y);
}
else
{
this->x = m_x;
this->y = m_y;
this->z = m_z;
}
}
Vector2D(Vector2D& other) { this->set_X(other.get_X()); this->set_Y(other.get_Y()); this->set_Z(other.get_Z());}
~Vector2D() {};
//GETTERS
T get_X()const { return this->x; }
T get_Y()const { return this->y; }
T get_Z()const { return this->z; }
//SETTERS
void set_Y(T m_y) { this->y = m_y; }
void set_X(T m_x) { this->x = m_x; }
void set_Z(T m_z) { this->z = m_z; }
//OVERLOADING OPERATORS
Vector2D& operator=(const Vector2D& other) { this->set_X(other.get_X()); this->set_Y(other.get_Y()); this->set_Z(other.get_Z()); return *this; }
Vector2D& operator=(Vector2D& other) {this->set_X(other.get_X()); this->set_Y(other.get_Y()); this->set_Z(other.get_Z()); return *this; }
Vector2D operator*(const Vector2D& other) { Vector2D<float> vec1(false, 0, 0,0); vec1.set_X(other.get_X() * this->get_X()); vec1.set_Y(other.get_Y() * this->get_Y()); vec1.set_Z(other.get_Z() * this->get_Z()); return vec1; }
Vector2D operator+(const Vector2D& other) { Vector2D<float> vec1(false, other.get_X() + this->get_X(), other.get_Y() + this->get_Y(), other.get_Z() + this->get_Z()); return vec1; }
Vector2D operator-(const Vector2D& other) { Vector2D<float> vec1(false, 0, 0, 0); vec1.set_X(this->get_X() - other.get_X()); vec1.set_Y(this->get_Y() - other.get_Y()); vec1.set_Z(other.get_Z() - this->get_Z()); return vec1; }
Vector2D operator*(float scalaire) { Vector2D<float> vec1(false, 0, 0, 0); vec1.set_X(this->get_X() * scalaire); vec1.set_Y(this->get_Y() * scalaire); vec1.set_Z(this->get_Z() * scalaire); return vec1; }
Vector2D operator+(float scalaire) { Vector2D<float> vec1(false, 0, 0, 0); vec1.set_X(this->get_X() + scalaire); vec1.set_Y(this->get_Y() + scalaire); vec1.set_Z(this->get_Z() + scalaire); return vec1; }
Vector2D operator-(float scalaire) { Vector2D<float> vec1(false, 0, 0, 0); vec1.set_X(this->get_X() - scalaire); vec1.set_Y(this->get_Y() - scalaire); vec1.set_Z(this->get_Z() - scalaire); return vec1; }
//VECTORS THINGS
long int scalar_Between(Vector2D& vec1) { return (vec1.get_X() * this->get_X()) + (vec1.getY() * this->get_Y()); }
long double get_Norme() { return sqrt((this->get_X()*this->get_X()) + (this->get_Y()*this->get_Y()) + (this->get_Z()*this->get_Z())); }
long double angle_With(Vector2D& vec1) { double vecAngle1 = cos(this->get_Norme())* 180.0 / PI; double vecAngle2 = cos(vec1.get_Norme())* 180.0 / PI; return vecAngle1 - vecAngle2; }
void normalize() { double length = this->get_Norme(); this->set_X(this->get_X() / length); this->set_Y(this->get_Y() / length);}
//TEST FUNCTIONS
bool isPerpendicular(Vector2D& vec2) { if (angle_With(this, vec2) == 90)return true; return false; }
bool isNull() { if (this.get_X() == 0 && this.get_Y() == 0)return true; return false; }
bool isParallel(Vector2D& vec2) { if (this->get_X() / vec2.get_X() == this->get_Y() / vec2.get_Y())return true; return false; }
bool isOpposed(Vector2D& vec2) { if (this->isParallel(vec2) && this->get_X() + vec2.get_X() < this->get_X() && this->get_Y() + vec2.get_Y() < this->get_Y())return true; return false; }
bool isNormalised() { if (this->get_Norme() == 1) { return true; } else return false; }
};
template <typename T>
std::ostream& operator<<(std::ostream& stream, Vector2D<T> & vec) { stream << vec.get_X() << " " << vec.get_Y() << " " << vec.get_Z(); return stream; }
\ No newline at end of file
#include <iostream>
#include "Vector2D.h"
int main()
{
Vector2D<float> v1( false, 1.0, -3.0f, 2.0f);
Vector2D<float> v2(false, -4.0f, -1.0f, 0.0f);
std::cout << "1) v1 + v2 = " << v1 + v2 << std::endl;
std::cout << "2) v1 - v2 = " << v1 - v2 << std::endl;
std::cout << "3) 2v1 = " << v1 * 2.0f << std::endl;
std::cout << "4) 3v1 - 2v2 = " << v1 * 3.0f - v2 * 2.0f << std::endl;
std::cout << "5) |v1| = " << v1.get_Norme() << std::endl;
std::cout << "6) |v2| = " << v2.get_Norme() << std::endl;
std::cout << "7) |v1| + |v2| = " << v1.get_Norme() + v2.get_Norme() << std::endl;
std::cout << "8) |2v1| = " << (v1 * 2.0f).get_Norme() << std::endl;
std::cout << "9) |v1 + v2| = " << (v1 + v2).get_Norme() << std::endl;
system("Pause");
return 0;
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment