aboutsummaryrefslogtreecommitdiffstats
path: root/dev/MinGfx/src/vector2.h
blob: 0988ff0e972c31ef13d7f7f7a80200da269e6890 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 This file is part of the MinGfx Project.
 
 Copyright (c) 2017,2018 Regents of the University of Minnesota.
 All Rights Reserved.
 
 Original Author(s) of this File:
	Dan Keefe, 2018, University of Minnesota
	
 Author(s) of Significant Updates/Modifications to the File:
	...
 */

#ifndef SRC_VECTOR2_H_
#define SRC_VECTOR2_H_

#include <iostream>

#include "point2.h"


namespace mingfx {


/** A 2D Vector with floating point coordinates, used for storing 2D translations,
 mouse movements, and screen-space vectors.
 */
class Vector2 {
public:
    
    /// Default constructor to create zero vector
    Vector2();
    
    /// Constructs a vector (x,y,0), where the 0 comes from the use of
    /// homogeneous coordinates in computer graphics.
    Vector2(float x, float y);
    
    /// Constructs a vector given a pointer to x,y,z data
    Vector2(float *v);
    
    /// Copy constructor for vector
    Vector2(const Vector2& v);
    
    /// Vector destructor
    virtual ~Vector2();
    
    /// Check for "equality", taking floating point imprecision into account
    bool operator==(const Vector2& v) const;
    
    /// Check for "inequality", taking floating point imprecision into account
    bool operator!=(const Vector2& v) const;
    
    /// Vector assignment operator
    Vector2& operator=(const Vector2& v);
    
    /// Read only access to the ith coordinate of the vector.
    float operator[](const int i) const;
    
    /// Returns a reference to the ith coordinate of the vector.  Use this
    /// accessor if you wish to set the coordinate rather than just request
    /// its value.  Example:
    /// ~~~
    /// Vector2 a;
    /// a[0] = 5.0; // set the x-coordinate of the vector
    /// ~~~
    float& operator[](const int i);
    
    /// Read only access to the x coordinate.  Can also use my_vector[0].  Use
    /// the my_vector[0] = 1.0; form if you need to set the value.
    float x() const { return v[0]; }
    
    /// Read only access to the y coordinate.  Can also use my_vector[1].  Use
    /// the my_vector[1] = 1.0; form if you need to set the value.
    float y() const { return v[1]; }
        
    /// In homogeneous coordinates, the w coordinate for all vectors is 0.0.
    float w() const { return 0.0; }
    
    
    // --- Vector operations ---
    
    /// Returns "this dot v"
    float Dot(const Vector2& v) const;
    
    /// Returns the length of the vector
    float Length() const;
    
    /// Normalizes the vector by making it unit length.
    void Normalize();
    
    /// Returns a normalized (i.e., unit length) version of the vector without
    /// modifying the original ('this') vector.
    Vector2 ToUnit() const;
    
    /// Linear interpolation between this vector and another. Alpha=0.0 returns
    /// this vector, and alpha=1.0 returns the other vector, other values blend
    /// between the two.
    Vector2 Lerp(const Vector2 &b, float alpha) const;
    
    /// Returns a const pointer to the raw data array
    const float * value_ptr() const;

    
    
    /// Returns a new vector that is the unit version of v.
    static Vector2 Normalize(const Vector2 &v);
    
    /// Returns v1 dot v2
    static float Dot(const Vector2 &v1, const Vector2 &v2);
    
    /// (0,0) - a shortcut for a special vector that is frequently needed
    static const Vector2& Zero();

    /// (1,1) - a shortcut for a special vector that is frequently needed
    static const Vector2& One();

    /// (1,0) - a shortcut for a special vector that is frequently needed
    static const Vector2& UnitX();

    /// (0,1) - a shortcut for a special vector that is frequently needed
    static const Vector2& UnitY();
    
    /// Linear interpolation between two vectors.  Alpha=0.0 returns 'a' and
    /// alpha=1.0 returns 'b', other values blend between the two.
    static Vector2 Lerp(const Vector2 &a, const Vector2 &b, float alpha);

private:
    float v[2];
};


// ---------- Operator Overloads for Working with Vectors ----------


// --- Scalers ---

/// Divide the vector by the scalar s
Vector2 operator/(const Vector2& v, const float s);

/// Multiply the vector by the scalar s
Vector2 operator*(const float s, const Vector2& v);

/// Multiply the vector by the scalar s
Vector2 operator*(const Vector2& v, const float s);

/// Negate the vector
Vector2 operator-(const Vector2& v);

// Note: no -(point) operator, that's an undefined operation


// --- Point and Vector Arithmetic ---

/// Adds a vector and a point, returns a point
Point2 operator+(const Vector2& v, const Point2& p);

/// Adds a point and a vector, returns a point
Point2 operator+(const Point2& p, const Vector2& v);

/// Adds a vector and a vector, returns a vector
Vector2 operator+(const Vector2& v1, const Vector2& v2);

// Note: no (point + point) operator, that's an undefined operation

/// Subtracts a vector from a point, returns a point
Point2 operator-(const Point2& p, const Vector2& v);

/// Subtracts v2 from v1, returns a vector
Vector2 operator-(const Vector2& v1, const Vector2& v2);

/// Returns the vector spanning p1 and p2
Vector2 operator-(const Point2& p1, const Point2& p2);

// Note: no (vector - point) operator, that's an undefined operation


// --- Stream operators ---

// Vector2
std::ostream & operator<< ( std::ostream &os, const Vector2 &v);
std::istream & operator>> ( std::istream &is, Vector2 &v);

    
} // end namespace

#endif