Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.8 kB
3
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

class Vector2{
    public:
        float x, y;
        
        //1
        Vector2(){ 
            x = 0;
            y = 0;
        }
        
        //1
        Vector2(float a, float b){
            x = a;
            y = b;
        }
        
        //3(etc hahha)
        void up(){
            x = 0;
            y = -1;
        }
        
        //5
        float getMagnitude(){
            return sqrt(x*x + y*y);
        }
        
        //2 - normalize overriden
        void normalize(){
            float magnitude = getMagnitude();
            if(magnitude == 0) return;
            x /= magnitude;
            y /= magnitude;
        }
        
        /*
        //2 - normalize new instance(inda ko kung pano ni basta arog kani dapat)
        Vector2 normalize(){
            Vector2 res(x, y);
            res.normalize();
            return res;
        }
        */
        
        //4 - Operators(+, +=, ==, != lang)
        Vector2 operator+(Vector2 const& v){
            Vector2 res;
            res.x = x + v.x;
            res.y = y + v.y;
            return res;
        }
        
        void operator+=(Vector2 const& v){
            x += v.x;
            y += v.y;
        }
        
        bool operator==(Vector2 const& v){
            return x == v.x && y == v.y;
        }
        
        bool operator!=(Vector2 const& v){
            return x != v.x && y != v.y;
        }
        
         
        void print(){
           cout << x << ", " << y << "\n";
        }
        
};

int main() {
    Vector2 a(1,2), b(1,5);
    a = a+b;
    a += b;
    if(a == b){
        cout << "OK\n";
    }
    //a.up();
    a.normalize();
    a.print();
    
    return 0;
}