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
|
#pragma once
#include <math.h>
#include <assert.h>
#include <stdlib.h>
#include <string>
#include "Settings.h"
#include "Constants.h"
namespace Phy2D
{
struct Vec2
{
Vec2() {}
Vec2(number x, number y) : x(x), y(y) {}
void Set(number x_, number y_) { x = x_; y = y_; }
Vec2 operator -() { return Vec2(-x, -y); }
void operator += (const Vec2& v)
{
x += v.x; y += v.y;
}
void operator -= (const Vec2& v)
{
x -= v.x; y -= v.y;
}
void operator *= (number a)
{
x *= a; y *= a;
}
number Length() const
{
return SQRT(x * x + y * y);
}
std::string ToString()
{
return std::to_string((float)x) + "," + std::to_string((float)y);
}
number x, y;
};
struct Mat22
{
Mat22() {}
Mat22(number angle)
{
number c = COS(angle), s = SIN(angle);
col1.x = c; col2.x = -s;
col1.y = s; col2.y = c;
}
Mat22(const Vec2& col1, const Vec2& col2) : col1(col1), col2(col2) {}
Mat22 Transpose() const
{
return Mat22(Vec2(col1.x, col2.x), Vec2(col1.y, col2.y));
}
Mat22 Invert() const
{
number a = col1.x, b = col2.x, c = col1.y, d = col2.y;
Mat22 B;
number det = a * d - b * c;
assert(det != _0);
det = (number)_1 / det;
B.col1.x = det * d; B.col2.x = -det * b;
B.col1.y = -det * c; B.col2.y = det * a;
return B;
}
Vec2 col1, col2;
};
inline number Dot(const Vec2& a, const Vec2& b)
{
return a.x * b.x + a.y * b.y;
}
inline number Cross(const Vec2& a, const Vec2& b)
{
return a.x * b.y - a.y * b.x;
}
inline Vec2 Cross(const Vec2& a, number s)
{
return Vec2(s * a.y, -s * a.x);
}
inline Vec2 Cross(number s, const Vec2& a)
{
return Vec2(-s * a.y, s * a.x);
}
inline Vec2 operator * (const Mat22& A, const Vec2& v)
{
return Vec2(A.col1.x * v.x + A.col2.x * v.y, A.col1.y * v.x + A.col2.y * v.y);
}
inline Vec2 operator + (const Vec2& a, const Vec2& b)
{
return Vec2(a.x + b.x, a.y + b.y);
}
inline Vec2 operator - (const Vec2& a, const Vec2& b)
{
return Vec2(a.x - b.x, a.y - b.y);
}
inline Vec2 operator * (number s, const Vec2& v)
{
return Vec2(s * v.x, s * v.y);
}
inline Mat22 operator + (const Mat22& A, const Mat22& B)
{
return Mat22(A.col1 + B.col1, A.col2 + B.col2);
}
inline Mat22 operator * (const Mat22& A, const Mat22& B)
{
return Mat22(A * B.col1, A * B.col2);
}
inline number Abs(number a)
{
return a > _0 ? a : -a;
}
inline Vec2 Abs(const Vec2& a)
{
return Vec2(fabsf(a.x), fabsf(a.y));
}
inline Mat22 Abs(const Mat22& A)
{
return Mat22(Abs(A.col1), Abs(A.col2));
}
inline number Sign(number x)
{
return x < _0 ? -_1 : _1;
}
inline number Min(number a, number b)
{
return a < b ? a : b;
}
inline number Max(number a, number b)
{
return a > b ? a : b;
}
inline number Clamp(number a, number low, number high)
{
return Max(low, Min(a, high));
}
template<typename T> inline void Swap(T& a, T& b)
{
T tmp = a;
a = b;
b = tmp;
}
//// Random number in range [-1,1]
//inline number Random()
//{
// number r = (number)rand();
// r /= RAND_MAX;
// r = (number)2.0f * r - _1;
// return r;
//}
//inline number Random(number lo, number hi)
//{
// number r = (number)rand();
// r /= RAND_MAX;
// r = (hi - lo) * r + lo;
// return r;
//}
}
|