File indexing completed on 2025-02-02 07:28:26
0001 /* 0002 Copyright 2010 Stefan Majewsky <majewsky@gmx.net> 0003 0004 This program is free software; you can redistribute it and/or modify 0005 it under the terms of the GNU General Public License as published by 0006 the Free Software Foundation; either version 2 of the License, or 0007 (at your option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0012 GNU General Public License for more details. 0013 0014 You should have received a copy of the GNU General Public License 0015 along with this program; if not, write to the Free Software 0016 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 0017 */ 0018 0019 #ifndef KOLF_VECTOR_H 0020 #define KOLF_VECTOR_H 0021 0022 #include <QPointF> 0023 #include <cmath> 0024 0025 inline int rad2deg(double radians) 0026 { 0027 return (180 / M_PI) * radians; 0028 } 0029 0030 inline double deg2rad(int degrees) 0031 { 0032 return (M_PI / 180) * degrees; 0033 } 0034 0035 class Vector : public QPointF 0036 { 0037 public: 0038 inline Vector(const QPointF& point = QPointF()) : QPointF(point) {} 0039 inline Vector(qreal x, qreal y) : QPointF(x, y) {} 0040 inline Vector& operator=(const QPointF& point) { setX(point.x()); setY(point.y()); return *this; } 0041 0042 //dot product 0043 inline qreal operator*(const Vector& rhs) const; 0044 0045 //getters and setters for polar coordinates 0046 inline qreal magnitude() const; 0047 inline qreal direction() const; //in radians! 0048 inline void setMagnitude(qreal magnitude); 0049 inline void setDirection(qreal direction); 0050 inline void setMagnitudeDirection(qreal magnitude, qreal direction); 0051 static inline Vector fromMagnitudeDirection(qreal magnitude, qreal direction); 0052 0053 //some further convenience 0054 inline Vector unitVector() const; 0055 }; 0056 0057 qreal Vector::operator*(const Vector& rhs) const 0058 { 0059 return x() * rhs.x() + y() * rhs.y(); 0060 } 0061 0062 qreal Vector::magnitude() const 0063 { 0064 return sqrt(*this * *this); 0065 } 0066 0067 qreal Vector::direction() const 0068 { 0069 return atan2(y(), x()); 0070 } 0071 0072 void Vector::setMagnitude(qreal magnitude) 0073 { 0074 setMagnitudeDirection(magnitude, this->direction()); 0075 } 0076 0077 void Vector::setDirection(qreal direction) 0078 { 0079 setMagnitudeDirection(this->magnitude(), direction); 0080 } 0081 0082 void Vector::setMagnitudeDirection(qreal magnitude, qreal direction) 0083 { 0084 setX(magnitude * cos(direction)); 0085 setY(magnitude * sin(direction)); 0086 } 0087 0088 Vector Vector::fromMagnitudeDirection(qreal magnitude, qreal direction) 0089 { 0090 Vector v; 0091 v.setMagnitudeDirection(magnitude, direction); 0092 return v; 0093 } 0094 0095 Vector Vector::unitVector() const 0096 { 0097 return *this / magnitude(); 0098 } 0099 0100 #endif // KOLF_VECTOR_H