1 | /* |
2 | Copyright 2010 Stefan Majewsky <majewsky@gmx.net> |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | |
9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
18 | |
19 | #ifndef KOLF_VECTOR_H |
20 | #define KOLF_VECTOR_H |
21 | |
22 | #include <QPointF> |
23 | #include <cmath> |
24 | |
25 | inline int rad2deg(double radians) |
26 | { |
27 | return (180 / M_PI) * radians; |
28 | } |
29 | |
30 | inline double deg2rad(int degrees) |
31 | { |
32 | return (M_PI / 180) * degrees; |
33 | } |
34 | |
35 | class Vector : public QPointF |
36 | { |
37 | public: |
38 | inline Vector(const QPointF& point = QPointF()) : QPointF(point) {} |
39 | inline Vector(qreal x, qreal y) : QPointF(x, y) {} |
40 | inline Vector& operator=(const QPointF& point) { setX(point.x()); setY(point.y()); return *this; } |
41 | |
42 | //dot product |
43 | inline qreal operator*(const Vector& rhs) const; |
44 | |
45 | //getters and setters for polar coordinates |
46 | inline qreal magnitude() const; |
47 | inline qreal direction() const; //in radians! |
48 | inline void setMagnitude(qreal magnitude); |
49 | inline void setDirection(qreal direction); |
50 | inline void setMagnitudeDirection(qreal magnitude, qreal direction); |
51 | static inline Vector fromMagnitudeDirection(qreal magnitude, qreal direction); |
52 | |
53 | //some further convenience |
54 | inline Vector unitVector() const; |
55 | }; |
56 | |
57 | qreal Vector::operator*(const Vector& rhs) const |
58 | { |
59 | return x() * rhs.x() + y() * rhs.y(); |
60 | } |
61 | |
62 | qreal Vector::magnitude() const |
63 | { |
64 | return sqrt(*this * *this); |
65 | } |
66 | |
67 | qreal Vector::direction() const |
68 | { |
69 | return atan2(y(), x()); |
70 | } |
71 | |
72 | void Vector::setMagnitude(qreal magnitude) |
73 | { |
74 | setMagnitudeDirection(magnitude, this->direction()); |
75 | } |
76 | |
77 | void Vector::setDirection(qreal direction) |
78 | { |
79 | setMagnitudeDirection(this->magnitude(), direction); |
80 | } |
81 | |
82 | void Vector::setMagnitudeDirection(qreal magnitude, qreal direction) |
83 | { |
84 | setX(magnitude * cos(direction)); |
85 | setY(magnitude * sin(direction)); |
86 | } |
87 | |
88 | Vector Vector::fromMagnitudeDirection(qreal magnitude, qreal direction) |
89 | { |
90 | Vector v; |
91 | v.setMagnitudeDirection(magnitude, direction); |
92 | return v; |
93 | } |
94 | |
95 | Vector Vector::unitVector() const |
96 | { |
97 | return *this / magnitude(); |
98 | } |
99 | |
100 | #endif // KOLF_VECTOR_H |
101 | |