aboutsummaryrefslogtreecommitdiffstats
path: root/dev/MinGfx/src/vector2.cc
blob: 92308f7147ac457ebdcdaddf1ff2f162d10f8190 (plain) (blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 Copyright (c) 2017,2018 Regents of the University of Minnesota.
 All Rights Reserved.
 See corresponding header file for details.
 */

#include "vector2.h"

#include <math.h>

namespace mingfx {
    
    
static const Vector2 s_zerov2d = Vector2(0,0);
static const Vector2 s_onev2d = Vector2(1,1);
static const Vector2 s_unitxv2d = Vector2(1,0);
static const Vector2 s_unityv2d = Vector2(0,1);

const Vector2& Vector2::Zero() { return s_zerov2d; }
const Vector2& Vector2::One() { return s_onev2d; }
const Vector2& Vector2::UnitX() { return s_unitxv2d; }
const Vector2& Vector2::UnitY() { return s_unityv2d; }

Vector2::Vector2() {
    v[0] = 0.0;
    v[1] = 0.0;
}

Vector2::Vector2(float x, float y) {
    v[0] = x;
    v[1] = y;
}

Vector2::Vector2(float *ptr) {
    v[0] = ptr[0];
    v[1] = ptr[1];
}

Vector2::Vector2(const Vector2& other) {
    v[0] = other[0];
    v[1] = other[1];
}

Vector2::~Vector2() {
}

bool Vector2::operator==(const Vector2& other) const {
    return (fabs(other[0] - v[0]) < MINGFX_MATH_EPSILON &&
            fabs(other[1] - v[1]) < MINGFX_MATH_EPSILON);
}

bool Vector2::operator!=(const Vector2& other) const {
    return (fabs(other[0] - v[0]) >= MINGFX_MATH_EPSILON ||
            fabs(other[1] - v[1]) >= MINGFX_MATH_EPSILON);
}

Vector2& Vector2::operator=(const Vector2& other) {
    v[0] = other[0];
    v[1] = other[1];
    return *this;
}

float Vector2::operator[](const int i) const {
    if ((i>=0) && (i<=1)) {
        return v[i];
    }
    else {
        // w component of a vector is 0 so return the constant 0.0
        return 0.0;
    }
}

float& Vector2::operator[](const int i) {
    return v[i];
}

float Vector2::Dot(const Vector2& other) const {
    return v[0]*other[0] + v[1]*other[1];
}

float Vector2::Length() const {
    return sqrt(v[0]*v[0] + v[1]*v[1]);
}

void Vector2::Normalize() {
    // Hill & Kelley provide this:
    float sizeSq =  + v[0]*v[0] + v[1]*v[1];
    if (sizeSq < MINGFX_MATH_EPSILON) {
        return; // do nothing to zero vectors;
    }
    float scaleFactor = (float)1.0/(float)sqrt(sizeSq);
    v[0] *= scaleFactor;
    v[1] *= scaleFactor;
}

Vector2 Vector2::ToUnit() const {
    Vector2 v(*this);
    v.Normalize();
    return v;
}


Vector2 Vector2::Lerp(const Vector2 &b, float alpha) const {
    float x = (1.0f-alpha)*(*this)[0] + alpha*b[0];
    float y = (1.0f-alpha)*(*this)[1] + alpha*b[1];
    return Vector2(x,y);
}

Vector2 Vector2::Lerp(const Vector2 &a, const Vector2 &b, float alpha) {
    float x = (1.0f-alpha)*a[0] + alpha*b[0];
    float y = (1.0f-alpha)*a[1] + alpha*b[1];
    return Vector2(x,y);
}

    
const float * Vector2::value_ptr() const {
    return v;
}

    
Vector2 Vector2::Normalize(const Vector2 &v) {
    return v.ToUnit();
}


float Vector2::Dot(const Vector2 &v1, const Vector2 &v2) {
    return v1.Dot(v2);
}
    


Vector2 operator/(const Vector2& v, const float s) {
    const float invS = 1 / s;
    return Vector2(v[0]*invS, v[1]*invS);
}

Vector2 operator*(const float s, const Vector2& v) {
    return Vector2(v[0]*s, v[1]*s);
}

Vector2 operator*(const Vector2& v, const float s) {
    return Vector2(v[0]*s, v[1]*s);
}

Vector2 operator-(const Vector2& v) {
    return Vector2(-v[0], -v[1]);
}

Point2 operator+(const Vector2& v, const Point2& p) {
    return Point2(p[0] + v[0], p[1] + v[1]);
};

Point2 operator+(const Point2& p, const Vector2& v) {
    return Point2(p[0] + v[0], p[1] + v[1]);
}

Vector2 operator+(const Vector2& v1, const Vector2& v2) {
    return Vector2(v1[0] + v2[0], v1[1] + v2[1]);
}

Point2 operator-(const Point2& p, const Vector2& v) {
    return Point2(p[0] - v[0], p[1] - v[1]);
}

Vector2 operator-(const Vector2& v1, const Vector2& v2) {
    return Vector2(v1[0] - v2[0], v1[1] - v2[1]);
}

Vector2 operator-(const Point2& p1, const Point2& p2) {
    return Vector2(p1[0] - p2[0], p1[1] - p2[1]);
}


std::ostream & operator<< ( std::ostream &os, const Vector2 &v) {
    return os << "<" << v[0] << ", " << v[1] << ">";
}

std::istream & operator>> ( std::istream &is, Vector2 &v) {
    // format:  <x, y, z>
    char dummy;
    return is >> dummy >> v[0] >> dummy >> v[1] >> dummy;
}

    
} // end namespace