vec2.hpp
Go to the documentation of this file.
1 /*
2 ** TP CPE Lyon
3 ** Copyright (C) 2014 Damien Rohmer
4 **
5 ** This program is free software: you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation, either version 3 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 
21 
22 #ifndef VEC2_HPP
23 #define VEC2_HPP
24 
25 #include <string>
26 #include <iostream>
27 
28 
29 
30 namespace cpe
31 {
32 
34 class vec2
35 {
36 public:
37 
38  // ********************************************* //
39  // ********************************************* //
40  // CONSTRUCTORS
41  // ********************************************* //
42  // ********************************************* //
43 
45  vec2();
47  vec2(float x,float y);
48 
49  // ********************************************* //
50  // ********************************************* //
51  // Get/set
52  // ********************************************* //
53  // ********************************************* //
54 
56  float x() const;
58  float& x();
60  float y() const;
62  float& y();
63 
64 
66  float operator[](const size_t& k) const;
68  float& operator[](const size_t& k);
70  float operator()(const size_t& k) const;
72  float& operator()(const size_t& k);
73 
75  void set_zero();
76 
77 
80  const float *pointer() const;
81 
82  // ********************************************* //
83  // ********************************************* //
84  // Math operation
85  // ********************************************* //
86  // ********************************************* //
87 
89  float dot(const vec2& p) const;
90 
92  float norm() const;
94  float norm2() const;
96  vec2 normalized() const;
97 
98 
99 
100 
101  // ********************************************* //
102  // ********************************************* //
103  // Operator +-*/
104  // ********************************************* //
105  // ********************************************* //
106 
108  vec2 operator+(const vec2& p2) const;
110  vec2 operator+(float s) const;
111 
113  vec2 operator-(const vec2& p2) const;
115  vec2 operator-(float s) const;
116 
117 
119  vec2 operator*(float s) const;
121  vec2 operator/(float s) const;
122 
124  vec2& operator+=(const vec2& p);
126  vec2& operator+=(float s);
128  vec2& operator-=(const vec2& p);
130  vec2& operator-=(float s);
132  vec2& operator*=(float s);
134  vec2& operator/=(float s);
135 
137  vec2 operator-() const;
138 
142  vec2 product_compontentwise(const vec2& p) const;
149  void scale(float sx,float sy);
150 
151 
152  // ********************************************* //
153  // ********************************************* //
154  // Output
155  // ********************************************* //
156  // ********************************************* //
157 
159  std::string to_string() const;
160 
161 
162 
163 private:
164 
165 
166  // ********************************************* //
167  // ********************************************* //
168  // Internal parameters
169  // ********************************************* //
170  // ********************************************* //
171 
173  float internal_x;
175  float internal_y;
176 
177 
178  // ********************************************* //
179  // ********************************************* //
180  // Helper
181  // ********************************************* //
182  // ********************************************* //
183 
185  void assert_size(const size_t& k) const;
186 
187 };
188 
190 vec2 operator+(float s,const vec2& p);
192 vec2 operator-(float s,const vec2& p);
194 vec2 operator*(float s,const vec2& p);
195 
196 // ********************************************* //
197 // ********************************************* //
198 // Output
199 // ********************************************* //
200 // ********************************************* //
201 
203 std::ostream& operator<<(std::ostream& stream,const vec2& p);
204 
205 
206 }
207 
208 
209 
210 #endif
vec2 normalized() const
normalize the vector to unit length
Definition: vec2.cpp:245
vec2 operator-() const
unary negation
Definition: vec2.cpp:208
void assert_size(const size_t &k) const
assert that a size_t belongs to [[0,1]]
Definition: vec2.cpp:236
vec2 operator/(float s) const
divide by a scalar operator
Definition: vec2.cpp:157
vec2 product_compontentwise(const vec2 &p) const
does componentwise mutliplication
Definition: vec2.cpp:213
float y() const
get y coordinate
Definition: vec2.cpp:44
void scale(float sx, float sy)
internal scaling (similar to componentwise)
Definition: vec2.cpp:255
void set_zero()
set every entry to 0
Definition: vec2.cpp:94
float internal_y
y coordinate
Definition: vec2.hpp:175
vec2 & operator-=(const vec2 &p)
internal -
Definition: vec2.cpp:180
vec2 operator*(float s) const
multiply by a scalar operator
Definition: vec2.cpp:152
float internal_x
x coordinate
Definition: vec2.hpp:173
matrix1x4 operator-(float s, const matrix1x4 &m)
operator
Definition: matrix1x4.cpp:82
matrix1x4 operator+(float s, const matrix1x4 &m)
operator
Definition: matrix1x4.cpp:61
float x() const
get x coordinate
Definition: vec2.cpp:34
float operator[](const size_t &k) const
Access to the k_th entry (k in [0,1])
Definition: vec2.cpp:55
float norm2() const
get the square norm of the vector
Definition: vec2.cpp:110
vec2 & product_compontentwise_internal(const vec2 &p)
does componentwise mutliplication
Definition: vec2.cpp:218
float operator()(const size_t &k) const
Access to the k_th entry (k in [0,1])
Definition: vec2.cpp:85
matrix1x4 operator*(float s, const matrix1x4 &m)
multiply by a scalar operator
Definition: matrix1x4.cpp:93
vec2 & operator+=(const vec2 &p)
internal +
Definition: vec2.cpp:166
std::ostream & operator<<(std::ostream &stream, const matrix1x4 &_m)
output the vector in ostream
Definition: matrix1x4.cpp:182
const float * pointer() const
fast pointer access
Definition: vec2.cpp:261
Vectors/Points 2D.
Definition: vec2.hpp:34
vec2 & operator*=(float s)
internal *
Definition: vec2.cpp:194
float dot(const vec2 &p) const
perform dot product between two v2
Definition: vec2.cpp:100
vec2 & operator/=(float s)
internal /
Definition: vec2.cpp:201
float norm() const
get the norm of the vector
Definition: vec2.cpp:105
std::string to_string() const
export the value as string cout<<v2(2,3,6) => 2 3 6
Definition: vec2.cpp:224
vec2()
empty constructor
Definition: vec2.cpp:32
vec2 operator+(const vec2 &p2) const
operator
Definition: vec2.cpp:115