-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredmath.cpp
More file actions
238 lines (219 loc) · 5.72 KB
/
redmath.cpp
File metadata and controls
238 lines (219 loc) · 5.72 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "redmath.hpp"
using namespace math_op;
/* VECTOR */
const Vector2 Vector2::Left(-1, 0);
const Vector2 Vector2::Right(1, 0);
const Vector2 Vector2::Up(0, 1);
const Vector2 Vector2::Down(0, -1);
const Vector2 Vector2::Zero(0, 0);
const Vector2 Vector2::One(1, 1);
Vector2::Vector2(){
this->x = 0;
this->y = 0;
}
Vector2::Vector2(double v[2]){
this->x = v[0];
this->y = v[1];
}
Vector2::Vector2(double xv, double yv){
this->x = xv;
this->y = yv;
}
Vector2 & Vector2::operator = (Vector2 other){
x = other.x;
y = other.y;
return *this;
}
/* DIV & MUL */
Vector2* Vector2::operator*(Vector2& rht){
this->x *= rht.x;
this->y *= rht.y;
return this;
}
Vector2* Vector2::operator*=(double num){
this->x *= num;
this->y *= num;
return this;
}
Vector2* Vector2::operator/(Vector2& rht){
this->x /= rht.x;
this->y /= rht.y;
return this;
}
Vector2& Vector2::operator/=(double num){
this->x /= num;
this->y /= num;
return *this;
}
/* SUB & SUM */
Vector2* Vector2::operator+(Vector2 & rht){
this->x += rht.x;
this->y += rht.y;
return this;
}
Vector2* Vector2::operator-(Vector2 & rht){
this->x -= rht.x;
this->y -= rht.y;
return this;
}
/* COMP */
bool Vector2::operator==(Vector2 & val){
return this->x == val.x && this->y == val.y;
}
void Vector2::Rotate(Vector2 point, double angle){
double angleRad = angle * PI / 180;
float newX = round(((this->x - point.x) * cos(angleRad) - (this->y - point.y) * sin(angleRad) + point.x) * 100) / 100;
float newY = round(((this->x - point.x) * sin(angleRad) + (this->y - point.y) * cos(angleRad) + point.y) * 100) / 100;
this->x = newX;
this->y = newY;
}
void Vector2::Rotate(double angle){
Rotate(Vector2(0, 0), angle);
}
double Vector2::GetAngle(){
return atan2(this->y,this->x) * 180 / PI;
}
double Vector2::Size(){
return sqrt(pow(this->x, 2) + pow(this->y, 2));
}
Vector2* Vector2::Normalize(){
this->x = this->x / Size();
this->y = this->y / Size();
return this;
}
/* VECTOR END */
unsigned int math_op::GetFactorial(unsigned int number)
{
return number <= 1 ? number : math_op::GetFactorial(number-1) * number;
}
double geom::area_of_polygon_inside_circle(double circle_radius, int number_sides) {
double area = number_sides * (0.5 * pow(circle_radius, 2) * sin(2 * PI / number_sides));
return round(area * 1e3) / 1e3;
}
double geom::area_of_circle(double circle_radius) {
double area = PI * pow(circle_radius, 2);
return round(area * 1e3) / 1e3;
}
/* LINE */
Line::Line(double v1[2], double v2[2]){
from = Vector2(v1);
to = Vector2(v2);
}
Line::Line(double coord[4]){
from = Vector2(coord[0], coord[1]);
to = Vector2(coord[2], coord[3]);
}
Line::Line(double x1, double y1, double x2, double y2){
from = Vector2(x1, y1);
to = Vector2(x2, y2);
}
Line::Line(Vector2 v1, Vector2 v2){
from = v1;
to = v2;
}
double Line::Size(){
return sqrt(pow(to.x - from.x, 2) + pow(to.y - from.y, 2));
}
void Line::Rotate(Vector2 point, double angle){
double angleRad = angle * PI_s / 180;
Vector2 _from;
_from.x = round(((this->from.x - point.x) * cos(angleRad) - (this->from.y - point.y) * sin(angleRad) + point.x) * 100) / 100;
_from.y = round(((this->from.x - point.x) * sin(angleRad) + (this->from.y - point.y) * cos(angleRad) + point.y) * 100) / 100;
this->from = _from;
Vector2 _to;
_to.x = round(((this->to.x - point.x) * cos(angleRad) - (this->to.y - point.y) * sin(angleRad) + point.x) * 100) / 100;
_to.y = round(((this->to.x - point.x) * sin(angleRad) + (this->to.y - point.y) * cos(angleRad) + point.y) * 100) / 100;
this->to = _to;
}
Vector2 Line::GetCenter(){
return Vector2((from.x + to.x) / 2, (from.y + to.y) / 2);
}
Line* Line::Normalize(){
from.Normalize();
to.Normalize();
return this;
}
Vector2 Line::Direction(){
return *(this->to - this->from);
}
void Line::Move(Vector2 tv){
this->from.x += tv.x;
this->from.y += tv.y;
this->to.x += tv.x;
this->to.y += tv.y;
}
/* LINE END */
Vector3::Vector3(){
this->x = 0;
this->y = 0;
this->z = 0;
}
Vector3::Vector3(double v[3]){
this->x = v[0];
this->y = v[1];
this->z = v[2];
}
Vector3::Vector3(double xv, double yv, double zv){
this->x = xv;
this->y = yv;
this->z = zv;
}
Vector3 & Vector3::operator = (Vector3 other){
this->x = other.x;
this->y = other.y;
this->z = other.z;
return *this;
}
Vector3* Vector3::operator*(Vector3& rht){
this->x *= rht.x;
this->y *= rht.y;
this->z *= rht.z;
return this;
}
Vector3* Vector3::operator*=(double num){
this->x *= num;
this->y *= num;
this->z *= num;
return this;
}
Vector3* Vector3::operator/(Vector3& rht){
this->x /= rht.x;
this->y /= rht.y;
this->z /= rht.z;
return this;
}
Vector3& Vector3::operator/=(double num){
this->x /= num;
this->y /= num;
this->z /= num;
return *this;
}
Vector3* Vector3::operator+(Vector3 & rht){
this->x += rht.x;
this->y += rht.y;
this->z += rht.z;
return this;
}
Vector3* Vector3::operator-(Vector3 & rht){
this->x -= rht.x;
this->y -= rht.y;
this->z -= rht.z;
return this;
}
double Vector3::Size(){
return sqrt(pow(this->x, 2) + pow(this->y, 2) + pow(this->z, 2));
}
Vector3* Vector3::Normalize(){
this->x = this->x / Size();
this->y = this->y / Size();
this->z = this->z / Size();
return this;
}
const Vector3 Vector3::Left(-1, 0, 0);
const Vector3 Vector3::Right(1, 0, 0);
const Vector3 Vector3::Up(0, 1, 0);
const Vector3 Vector3::Down(0, -1, 0);
const Vector3 Vector3::Forward(0, 0, 1);
const Vector3 Vector3::Backward(0, 0, -1);
const Vector3 Vector3::Zero(0, 0, 0);
const Vector3 Vector3::One(1, 1, 1);