-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec2.cpp
More file actions
56 lines (55 loc) · 1.87 KB
/
vec2.cpp
File metadata and controls
56 lines (55 loc) · 1.87 KB
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
/* Author: Brendon Kofink
* Wesley Goyette
* Aaron Sierra
* David Day
* Lucy Ray
* Assignment Title: Epicer Puzzle
* Assignment Description: A Puzzle game where the user can
* solve a puzzle by moving and snapping pieces into place.
* Due Date: 5/4/2023
* Date Created: 4/20/2023
* Date Last Modified: 4/20/2023
*/
#include "vec2.h"
std::ostream &operator<<(std::ostream &os, iVec2 v);
iVec2 Vec2::toIVec2() { return {static_cast<int>(x), static_cast<int>(y)}; }
Vec2::Vec2(float x, float y) : x(x), y(y) {}
Vec2 Vec2::operator+(Vec2 other) { return Vec2(x + other.x, y + other.y); }
Vec2 Vec2::operator-(Vec2 other) { return Vec2(x - other.x, y - other.y); }
Vec2 Vec2::operator*(float scalar) { return Vec2(x * scalar, y * scalar); }
Vec2 Vec2::operator/(float scalar) { return Vec2(x / scalar, y / scalar); }
Vec2 Vec2::operator+=(Vec2 other) {
x += other.x;
y += other.y;
return *this;
}
Vec2 Vec2::operator-=(Vec2 other) {
x -= other.x;
y -= other.y;
return *this;
}
Vec2 Vec2::operator*=(float scalar) {
x *= scalar;
y *= scalar;
return *this;
}
Vec2 Vec2::operator/=(float scalar) {
x /= scalar;
y /= scalar;
return *this;
}
Vec2 Vec2::flip() { return Vec2(y, x); }
Vec2 Vec2::ycomp() { return Vec2(0, y); }
Vec2 Vec2::xcomp() { return Vec2(x, 0); }
Vec2 Vec2::splatx() { return Vec2(x, x); }
Vec2 Vec2::splaty() { return Vec2(y, y); }
std::ostream &operator<<(std::ostream &os, Vec2 v) {
os << "(" << v.x << ", " << v.y << ")";
return os;
}
Vec2 Vec2::fromPoint(point p) { return Vec2(p.x, p.y); }
point Vec2::toPoint() { return {static_cast<int>(x), static_cast<int>(y)}; }
float Vec2::sqMagnitude() { return x * x + y * y; }
float Vec2::dot(Vec2 other) { return x * other.x + y * other.y; }
float Vec2::magnitude() { return sqrt(sqMagnitude()); }
Vec2 Vec2::normalized() { return *this / magnitude(); }