-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec2.cpp
More file actions
154 lines (126 loc) · 2.23 KB
/
vec2.cpp
File metadata and controls
154 lines (126 loc) · 2.23 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
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
/* Author: Brendon Kofink
* Johann Rajadurai
* Aaron Sierra
* David Day
* Lucy Ray
* Assignment Title: Ball Game
* Assignment Description: user can launch balls to hit objects.
* Due Date: 12/08/2021
* Date Created: 10/21/2021
* Date Last Modified: 11/30/2021
*/
#include "vec2.h"
vec2::vec2(double setX, double setY)
{
x = setX;
y = setY;
}
vec2 vec2::operator-() const
{
return vec2(-x, -y);
}
vec2 vec2::add(const vec2 &other) const
{
return vec2(x + other.x, y + other.y);
}
vec2 vec2::operator+(const vec2 &other) const
{
return add(other);
}
vec2 vec2::operator-(const vec2 &other) const
{
return add(-other);
}
void vec2::apply(const vec2 &other)
{
x += other.x;
y += other.y;
}
void vec2::operator+=(const vec2 &other)
{
apply(other);
}
void vec2::operator-=(const vec2 &other)
{
apply(-other);
}
vec2 vec2::mult(double m) const
{
return vec2(x * m, y * m);
}
vec2 vec2::operator*(double m) const
{
return mult(m);
}
vec2 vec2::operator/(double m) const
{
return mult(1 / m);
}
void vec2::scale(double m)
{
x *= m;
y *= m;
}
void vec2::operator*=(double m)
{
scale(m);
}
void vec2::operator/=(double m)
{
scale(1 / m);
}
double vec2::distance(const vec2 &other) const
{
return sqrt(pow(x - other.x, 2) + pow(y - other.y, 2));
}
double vec2::magnitude() const
{
return sqrt(pow(x, 2) + pow(y, 2));
}
double vec2::sqrMagnitude() const
{
return pow(x, 2) + pow(y, 2);
}
double vec2::direction() const
{
return atan(y / x);
}
vec2 vec2::Angle(double angle) {
return vec2(cos(angle), sin(angle));
}
vec2 vec2::normalized() const {
return *this/magnitude();
}
double vec2::dot(const vec2 &other) const {
return x*other.x + y*other.y;
}
vec2 vec2::lerp(const vec2 &other, double t) const {
return *this + (other-*this)*t;
}
Edge::Edge() {
p1 = vec2(0, 0);
p2 = vec2(0, 0);
}
Edge::Edge(const vec2 a, const vec2 b)
{
if(a.x < b.x)
{
p1 = a;
p2 = b;
}
else
{
p1 = b;
p2 = a;
}
}
double Edge::Evaluate(double x) const
{
return p1.y + (p2.y - p1.y) * (x - p1.x) / (p2.x - p1.x);
}
vec2 Edge::GetNormal() const {
return vec2(p2.y - p1.y, -(p2.x - p1.x));
}
bool Edge::ContainsX(double x) const {
return p1.x <= x && x <= p2.x;
}