/home/damien/work/2012_2013_teaching/2012_5eti_synthese/lib3d/v2.hpp
Go to the documentation of this file.
1 /*
2 ** TP 5ETI CPE Lyon
3 ** Copyright (C) 2012 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 
20 
21 #ifndef V2_HPP
22 #define V2_HPP
23 
24 #include <string>
25 #include <iostream>
26 
27 #include <exception_cpe.hpp>
28 
29 namespace cpe
30 {
31 
33 class v2
34 {
35 public:
36 
37  // ********************************************* //
38  // ********************************************* //
39  // CONSTRUCTORS
40  // ********************************************* //
41  // ********************************************* //
42 
44  v2();
46  v2(const double& x,const double& y);
47 
48  // ********************************************* //
49  // ********************************************* //
50  // Get/set
51  // ********************************************* //
52  // ********************************************* //
53 
55  const double& x() const;
57  double& x();
59  const double& y() const;
61  double& y();
62 
63 
65  const double& operator[](const size_t& k) const;
67  double& operator[](const size_t& k);
69  const double& operator()(const size_t& k) const;
71  double& operator()(const size_t& k);
72 
74  void set_zero();
75 
76 
77  // ********************************************* //
78  // ********************************************* //
79  // Math operation
80  // ********************************************* //
81  // ********************************************* //
82 
84  double dot(const v2& p) const;
85 
87  double norm() const;
89  double norm2() const;
91  v2 normalized() const;
92 
93 
94 
95 
96  // ********************************************* //
97  // ********************************************* //
98  // Operator +-*/
99  // ********************************************* //
100  // ********************************************* //
101 
103  v2 operator+(const v2& p2) const;
105  v2 operator+(const double& s) const;
106 
108  v2 operator-(const v2& p2) const;
110  v2 operator-(const double& s) const;
111 
112 
114  v2 operator*(const double& s) const;
116  v2 operator/(const double& s) const;
117 
119  v2& operator+=(const v2& p);
121  v2& operator+=(const double& s);
123  v2& operator-=(const v2& p);
125  v2& operator-=(const double& s);
127  v2& operator*=(const double& s);
129  v2& operator/=(const double& s);
130 
132  v2 operator-() const;
133 
137  v2 product_compontentwise(const v2& p) const;
144  void scale(const double& sx,const double& sy);
145 
146 
147  // ********************************************* //
148  // ********************************************* //
149  // Output
150  // ********************************************* //
151  // ********************************************* //
152 
154  std::string to_string() const;
155 
156 
157 
158 private:
159 
160 
161  // ********************************************* //
162  // ********************************************* //
163  // Internal parameters
164  // ********************************************* //
165  // ********************************************* //
166 
168  double internal_x;
170  double internal_y;
171 
172 
173  // ********************************************* //
174  // ********************************************* //
175  // Helper
176  // ********************************************* //
177  // ********************************************* //
178 
180  void assert_size(const size_t& k) const;
181 
182 };
183 
185 v2 operator+(const double& s,const v2& p);
187 v2 operator-(const double& s,const v2& p);
189 v2 operator*(const double& s,const v2& p);
190 
191 // ********************************************* //
192 // ********************************************* //
193 // Output
194 // ********************************************* //
195 // ********************************************* //
196 
198 std::ostream& operator<<(std::ostream& stream,const v2& p);
199 
200 
203 {
204 public:
205 
209  exception_v2(const std::string& msg,const std::string& file,const std::string& caller,const int& line):exception_cpe(msg,file,caller,line){}
210 };
211 
212 }
213 
214 
215 
216 #endif