Quark Physics  1.0
2D Rigid and Soft Body Physics Engine
qvector.h
1 
2 /************************************************************************************
3  * MIT License
4  *
5  * Copyright (c) 2023 Eray Zesen
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * https://github.com/erayzesen/QuarkPhysics
25  *
26 **************************************************************************************/
27 
28 #ifndef QVECTOR_H
29 #define QVECTOR_H
30 #include <cmath>
31 #include <ostream>
32 #include "qmath_utils.h"
33 
34 using namespace std;
35 
36 enum QSides{
37  UP,
38  RIGHT,
39  DOWN,
40  LEFT,
41  NONE
42 };
43 
44 struct QVector{
45 public:
46  QVector(){
47  QVector(0,0);
48  };
49  QVector(float px, float py){
50  x=px;
51  y=py;
52  };
53  float x=0;
54 
55  float y=0;
56 
57  static QVector Up(){
58  return QVector(0.0f,-1.0f);
59  };
60  static QVector Right(){
61  return QVector(1.0f,0.0f);
62  };
63  static QVector Down(){
64  return QVector(0.0f,1.0f);
65  };
66  static QVector Left(){
67  return QVector(-1.0f,0.0f);
68  };
69  static QVector Zero(){
70  return QVector(-0.0f,0.0f);
71  };
72 
73  static QVector NaN(){
74  return QVector(NAN,NAN);
75  };
76 
77 
78 
79 
80  //Math Operations
81  QVector &operator +=(const QVector &other) {
82  this->x+=other.x;
83  this->y+=other.y;
84  return *this;
85  };
86  friend QVector operator +(const QVector pointA, const QVector pointB){
87  return QVector(pointA.x+pointB.x,pointA.y+pointB.y);
88  }
89 
90  QVector &operator /=(const QVector &other) {
91  this->x/=other.x;
92  this->y/=other.y;
93  return *this;
94  };
95  QVector &operator /=(const float &value) {
96  this->x/=value;
97  this->y/=value;
98  return *this;
99  };
100  friend QVector operator /(const QVector pointA,const QVector pointB){
101  return QVector(pointA.x/pointB.x,pointA.y/pointB.y);
102  }
103  friend QVector operator /(const float value,const QVector point){
104  return QVector(point.x/value,point.y/value);
105  }
106  friend QVector operator /(const QVector point,const float value){
107  return QVector(point.x/value,point.y/value);
108  }
109  QVector &operator *=(const QVector &other){
110  this->x*=other.x;
111  this->y*=other.y;
112  return *this;
113  };
114 
115  QVector &operator *=(const float &value) {
116  this->x*=value;
117  this->y*=value;
118  return *this;
119  };
120  friend QVector operator *(const QVector point, const float value){
121  return QVector(point.x*value,point.y*value);
122  }
123  friend QVector operator *(const float value,const QVector point){
124  return QVector(point.x*value,point.y*value);
125  }
126  QVector &operator -=(const QVector &other) {
127  this->x-=other.x;
128  this->y-=other.y;
129  return *this;
130  };
131  QVector operator -() const {
132  return QVector(-x,-y);
133  };
134  friend QVector operator -(const QVector pointA, const QVector pointB){
135  return QVector(pointA.x-pointB.x,pointA.y-pointB.y);
136  }
137 
138  bool operator ==(const QVector &other) const {
139  return x==other.x && y==other.y;
140  };
141  bool operator !=(const QVector &other) const {
142  return !(x==other.x && y==other.y);
143  };
144 
145 
146 
147  friend ostream& operator<<(ostream& os,QVector const & point){
148  return os<<"QVector("<< point.x<<","<<point.y<<")";
149  }
150 
151  //Methods
152 
153  QVector Rotated(float radianAngle) const;
154  static QVector AngleToUnitVector(const float radianAngle);
155  static float AngleBetweenTwoVectors(QVector vector,QVector referenceVector);
156  static QSides GetVectorSide(QVector vector,QVector referenceUpVector,float maxAngleDefiningSide=0.785398f);
157  static QVector GeteBisectorUnitVector(QVector pointA,QVector pointB,QVector pointC,bool checkPointsAreCCW=false);
158 
159  //Operations
160 
161  inline float Dot(QVector with) const{
162  return (x*with.x)+(y*with.y);
163  }
164  inline float Length() const {
165  return sqrt(x*x+y*y);
166  }
167  inline QVector Normalized() const {
168  if(x==0 && y==0)
169  return QVector::Zero();
170  float lsqrt=LengthSquared();
171  if(lsqrt==0)
172  return QVector::Zero();
173  float l=sqrt(lsqrt);
174  return QVector(x/l,y/l);
175  }
176  inline QVector Perpendicular() const {
177  return QVector(y,-x);
178  }
179  inline float LengthSquared()const {
180  return (x*x)+(y*y);
181  }
182 
183  inline bool isNaN() const{
184  if(isnan(x) && isnan(y) ){
185  return true;
186  }
187  return false;
188  }
189 
190 
191 
192 
193 };
194 
195 #endif // QVECTOR_H
Definition: qvector.h:44