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
25inline int rad2deg(double radians)
26{
27 return (180 / M_PI) * radians;
28}
29
30inline double deg2rad(int degrees)
31{
32 return (M_PI / 180) * degrees;
33}
34
35class 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
57qreal Vector::operator*(const Vector& rhs) const
58{
59 return x() * rhs.x() + y() * rhs.y();
60}
61
62qreal Vector::magnitude() const
63{
64 return sqrt(*this * *this);
65}
66
67qreal Vector::direction() const
68{
69 return atan2(y(), x());
70}
71
72void Vector::setMagnitude(qreal magnitude)
73{
74 setMagnitudeDirection(magnitude, this->direction());
75}
76
77void Vector::setDirection(qreal direction)
78{
79 setMagnitudeDirection(this->magnitude(), direction);
80}
81
82void Vector::setMagnitudeDirection(qreal magnitude, qreal direction)
83{
84 setX(magnitude * cos(direction));
85 setY(magnitude * sin(direction));
86}
87
88Vector Vector::fromMagnitudeDirection(qreal magnitude, qreal direction)
89{
90 Vector v;
91 v.setMagnitudeDirection(magnitude, direction);
92 return v;
93}
94
95Vector Vector::unitVector() const
96{
97 return *this / magnitude();
98}
99
100#endif // KOLF_VECTOR_H
101