MinGfx Toolkit  1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
vector2.h
Go to the documentation of this file.
1 /*
2  This file is part of the MinGfx Project.
3 
4  Copyright (c) 2017,2018 Regents of the University of Minnesota.
5  All Rights Reserved.
6 
7  Original Author(s) of this File:
8  Dan Keefe, 2018, University of Minnesota
9 
10  Author(s) of Significant Updates/Modifications to the File:
11  ...
12  */
13 
14 #ifndef SRC_VECTOR2_H_
15 #define SRC_VECTOR2_H_
16 
17 #include <iostream>
18 
19 #include "point2.h"
20 
21 
22 namespace mingfx {
23 
24 
28 class Vector2 {
29 public:
30 
33 
36  Vector2(float x, float y);
37 
39  Vector2(float *v);
40 
42  Vector2(const Vector2& v);
43 
45  virtual ~Vector2();
46 
48  bool operator==(const Vector2& v) const;
49 
51  bool operator!=(const Vector2& v) const;
52 
55 
57  float operator[](const int i) const;
58 
66  float& operator[](const int i);
67 
70  float x() const { return v[0]; }
71 
74  float y() const { return v[1]; }
75 
77  float w() const { return 0.0; }
78 
79 
80  // --- Vector operations ---
81 
83  float Dot(const Vector2& v) const;
84 
86  float Length() const;
87 
89  void Normalize();
90 
93  Vector2 ToUnit() const;
94 
98  Vector2 Lerp(const Vector2 &b, float alpha) const;
99 
101  const float * value_ptr() const;
102 
103 
104 
106  static Vector2 Normalize(const Vector2 &v);
107 
109  static float Dot(const Vector2 &v1, const Vector2 &v2);
110 
112  static const Vector2& Zero();
113 
115  static const Vector2& One();
116 
118  static const Vector2& UnitX();
119 
121  static const Vector2& UnitY();
122 
125  static Vector2 Lerp(const Vector2 &a, const Vector2 &b, float alpha);
126 
127 private:
128  float v[2];
129 };
130 
131 
132 // ---------- Operator Overloads for Working with Vectors ----------
133 
134 
135 // --- Scalers ---
136 
138 Vector2 operator/(const Vector2& v, const float s);
139 
141 Vector2 operator*(const float s, const Vector2& v);
142 
144 Vector2 operator*(const Vector2& v, const float s);
145 
148 
149 // Note: no -(point) operator, that's an undefined operation
150 
151 
152 // --- Point and Vector Arithmetic ---
153 
155 Point2 operator+(const Vector2& v, const Point2& p);
156 
158 Point2 operator+(const Point2& p, const Vector2& v);
159 
161 Vector2 operator+(const Vector2& v1, const Vector2& v2);
162 
163 // Note: no (point + point) operator, that's an undefined operation
164 
166 Point2 operator-(const Point2& p, const Vector2& v);
167 
169 Vector2 operator-(const Vector2& v1, const Vector2& v2);
170 
172 Vector2 operator-(const Point2& p1, const Point2& p2);
173 
174 // Note: no (vector - point) operator, that's an undefined operation
175 
176 
177 // --- Stream operators ---
178 
179 // Vector2
180 std::ostream & operator<< ( std::ostream &os, const Vector2 &v);
181 std::istream & operator>> ( std::istream &is, Vector2 &v);
182 
183 
184 } // end namespace
185 
186 #endif
A 2D Point with floating point coordinates, used for storing 2D texture coordinates,...
Definition: point2.h:28
A 2D Vector with floating point coordinates, used for storing 2D translations, mouse movements,...
Definition: vector2.h:28
static Vector2 Normalize(const Vector2 &v)
Returns a new vector that is the unit version of v.
bool operator!=(const Vector2 &v) const
Check for "inequality", taking floating point imprecision into account.
float w() const
In homogeneous coordinates, the w coordinate for all vectors is 0.0.
Definition: vector2.h:77
Vector2 & operator=(const Vector2 &v)
Vector assignment operator.
Vector2(const Vector2 &v)
Copy constructor for vector.
Vector2 Lerp(const Vector2 &b, float alpha) const
Linear interpolation between this vector and another. Alpha=0.0 returns this vector,...
static const Vector2 & One()
(1,1) - a shortcut for a special vector that is frequently needed
void Normalize()
Normalizes the vector by making it unit length.
Vector2(float *v)
Constructs a vector given a pointer to x,y,z data.
Vector2 ToUnit() const
Returns a normalized (i.e., unit length) version of the vector without modifying the original ('this'...
float Length() const
Returns the length of the vector.
static const Vector2 & Zero()
(0,0) - a shortcut for a special vector that is frequently needed
const float * value_ptr() const
Returns a const pointer to the raw data array.
static const Vector2 & UnitX()
(1,0) - a shortcut for a special vector that is frequently needed
float & operator[](const int i)
Returns a reference to the ith coordinate of the vector. Use this accessor if you wish to set the coo...
Vector2()
Default constructor to create zero vector.
static float Dot(const Vector2 &v1, const Vector2 &v2)
Returns v1 dot v2.
static const Vector2 & UnitY()
(0,1) - a shortcut for a special vector that is frequently needed
static Vector2 Lerp(const Vector2 &a, const Vector2 &b, float alpha)
Linear interpolation between two vectors. Alpha=0.0 returns 'a' and alpha=1.0 returns 'b',...
float y() const
Read only access to the y coordinate. Can also use my_vector[1]. Use the my_vector[1] = 1....
Definition: vector2.h:74
virtual ~Vector2()
Vector destructor.
Vector2(float x, float y)
Constructs a vector (x,y,0), where the 0 comes from the use of homogeneous coordinates in computer gr...
bool operator==(const Vector2 &v) const
Check for "equality", taking floating point imprecision into account.
float operator[](const int i) const
Read only access to the ith coordinate of the vector.
float Dot(const Vector2 &v) const
Returns "this dot v".
float x() const
Read only access to the x coordinate. Can also use my_vector[0]. Use the my_vector[0] = 1....
Definition: vector2.h:70
Namespace for the MinGfx Toolkit.
Definition: aabb.h:21
Quaternion operator-(const Quaternion &q)
AABB operator+(const AABB &A, const AABB &B)
Quaternion operator/(const Quaternion &q, const float s)
std::ostream & operator<<(std::ostream &os, const Color &c)
std::istream & operator>>(std::istream &is, Color &c)
Matrix4 operator*(const Matrix4 &m, const float &s)
Multiply matrix and scalar, returns the new matrix.