Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Code critique - vectors.
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Martin




PostPosted: Thu Jul 27, 2006 7:14 pm   Post subject: Code critique - vectors.

CVector2f.h

c++:

#ifndef CVECTOR2F_H
#define CVECTOR2F_H


#include <cmath>
#include <algorithm>


class CVector2f
{
public:
    //Variables//
    float x, y;


    //Constructors//
    CVector2f( ): x( 0.0f ), y( 0.0f ) { }
    CVector2f( float _x, float _y ): x( _x ), y( _y ) { }


    //Functions//
   
    //Magnitude of the vector, squared( faster than norm() )
    float norm2( ) const
    {
        return x * x + y * y;
    }

    //Magnitude of the vector (slower than norm2() because of sqrt )
    float norm( ) const
    {
        return sqrt( norm2( ) );
    }

    //returns a vector of the same direction as this*, but with a norm() of 1
    //or the vector if its norm is 0
    CVector2f normal( ) const
    {
        float n = norm( );
        return n == 0 ? *this : CVector2f( x/n, y/n);
           
    }

    //Turns this vector into its normal
    void normalize( )
    {
        float n = norm( );
        if( n != 0 )
            *this /= n;
    }

    inline static CVector2f vmin( const CVector2f &a, const CVector2f &b )
    {
        return CVector2f( std::min( a.x, b.x ), std::min( a.y, b.y ) );
    }

    inline static CVector2f vmax( const CVector2f &a, const CVector2f &b )
    {
        return CVector2f( std::max( a.x, b.x), std::min( a.y, b.y ) );
    }

     
    inline static float crossProduct( const CVector2f &a, const CVector2f &b )
    {
        return a.x * b.y - b.x * a.y;
    }

    inline static float dotProduct( const CVector2f &a, const CVector2f &b )
    {
        return a.x * b.x + a.y * b.y;
    }
 

    //Operator Overloading//
    //+,-,*,/
    inline CVector2f operator+( const CVector2f &other ) const
    {
        return CVector2f( x + other.x, y + other.y );
    }
    inline CVector2f operator-( const CVector2f &other ) const
    {
        return CVector2f( x - other.x, y - other.y );
    }
    inline CVector2f operator*( float factor ) const    //scale
    {
        return CVector2f( x * factor, y * factor );
    }
    inline CVector2f operator/( float factor ) const
    {
        return CVector2f( x / factor, y / factor );
    }
    //+=,-=,/=,*=
    inline CVector2f& operator+=( const CVector2f &other )
    {
        x += other.x;
        y += other.y;
        return *this;
    }
    inline CVector2f& operator-=( const CVector2f &other )
    {
        x -= other.x;
        y -= other.y;
        return *this;
    }
    inline CVector2f& operator*=( float factor )
    {
        x *= factor;
        y *= factor;
        return *this;
    }

    inline CVector2f& operator/=( float factor )
    {
        x /= factor;
        y /= factor;
        return *this;
    }
    //-,*
    inline CVector2f operator-( ) const
    {
        return CVector2f( -x, -y );
    }

    //maybe confusing?
    inline float operator*( const CVector2f &other ) const  //dot product
    {
        return ( x * other.x + y * other.y );
    }
    //==,!=
    inline bool operator==( const CVector2f &other ) const
    {
        return (x == other.x) && (y == other.y);
    }
    inline bool operator!=( const CVector2f &other ) const
    {
        return !(*this==other);
    }
    //other
    inline float crossProduct( const CVector2f &other ) const
    {
        return x * other.y - y * other.x;
    }


    //friends
    friend CVector2f operator*( float factor, const CVector2f &vector );
};

inline CVector2f operator*( float factor, const CVector2f &vector )
{
    return vector * factor;   
}


#endif

Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Jul 27, 2006 9:50 pm   Post subject: (No subject)

Any particular reason you rely on floats, rather than templating this?
Martin




PostPosted: Thu Jul 27, 2006 9:52 pm   Post subject: (No subject)

Hey, good call. Smile
wtd




PostPosted: Thu Jul 27, 2006 9:59 pm   Post subject: (No subject)

Considering that your class is internally representing itself as two values, you might also find it beneficial to implement it as privately inheriting std::pair.
bugzpodder




PostPosted: Sun Jul 30, 2006 1:48 pm   Post subject: (No subject)

consider use complex numbers (part of C++ standard library). most of these functions are defined
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: