Debian Codegraph @

thirdparty/msdfgen/core/Vector2.hpp

raw symbols SCIP
1
2 #pragma once
3
4 #include <cmath>
5 #include "base.h"
6
7 namespace msdfgen {
8
9 /**
10 * A 2-dimensional euclidean floating-point vector.
11 * @author Viktor Chlumsky
12 */
13 struct Vector2 {
14
15 double x, y;
16
17 inline Vector2(double val = 0) : x(val), y(val) { }
18
19 inline Vector2(double x, double y) : x(x), y(y) { }
20
21 /// Sets the vector to zero.
22 inline void reset() {
23 x = 0, y = 0;
24 }
25
26 /// Sets individual elements of the vector.
27 inline void set(double newX, double newY) {
28 x = newX, y = newY;
29 }
30
31 /// Returns the vector's squared length.
32 inline double squaredLength() const {
33 return x*x+y*y;
34 }
35
36 /// Returns the vector's length.
37 inline double length() const {
38 return sqrt(x*x+y*y);
39 }
40
41 /// Returns the normalized vector - one that has the same direction but unit length.
42 inline Vector2 normalize(bool allowZero = false) const {
43 if (double len = length())
44 return Vector2(x/len, y/len);
45 return Vector2(0, !allowZero);
46 }
47
48 /// Returns a vector with the same length that is orthogonal to this one.
49 inline Vector2 getOrthogonal(bool polarity = true) const {
50 return polarity ? Vector2(-y, x) : Vector2(y, -x);
51 }
52
53 /// Returns a vector with unit length that is orthogonal to this one.
54 inline Vector2 getOrthonormal(bool polarity = true, bool allowZero = false) const {
55 if (double len = length())
56 return polarity ? Vector2(-y/len, x/len) : Vector2(y/len, -x/len);
57 return polarity ? Vector2(0, !allowZero) : Vector2(0, -!allowZero);
58 }
59
60 #ifdef MSDFGEN_USE_CPP11
61 inline explicit operator bool() const {
62 return x || y;
63 }
64 #else
65 inline operator const void *() const {
66 return x || y ? this : NULL;
67 }
68 #endif
69
70 inline Vector2 &operator+=(const Vector2 other) {
71 x += other.x, y += other.y;
72 return *this;
73 }
74
75 inline Vector2 &operator-=(const Vector2 other) {
76 x -= other.x, y -= other.y;
77 return *this;
78 }
79
80 inline Vector2 &operator*=(const Vector2 other) {
81 x *= other.x, y *= other.y;
82 return *this;
83 }
84
85 inline Vector2 &operator/=(const Vector2 other) {
86 x /= other.x, y /= other.y;
87 return *this;
88 }
89
90 inline Vector2 &operator*=(double value) {
91 x *= value, y *= value;
92 return *this;
93 }
94
95 inline Vector2 &operator/=(double value) {
96 x /= value, y /= value;
97 return *this;
98 }
99
100 };
101
102 /// A vector may also represent a point, which shall be differentiated semantically using the alias Point2.
103 typedef Vector2 Point2;
104
105 /// Dot product of two vectors.
106 inline double dotProduct(const Vector2 a, const Vector2 b) {
107 return a.x*b.x+a.y*b.y;
108 }
109
110 /// A special version of the cross product for 2D vectors (returns scalar value).
111 inline double crossProduct(const Vector2 a, const Vector2 b) {
112 return a.x*b.y-a.y*b.x;
113 }
114
115 inline bool operator==(const Vector2 a, const Vector2 b) {
116 return a.x == b.x && a.y == b.y;
117 }
118
119 inline bool operator!=(const Vector2 a, const Vector2 b) {
120 return a.x != b.x || a.y != b.y;
121 }
122
123 inline Vector2 operator+(const Vector2 v) {
124 return v;
125 }
126
127 inline Vector2 operator-(const Vector2 v) {
128 return Vector2(-v.x, -v.y);
129 }
130
131 inline bool operator!(const Vector2 v) {
132 return !v.x && !v.y;
133 }
134
135 inline Vector2 operator+(const Vector2 a, const Vector2 b) {
136 return Vector2(a.x+b.x, a.y+b.y);
137 }
138
139 inline Vector2 operator-(const Vector2 a, const Vector2 b) {
140 return Vector2(a.x-b.x, a.y-b.y);
141 }
142
143 inline Vector2 operator*(const Vector2 a, const Vector2 b) {
144 return Vector2(a.x*b.x, a.y*b.y);
145 }
146
147 inline Vector2 operator/(const Vector2 a, const Vector2 b) {
148 return Vector2(a.x/b.x, a.y/b.y);
149 }
150
151 inline Vector2 operator*(double a, const Vector2 b) {
152 return Vector2(a*b.x, a*b.y);
153 }
154
155 inline Vector2 operator/(double a, const Vector2 b) {
156 return Vector2(a/b.x, a/b.y);
157 }
158
159 inline Vector2 operator*(const Vector2 a, double b) {
160 return Vector2(a.x*b, a.y*b);
161 }
162
163 inline Vector2 operator/(const Vector2 a, double b) {
164 return Vector2(a.x/b, a.y/b);
165 }
166
167 }