-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase.h
58 lines (57 loc) · 1.35 KB
/
Base.h
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
#ifndef __SHARPPHYSICS_BASE_H_
#define __SHARPPHYSICS_BASE_H_
#include <cmath>
#include <limits>
namespace SharpPhysics {
typedef double spf;
// Durations are in seconds.
typedef spf Duration;
// Timestamps are in seconds since the physics engine was started.
typedef spf Timestamp;
const spf NaN = std::numeric_limits<spf>::quiet_NaN();
const spf Infinity = std::numeric_limits<spf>::infinity();
const spf PI = std::acos(-1);
struct Vec2d {
spf x, y;
static spf Dot(Vec2d a, Vec2d b) {
return a.x * b.x + a.y * b.y;
}
spf SqrMagnitude() const {
return x*x + y*y;
}
spf Magnitude() const {
return std::sqrt(SqrMagnitude());
}
Vec2d Normalized() const {
return (x == 0 && y == 0) ? Vec2d::Zero : *this * (1.0 / Magnitude());
}
Vec2d &operator+= (Vec2d a) {
x += a.x; y += a.y; return *this;
}
Vec2d operator* (spf n) const {
return Vec2d{ x * n, y*n };
}
Vec2d operator+ (Vec2d b) const {
return Vec2d{ x + b.x, y + b.y };
}
Vec2d operator- (Vec2d b) const {
return Vec2d{ x - b.x, y - b.y };
}
Vec2d operator-() const {
return Vec2d{ -x, -y };
}
static const Vec2d Zero;
static const Vec2d NaN;
bool IsNaN() const {
return std::isnan(x);
}
};
typedef Vec2d Point2d;
struct LineSeg {
Point2d a, b;
Vec2d GetDelta() const {
return b - a;
}
};
}
#endif //__SHARPPHYSICS_BASE_H_