src/proto2/Vector.hh
author saiam
Sat, 29 Nov 2008 17:29:53 +0000
changeset 133 c05e84ccc4b3
parent 130 81406f8b7535
child 158 0215ace86018
permissions -rw-r--r--
Made unnecessarily virtual function PhysicsObject::applyForce nonvirtual
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
     1
#ifndef VECTOR_HH
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
     2
#define VECTOR_HH
46
saiam
parents:
diff changeset
     3
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
     4
#include <iostream>
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 66
diff changeset
     5
#include <cmath>
51
360208b631c1 Resolved conflicts.
saiam
parents: 50
diff changeset
     6
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
     7
/**
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
     8
 * A 2D Vector class. Implements standard vector operations.
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
     9
 */
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    10
template <typename T>
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    11
class _Vector {
51
360208b631c1 Resolved conflicts.
saiam
parents: 50
diff changeset
    12
public:
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    13
    T x;
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    14
    T y;
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    15
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    16
    /**
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    17
     * Default constructor.
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    18
     */
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    19
    _Vector() : x(0), y(0) {}
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    20
    
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    21
    /**
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    22
     * Constuctor.
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    23
     *
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    24
     * @param x Initial x-coordinate
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    25
     * @param y Initial y-coordinate
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    26
     */
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    27
    _Vector(T x, T y) : x(x), y(y) {}
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    28
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    29
    /**
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    30
     * Copy constructor.
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    31
     *
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    32
     * @param v Vector to be copied.
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    33
     */
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    34
    _Vector(const _Vector &v) : x(v.x), y(v.y) {}
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    35
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    36
    // Operator declarations
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    37
    void operator=(const _Vector &v) {
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    38
        this->x = v.x;
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    39
        this->y = v.y;
53
a76ddb2e39fb changed Vector.hh to work as coor.hh
nireco
parents: 51
diff changeset
    40
    }
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    41
    _Vector operator+(const _Vector &v) const {
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    42
        return _Vector(this->x+v.x, this->y+v.y);
53
a76ddb2e39fb changed Vector.hh to work as coor.hh
nireco
parents: 51
diff changeset
    43
    }
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    44
    _Vector operator-(const _Vector &v) const {
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    45
        return _Vector(this->x-v.x, this->y-v.y);
53
a76ddb2e39fb changed Vector.hh to work as coor.hh
nireco
parents: 51
diff changeset
    46
    }
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    47
    _Vector operator*(const T &scalar) const {
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    48
        return _Vector(this->x*scalar, this->y*scalar);
51
360208b631c1 Resolved conflicts.
saiam
parents: 50
diff changeset
    49
    }
99
c9b96dcfe4ee Mui. Sienet.
saiam
parents: 77
diff changeset
    50
    T operator*(const _Vector &v) const {
c9b96dcfe4ee Mui. Sienet.
saiam
parents: 77
diff changeset
    51
        return (this->x*v.x + this->y*v.y);
c9b96dcfe4ee Mui. Sienet.
saiam
parents: 77
diff changeset
    52
    }
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    53
    _Vector operator/(const T &d) const {
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    54
        return _Vector(this->x/d, this->y/d);
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    55
    }
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    56
    void operator+=(const _Vector &v) {
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    57
        *this = *this + v;
51
360208b631c1 Resolved conflicts.
saiam
parents: 50
diff changeset
    58
    }
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    59
    void operator-=(const _Vector &v) {
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    60
        *this = *this - v;
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    61
    }
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    62
    void operator*=(const T &scalar) {
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    63
        *this = *this * scalar;
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    64
    }
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    65
    void operator/=(const T &scalar) {
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    66
        *this = *this / scalar;
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    67
    }
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    68
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    69
    // Other operations
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 66
diff changeset
    70
    T length() const {
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    71
        return sqrt((this->x * this->x) + (this->y * this->y));
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 66
diff changeset
    72
    }
124
2fd698e04779 fixed collision detection again, and put some needed rounding to getNormal
nireco
parents: 99
diff changeset
    73
    _Vector roundToInt() const {
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    74
        return _Vector(round(x), round(y));
124
2fd698e04779 fixed collision detection again, and put some needed rounding to getNormal
nireco
parents: 99
diff changeset
    75
    }
51
360208b631c1 Resolved conflicts.
saiam
parents: 50
diff changeset
    76
};
46
saiam
parents:
diff changeset
    77
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    78
// Unary operators
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    79
template<typename T>
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    80
_Vector<T> operator*(const T &scalar, const _Vector<T> v) {
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    81
    return (v * scalar);
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    82
} 
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    83
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    84
// Comparison operators
53
a76ddb2e39fb changed Vector.hh to work as coor.hh
nireco
parents: 51
diff changeset
    85
template<typename T>
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    86
bool operator==(const _Vector<T> &v1, const _Vector<T> &v2) {
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    87
    return ((v1.x == v2.x) && (v1.y == v2.y));
53
a76ddb2e39fb changed Vector.hh to work as coor.hh
nireco
parents: 51
diff changeset
    88
}
57
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    89
template<typename T>
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    90
bool operator!=(const _Vector<T> &v1, const _Vector<T> &v2) {
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    91
    return !(v1 == v2);
a89e02118931 Added some operators.
saiam
parents: 55
diff changeset
    92
}
53
a76ddb2e39fb changed Vector.hh to work as coor.hh
nireco
parents: 51
diff changeset
    93
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
    94
// Output operator
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    95
template<typename T>
66
1415a2d45686 working simple network-physics code
terom
parents: 60
diff changeset
    96
std::ostream& operator<<(std::ostream &s, const _Vector<T> &v) {
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    97
    return s<<"("<<v.x<<", "<<v.y<<")";
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    98
}
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
    99
130
81406f8b7535 New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents: 124
diff changeset
   100
// Standard vector
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
   101
typedef _Vector<float> Vector;
55
8ae9dd0ae337 It compiles!!!!
saiam
parents: 54
diff changeset
   102
46
saiam
parents:
diff changeset
   103
#endif