Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef WS_MATH_VECTOR_
- #define WS_MATH_VECTOR_
- #include "ws/io.hpp"
- namespace ws {
- namespace math {
- template<size_t length = 3, class type = float>
- class vector {
- //protected:
- public:
- // public, else I can't do ostream << overload ;_;
- type data[length];
- vector(){
- for(size_t i=0; i<length; i++)
- data[i] = 0;
- }
- vector(type array[length]){
- data = array;
- }
- vector& operator = (const vector& other){
- memcpy(data, other.data, length);
- return (const vector&)*this;
- }
- type& operator [] (size_t index){
- return data[index];
- }
- vector& operator *= (const vector& other){
- for(size_t i=0; i<length; i++)
- data[i] *= other[i];
- return *this;
- }
- vector& operator /= (const vector& other){
- for(size_t i=0; i<length; i++)
- data[i] /= other[i];
- return *this;
- }
- vector& operator += (const vector& other){
- for(size_t i=0; i<length; i++)
- data[i] += other[i];
- return *this;
- }
- const vector& operator -= (const vector& other){
- for(size_t i=0; i<length; i++)
- data[i] -= other[i];
- return *this;
- }
- vector operator * (const vector& other) {
- vector temp;
- for(size_t i=0; i<length; i++)
- temp.data[i] = data[i] * other.data[i];
- return temp;
- }
- vector operator / (const vector& other) {
- vector temp;
- for(size_t i=0; i<length; i++)
- temp.data[i] = data[i] / other.data[i];
- return temp;
- }
- vector operator + (const vector& other) {
- vector temp;
- for(size_t i=0; i<length; i++)
- temp.data[i] = data[i] + other.data[i];
- return temp;
- }
- vector operator - (const vector& other) {
- vector temp;
- for(size_t i=0; i<length; i++)
- temp.data[i] = data[i] - other.data[i];
- return temp;
- }
- vector operator * (const type& scalar){
- vector temp;
- for(size_t i=0; i<length; i++)
- temp.data[i] = data[i] * scalar;
- return temp;
- }
- vector operator / (const type& divisor){
- vector temp;
- for(size_t i=0; i<length; i++)
- temp.data[i] = data[i] / divisor;
- return temp;
- }
- };
- }
- template<size_t length, class type>
- std::ostream& operator << (std::ostream& o, const math::vector<length, type> out){
- o << "vector(";
- for(size_t i=0; i<length; i++)
- o << out.data[i] << ((i != length-1) ? ", " : ")");
- return o;
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement