-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.cpp
417 lines (366 loc) · 9.63 KB
/
math.cpp
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
#############################################################################################
# Tux in Space - space exploration game #
# Copyright (C) 2016-2017 [email protected] #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, version 3 or compatibles. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc. #
#############################################################################################
*
* Low level math functions
*
*/
#include <cstdlib>
#include <cmath>
#include <ctime>
#include "math.h"
/***
* Return a "random" int in a range given, but the gap can't be greater than 100
*/
int math_RandomI (int min, int max) {
srand(time(nullptr));
return (rand() % (max - min)) + min;
}
// TFORCE SCALAR
long double tforce_scalar::value() const {
return m;
}
bool tforce_scalar::operator== (const tforce_scalar& f) const{
return m == f.m;
}
bool tforce_scalar::operator!= (const tforce_scalar& f) const{
return m != f.m;
}
// TPOSITION
tposition tposition::operator- (const tposition& p) const {
return tposition(v - p.v);
}
void tposition::operator+= (const tposition& p) {
v += p.v;
}
tpositionmass tposition::operator* (const tmass& m) const{
return tpositionmass(v * m.m);
}
tlength tposition::scalar() const {
return v.length();
}
vec3<long double> tposition::direction() const{
return v.direction();
}
vec3<long double> tposition::value() const{
return v;
}
bool tposition::operator== (const tposition& p) const{
return p.v == v;
}
bool tposition::operator!= (const tposition& p) const{
return p.v != v;
}
// TPOSITIONMASS
tposition tpositionmass::operator/ (const tmass& m){
return tposition(v / m.m);
}
// TAREA
tvolume tarea::operator* (const tlength& l){
return tvolume(m * l.m);
}
// TLENGTH
tarea tlength::operator* (const tlength& l){
return tarea(m * l.m);
}
void tlength::operator= (const long double& _m) {
m = _m;
}
void tlength::operator-= (const tlength& l) {
m -= l.m;
}
void tlength::operator+= (const tlength& l) {
m += l.m;
}
tlength tlength::operator+ (const tlength& l) const {
return tlength(m + l.m);
}
tlength tlength::operator- (const tlength& l) const {
return tlength(m - l.m);
}
bool tlength::operator== (const tlength& l) const {
if (l.m == m)
return true;
return false;
}
void tlength::operator>> (std::ostream& s){
s << std::to_string(m);
}
bool tlength::operator> (const tlength& t) const {
if (m > t.m)
return true;
return false;
}
bool tlength::operator< (const tlength& t) const {
if (m < t.m)
return true;
return false;
}
bool tlength::operator<= (const tlength& t) const {
if (m <= t.m)
return true;
return false;
}
tlength tlength::operator* (const long double& n){
return tlength(m * n);
}
long double tlength::operator/ (const tlength& l){
return m/l.m;
}
long double tlength::value() const {
return m;
}
bool tlength::operator== (const tlength& l){
return m == l.m;
}
bool tlength::operator!= (const tlength& l){
return m != l.m;
}
// TGUC
tforce tguc::newton(const tmass& mass1, const tmass& mass2, const tposition& dist) {
// apply the newton law of gravitation
tforce f(
dist.direction(),
m * mass1.value() * mass2.value() / (dist.scalar().value() * dist.scalar().value())
);
return f;
}
// TVELOCITY
tmomentum tvelocity::operator* (const tmass& m) const{
return tmomentum(v * m.m);
}
vec3<long double> tvelocity::value() const{
return v;
}
void tvelocity::operator+= (const tvelocity& t){
v += t.v;
}
tposition tvelocity::operator* (const time_raw& t) const {
return tposition(v * t.time());
}
tspeed tvelocity::scalar() const{
return tspeed(v.length());
}
tmomentum tvelocity::operator* (const tmass& m) const;
tvelocity tvelocity::operator+ (const tvelocity& vel) const{
return tvelocity(v+vel.v);
}
tvelocity tvelocity::operator- (const tvelocity& vel) const{
return tvelocity(v-vel.v);
}
bool tvelocity::operator< (const tvelocity& vel) const {
return v.length() < vel.v.length();
}
bool tvelocity::operator> (const tvelocity& vel) const {
return v.length() > vel.v.length();
}
tacceleration tvelocity::operator/ (const time_raw& t) const{
return tacceleration(v / t.time());
}
tvelocity tvelocity::operator- (void) const{
return tvelocity(-v);
}
bool tvelocity::operator== (const tvelocity& p) const{
return p.v == v;
}
bool tvelocity::operator!= (const tvelocity& p) const{
return p.v != v;
}
// TSPEED
long double tspeed::value() const {
return m;
}
tspeed tspeed::operator+ (const tspeed& s) const{
return tspeed(m + s.m);
}
tspeed tspeed::operator- (const tspeed& s) const{
return tspeed(m - s.m);
}
tacceleration_scalar tspeed::operator/ (const time_raw& t) const {
return tacceleration_scalar(m/t.time());
}
tspeed tspeed::operator* (const long double& a) const{
return tspeed(m*a);
}
tspeed tspeed::operator/ (const long double& a) const{
return tspeed(m/a);
}
void tspeed::operator+= (const tspeed& s){
m += s.m;
}
void tspeed::operator-= (const tspeed& s){
m -= s.m;
}
// TACCELERATION
tforce tacceleration::operator* (const tmass& m) const{
return tforce(v * m.m);
};
long double tacceleration::value() const {
return v.length();
}
// TACCELERATION_SCALAR
tacceleration_scalar tacceleration_scalar::operator/ (const long double& n) const{
return tacceleration_scalar(m/n);
}
tacceleration_scalar tacceleration_scalar::operator* (const long double& n) const{
return tacceleration_scalar(m*n);
}
inline long double tacceleration_scalar::value() const {
return m;
}
// TMOMENTUM
tvelocity tmomentum::operator/ (const tmass& m) const{
return tvelocity(v / m.value());
}
tmomentum tmomentum::operator+ (const tmomentum& i) const {
return tmomentum(v + i.v);
}
tforce tmomentum::operator/ (const time_raw& t) const{
return tforce(v / t.time());
}
tmomentum tmomentum::operator* (const vec3<long double>& vector) const{
return tmomentum(vec3<long double>(v.x*vector.x, v.y*vector.y, v.z*vector.z));
}
tmomentum tmomentum::operator/ (const vec3<long double>& vector) const{
return tmomentum(vec3<long double>(v.x/vector.x, v.y/vector.y, v.z/vector.z));
}
vec3<long double> tmomentum::value() const {
return v;
}
// TMOMENTUM SCALAR
tmomentum_scalar tmomentum_scalar::operator+ (const tmomentum_scalar& s) const{
return tmomentum_scalar(m + s.m);
};
tenergy tmomentum_scalar::operator* (const tspeed& s) const{
return tenergy(m * s.value());
}
tmomentum_scalar tmomentum_scalar::operator/ (const long double& n) const{
return tmomentum_scalar(m/n);
}
tmomentum_scalar tmomentum_scalar::operator* (const long double& n) const{
return tmomentum_scalar(m/n);
}
tforce_scalar tmomentum_scalar::operator/ (const time_raw& t) const{
return tforce_scalar(m/t.time());
}
// TMASS
tmomentum tmass::operator* (const tvelocity& vel) const{
return tmomentum(vel.value() * m);
}
tmass tmass::operator/ (const long double& n) const{
return tmass(m/n);
}
void tmass::operator= (const tmass& t) {
m = t.m;
}
void tmass::operator>> (std::ostream& s){
s << std::to_string(m);
}
void tmass::operator+= (const tmass& t) {
m += t.m;
}
void tmass::operator*= (const long double& pure) {
m *= pure;
}
tmass tmass::operator* (const long double& pure) const{
return tmass(m * pure);
}
tforce_scalar tmass::operator* (const tacceleration_scalar& a) const{
return tforce_scalar(m * a.value());
}
tmomentum_scalar tmass::operator* (const tspeed& v) const{
return tmomentum_scalar(v.value() * m);
}
long double tmass::operator/ (const tmass& t) const{
return m / t.m;
}
tmass tmass::operator+ (const tmass& t) const {
return tmass(m + t.m);
}
bool tmass::operator> (const tmass& t) const {
if(m > t.m)
return true;
return false;
}
bool tmass::operator< (const tmass& t) const {
if(m < t.m)
return true;
return false;
}
long double tmass::value() const {
return m;
}
bool tmass::operator== (const tmass& t) const{
return m == t.m;
}
bool tmass::operator!= (const tmass& t) const{
return m != t.m;
}
// TENERGY
tenergy tenergy::operator+ (const tenergy& e) const{
return tenergy(m + e.m);
}
tenergy tenergy::operator- (const tenergy& e) const{
return tenergy(m - e.m);
}
long double tenergy::value() const{
return m;
}
// TFORCE
tforce tforce::operator+ (const tforce& f) const {
return tforce(v + f.v);
}
tforce tforce::operator- (const tforce& f) const{
return tforce(v - f.v);
}
void tforce::operator+= (const tforce& f) {
v += f.v;
}
void tforce::operator-= (const tforce& f) {
v -= f.v;
}
tforce tforce::operator* (const long double& pure) const{
return tforce(v * pure);
}
tacceleration tforce::operator/ (const tmass& m) const {
return tacceleration(v / m.value());
}
tmomentum tforce::operator* (const time_raw& t) const {
return tmomentum(v * t.time());
}
tforce tforce::operator- (void) const {
return tforce(-v);
}
bool tforce::operator> (const tforce_scalar& f) const {
return v.length() > f.value();
}
bool tforce::operator< (const tforce_scalar& f) const {
return v.length() < f.value();
}
void tforce::zero() {
v.zero();
}
long double tforce::value() const{
return v.length();
}
bool tforce::operator== (const tforce& f) const{
return v == f.v;
}
bool tforce::operator!= (const tforce& f) const{
return v != f.v;
}