summaryrefslogtreecommitdiff
path: root/thirdparty/msdfgen/core/Vector2.cpp
blob: 896963ff2ccef4d44a75478ec568f000d69fb96e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

#include "Vector2.h"

namespace msdfgen {

Vector2::Vector2(double val) : x(val), y(val) { }

Vector2::Vector2(double x, double y) : x(x), y(y) { }

void Vector2::reset() {
    x = 0, y = 0;
}

void Vector2::set(double x, double y) {
    Vector2::x = x, Vector2::y = y;
}

double Vector2::length() const {
    return sqrt(x*x+y*y);
}

double Vector2::direction() const {
    return atan2(y, x);
}

Vector2 Vector2::normalize(bool allowZero) const {
    double len = length();
    if (len == 0)
        return Vector2(0, !allowZero);
    return Vector2(x/len, y/len);
}

Vector2 Vector2::getOrthogonal(bool polarity) const {
    return polarity ? Vector2(-y, x) : Vector2(y, -x);
}

Vector2 Vector2::getOrthonormal(bool polarity, bool allowZero) const {
    double len = length();
    if (len == 0)
        return polarity ? Vector2(0, !allowZero) : Vector2(0, -!allowZero);
    return polarity ? Vector2(-y/len, x/len) : Vector2(y/len, -x/len);
}

Vector2 Vector2::project(const Vector2 &vector, bool positive) const {
    Vector2 n = normalize(true);
    double t = dotProduct(vector, n);
    if (positive && t <= 0)
        return Vector2();
    return t*n;
}

Vector2::operator const void*() const {
    return x || y ? this : NULL;
}

bool Vector2::operator!() const {
    return !x && !y;
}

bool Vector2::operator==(const Vector2 &other) const {
    return x == other.x && y == other.y;
}

bool Vector2::operator!=(const Vector2 &other) const {
    return x != other.x || y != other.y;
}

Vector2 Vector2::operator+() const {
    return *this;
}

Vector2 Vector2::operator-() const {
    return Vector2(-x, -y);
}

Vector2 Vector2::operator+(const Vector2 &other) const {
    return Vector2(x+other.x, y+other.y);
}

Vector2 Vector2::operator-(const Vector2 &other) const {
    return Vector2(x-other.x, y-other.y);
}

Vector2 Vector2::operator*(const Vector2 &other) const {
    return Vector2(x*other.x, y*other.y);
}

Vector2 Vector2::operator/(const Vector2 &other) const {
    return Vector2(x/other.x, y/other.y);
}

Vector2 Vector2::operator*(double value) const {
    return Vector2(x*value, y*value);
}

Vector2 Vector2::operator/(double value) const {
    return Vector2(x/value, y/value);
}

Vector2 & Vector2::operator+=(const Vector2 &other) {
    x += other.x, y += other.y;
    return *this;
}

Vector2 & Vector2::operator-=(const Vector2 &other) {
    x -= other.x, y -= other.y;
    return *this;
}

Vector2 & Vector2::operator*=(const Vector2 &other) {
    x *= other.x, y *= other.y;
    return *this;
}

Vector2 & Vector2::operator/=(const Vector2 &other) {
    x /= other.x, y /= other.y;
    return *this;
}

Vector2 & Vector2::operator*=(double value) {
    x *= value, y *= value;
    return *this;
}

Vector2 & Vector2::operator/=(double value) {
    x /= value, y /= value;
    return *this;
}

double dotProduct(const Vector2 &a, const Vector2 &b) {
    return a.x*b.x+a.y*b.y;
}

double crossProduct(const Vector2 &a, const Vector2 &b) {
    return a.x*b.y-a.y*b.x;
}

Vector2 operator*(double value, const Vector2 &vector) {
    return Vector2(value*vector.x, value*vector.y);
}

Vector2 operator/(double value, const Vector2 &vector) {
    return Vector2(value/vector.x, value/vector.y);
}

}