#ifndef _OF_VEC2f
#define _OF_VEC2f


#ifndef DEG_TO_RAD
	#define DEG_TO_RAD 0.0174532925199433
#endif

#ifndef RAD_TO_DEG
	#define RAD_TO_DEG 57.2957795130823
#endif



class ofVec2f {

  public:
	
	
    //----------------  Constructors
    
    ofVec2f( const float _x = 0.0f, const float _y = 0.0f )
    {
        x = _x;
        y = _y;
    }
	
    ofVec2f( const ofVec2f& vec )  
    {
        x = vec.x;
        y = vec.y;
    }
    
    
    
    
    //---------------- Data Members
    float x;
    float y;
    
   
   
   
    //---------------- Methods


    // Getters and Setters.
    //
    //     
    void set( float _x, float _y )
    {
        x = _x;
        y = _y;
    }
    
    void set( const ofVec2f &vec )
    {
        x = vec.x;
        y = vec.y;
    }

    float &operator[]( const int &i )           
    { 
        switch(i) {
            case 0:  return x;
            case 1:  return y;
            default: return x;
        }
    }

    
    
    // Check similarity/equality.
    //
    //        
    bool operator==( const ofVec2f vec )           
    { 
        return (x == vec.x) && (y == vec.y);
    }

    bool operator!=( const ofVec2f vec )           
    { 
        return (x != vec.x) || (y != vec.y);
    }
    
    bool match( const ofVec2f vec, float tollerance=0.0001) {
        return (fabs(x - vec.x) < tollerance) 
            && (fabs(y - vec.y) < tollerance);
    }    

    /**
    * Checks if vectors look in the same direction.
    */      
    bool align( const ofVec2f& vec ) const
    {
        return (x*vec.x + y*vec.y) > 0.0;
    }    
    
    

    // Additions and Subtractions.
    //
    //
    ofVec2f operator+( const ofVec2f& vec ) const          
    { 
        return ofVec2f( x+vec.x, y+vec.y); 
    } 
    
    ofVec2f& operator+=( const ofVec2f& vec )              
    { 
        x += vec.x;
        y += vec.y;
        return *this; 
    } 
    
    ofVec2f operator-( const ofVec2f& vec ) const          
    { 
        return ofVec2f( x-vec.x, y-vec.y ); 
    } 
    
    ofVec2f& operator-=( const ofVec2f& vec )              
    { 
        x -= vec.x;
        y -= vec.y;
        return *this; 
    } 

    ofVec2f operator-() const                               
    { 
        return ofVec2f( -x, -y ); 
    }
    


    // Scalings
    //
    //
    ofVec2f operator*( const ofVec2f& vec ) const
    { 
        return ofVec2f( x*vec.x, y*vec.y ); 
    } 
    
    ofVec2f& operator*=( const ofVec2f& vec )      
    { 
        x *= vec.x;
        y *= vec.y;
        return *this; 
    } 

    ofVec2f operator*( const float f ) const                
    { 
        return ofVec2f( x*f, y*f );
    } 
    
    ofVec2f& operator*=( const float f )                    
    { 
        x *= f;
        y *= f;
        return *this; 
    } 
    
    ofVec2f operator/( const ofVec2f& vec ) const  
    { 
        return ofVec2f( x/vec.x, y/vec.y );
    } 
    
    ofVec2f& operator/=( const ofVec2f& vec )      
    { 
        x /= vec.x;
        y /= vec.y;
        return *this; 
    } 

    ofVec2f operator/( const float f ) const      
    { 
        return ofVec2f( x/f, y/f );
    } 

    ofVec2f& operator/=( const float f )          
    { 
        x /= f;
        y /= f;
        return *this; 
    }
    
    ofVec2f rescaled( const float length ) const
    { 
        float l = (float)sqrt(x*x + y*y);
        if( l > 0 )
            return ofVec2f( (x/l)*length, (y/l)*length );
        else
            return ofVec2f();
    }
        
    ofVec2f& rescale( const float length )
    { 
        float l = (float)sqrt(x*x + y*y);
        if (l > 0) {
            x = (x/l)*length;
            y = (y/l)*length;
        }
        return *this;
    }



    // Rotation
    //
    //     
    ofVec2f rotated( float angle ) const
    {
        float a = angle * DEG_TO_RAD;
        return ofVec2f( x*cos(a) - y*sin(a), 
                        x*sin(a) + y*cos(a) );
    }
    
    ofVec2f& rotate( float angle )
    {
        float a = angle * DEG_TO_RAD;
        float xrot = x*cos(a) - y*sin(a);
        y = x*sin(a) + y*cos(a);
        x = xrot;
        return *this;
    }   
    
    

    // Normalization
    //
    //
    ofVec2f normalized() const 
    { 
        float lenght = (float)sqrt(x*x + y*y);
        if( lenght > 0 ) {
            return ofVec2f( x/lenght, y/lenght ); 
        } else { 
            return ofVec2f(); 
        }
    } 

    ofVec2f& normalize()                  
    { 
        float lenght = (float)sqrt(x*x + y*y);
        if( lenght > 0 ) { 
            x /= lenght; 
            y /= lenght; 
        }
        return *this;
	} 
    
    

    // Limit length.
    //
    //         
	 ofVec2f limited(float max) const
     {
        float length = (float)sqrt(x*x + y*y);
		if( length > max && length > 0 ) {
            return ofVec2f( (x/length)*max, (y/length)*max );
		} else {
            return ofVec2f( x, y ); 
        }
	}
    
    ofVec2f& limit(float max)
    {
        float length = (float)sqrt(x*x + y*y);
		if( length > max && length > 0 ) {
            x = (x/length)*max;
            y = (y/length)*max;
        }
        return *this;
    }    
    


    // Perpendicular normalized vector.
    //
    //          
    ofVec2f perpendicular() const
    {
        float length = (float)sqrt( x*x + y*y );
        if( length > 0 )
            return ofVec2f( -(y/length), x/length );
        else
            return ofVec2f();
    }    
    


    // Length
    //
    //
    float length() const                
    { 
        return (float)sqrt( x*x + y*y ); 
    } 
    
    float lengthSquared() const                
    { 
        return (float)(x*x + y*y); 
    } 
    
    

    /**
    * Angle (deg) between two vectors.
    */
    float angle( const ofVec2f& vec ) const
    {
        return atan2( vec.y - y, vec.x - x )*RAD_TO_DEG;
    }
    
    
    
    /**
    * Dot Product.
    */    
    float dot( const ofVec2f& vec ) const
    {     
        return x*vec.x + y*vec.y;
    }    
	
};

#endif