1
2#include "Vector2.h"
3
4namespace msdfgen {
5
6Vector2::Vector2(double val) : x(val), y(val) { }
7
8Vector2::Vector2(double x, double y) : x(x), y(y) { }
9
10void Vector2::reset() {
11 x = 0, y = 0;
12}
13
14void Vector2::set(double x, double y) {
15 Vector2::x = x, Vector2::y = y;
16}
17
18double Vector2::length() const {
19 return sqrt(x*x+y*y);
20}
21
22double Vector2::direction() const {
23 return atan2(y, x);
24}
25
26Vector2 Vector2::normalize(bool allowZero) const {
27 double len = length();
28 if (len == 0)
29 return Vector2(0, !allowZero);
30 return Vector2(x/len, y/len);
31}
32
33Vector2 Vector2::getOrthogonal(bool polarity) const {
34 return polarity ? Vector2(-y, x) : Vector2(y, -x);
35}
36
37Vector2 Vector2::getOrthonormal(bool polarity, bool allowZero) const {
38 double len = length();
39 if (len == 0)
40 return polarity ? Vector2(0, !allowZero) : Vector2(0, -!allowZero);
41 return polarity ? Vector2(-y/len, x/len) : Vector2(y/len, -x/len);
42}
43
44Vector2 Vector2::project(const Vector2 &vector, bool positive) const {
45 Vector2 n = normalize(true);
46 double t = dotProduct(vector, n);
47 if (positive && t <= 0)
48 return Vector2();
49 return t*n;
50}
51
52Vector2::operator const void*() const {
53 return x || y ? this : NULL;
54}
55
56bool Vector2::operator!() const {
57 return !x && !y;
58}
59
60bool Vector2::operator==(const Vector2 &other) const {
61 return x == other.x && y == other.y;
62}
63
64bool Vector2::operator!=(const Vector2 &other) const {
65 return x != other.x || y != other.y;
66}
67
68Vector2 Vector2::operator+() const {
69 return *this;
70}
71
72Vector2 Vector2::operator-() const {
73 return Vector2(-x, -y);
74}
75
76Vector2 Vector2::operator+(const Vector2 &other) const {
77 return Vector2(x+other.x, y+other.y);
78}
79
80Vector2 Vector2::operator-(const Vector2 &other) const {
81 return Vector2(x-other.x, y-other.y);
82}
83
84Vector2 Vector2::operator*(const Vector2 &other) const {
85 return Vector2(x*other.x, y*other.y);
86}
87
88Vector2 Vector2::operator/(const Vector2 &other) const {
89 return Vector2(x/other.x, y/other.y);
90}
91
92Vector2 Vector2::operator*(double value) const {
93 return Vector2(x*value, y*value);
94}
95
96Vector2 Vector2::operator/(double value) const {
97 return Vector2(x/value, y/value);
98}
99
100Vector2 & Vector2::operator+=(const Vector2 &other) {
101 x += other.x, y += other.y;
102 return *this;
103}
104
105Vector2 & Vector2::operator-=(const Vector2 &other) {
106 x -= other.x, y -= other.y;
107 return *this;
108}
109
110Vector2 & Vector2::operator*=(const Vector2 &other) {
111 x *= other.x, y *= other.y;
112 return *this;
113}
114
115Vector2 & Vector2::operator/=(const Vector2 &other) {
116 x /= other.x, y /= other.y;
117 return *this;
118}
119
120Vector2 & Vector2::operator*=(double value) {
121 x *= value, y *= value;
122 return *this;
123}
124
125Vector2 & Vector2::operator/=(double value) {
126 x /= value, y /= value;
127 return *this;
128}
129
130double dotProduct(const Vector2 &a, const Vector2 &b) {
131 return a.x*b.x+a.y*b.y;
132}
133
134double crossProduct(const Vector2 &a, const Vector2 &b) {
135 return a.x*b.y-a.y*b.x;
136}
137
138Vector2 operator*(double value, const Vector2 &vector) {
139 return Vector2(value*vector.x, value*vector.y);
140}
141
142Vector2 operator/(double value, const Vector2 &vector) {
143 return Vector2(value/vector.x, value/vector.y);
144}
145
146}
147