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