forked from PrototypeX29A/norbit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmath2d.h
More file actions
233 lines (175 loc) · 4.6 KB
/
math2d.h
File metadata and controls
233 lines (175 loc) · 4.6 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
/*----------------------------------------------------------------------------
2D Physics Test Program - a cheesy test harness for 2D physics
by Chris Hecker for my Game Developer Magazine articles. See my homepage
for more information.
NOTE: This is a hacked test program, not a nice example of Windows programming.
physics.cpp the only part of this you should look at!!!
This material is Copyright 1997 Chris Hecker, All Rights Reserved.
It's for you to read and learn from, not to put in your own articles
or books or on your website, etc. Thank you.
Chris Hecker
checker@d6.com
http://www.d6.com/users/checker
*/
/*----------------------------------------------------------------------------
math2d.h - The simple math library for the 2D physics demo.
10/13/96 - checker
*/
#if !defined(MATH2D_H)
#define MATH2D_H
// explicit dependencies
#include <math.h> // for sqrt
#include <float.h>
/*----------------------------------------------------------------------------
Type and constant declarations.
*/
typedef float real;
typedef real r; // for constants, ie. r(1), not for declaration use
real const REAL_MAX = FLT_MAX;
real const REAL_MIN = FLT_MIN;
real const PI = r (3.14159265358979323846);
real const Epsilon = r (0.00001);
struct vector_2
{
real X, Y;
inline vector_2 (void);
inline vector_2 (real X, real Y);
inline vector_2 & operator+= (vector_2 const &A);
inline vector_2 & operator-= (vector_2 const &A);
};
struct matrix_2x2
{
real aElements[2][2];
inline matrix_2x2 (void);
inline matrix_2x2 (real RotateByThisManyRadians);
};
/*----------------------------------------------------------------------------
Functions.
*/
inline real RadiansFrom (real Degrees);
inline real DegreesFrom (real Radians);
inline vector_2 operator- (vector_2 const &A, vector_2 const &B);
inline vector_2 operator+ (vector_2 const &A, vector_2 const &B);
inline vector_2 operator* (real A, vector_2 const &B);
inline vector_2 operator* (vector_2 const &A, real B);
inline vector_2 operator/ (vector_2 const &A, real B);
inline real DotProduct (vector_2 const &A, vector_2 const &B);
// A-perp dot B
inline real PerpDotProduct (vector_2 const &A, vector_2 const &B);
inline vector_2 GetPerpendicular (vector_2 const &A);
inline real GetLength (vector_2 const &A);
inline vector_2 GetNormal (vector_2 const &A); // @todo need this?
inline vector_2 operator* (matrix_2x2 const &A, vector_2 const &B);
/*----------------------------------------------------------------------------
Inline function definitions.
*/
inline real
RadiansFrom (real Degrees)
{
return (Degrees * PI) / r (180);
}
inline real
DegreesFrom (real Radians)
{
return (Radians * r (180)) / PI;
}
inline vector_2
operator- (vector_2 const &A, vector_2 const &B)
{
return vector_2 (A.X - B.X, A.Y - B.Y);
}
inline vector_2
operator+ (vector_2 const &A, vector_2 const &B)
{
return vector_2 (A.X + B.X, A.Y + B.Y);
}
inline vector_2
operator* (real A, vector_2 const &B)
{
return vector_2 (A * B.X, A * B.Y);
}
inline vector_2
operator* (vector_2 const &A, real B)
{
return vector_2 (B * A.X, B * A.Y);
}
inline vector_2
operator/ (vector_2 const &A, real B)
{
return vector_2 (A.X / B, A.Y / B);
}
inline real
DotProduct (vector_2 const &A, vector_2 const &B)
{
return A.X * B.X + A.Y * B.Y;
}
inline real
PerpDotProduct (vector_2 const &A, vector_2 const &B)
{
return A.X * B.Y - A.Y * B.X;
}
inline vector_2
GetPerpendicular (vector_2 const &A)
{
return vector_2 (-A.Y, A.X);
}
inline real
GetLength (vector_2 const &A)
{
return r (sqrt (A.X * A.X + A.Y * A.Y));
}
inline vector_2
GetNormal (vector_2 const &A)
{
real OneOverLength = r (1) / GetLength (A);
return vector_2 (OneOverLength * A.X, OneOverLength * A.Y);
}
inline
vector_2::vector_2 (void):
X (r (0)),
Y (r (0))
{
}
inline
vector_2::vector_2 (real X_, real Y_):
X (X_),
Y (Y_)
{
}
inline vector_2 &
vector_2::operator+= (vector_2 const &A)
{
X += A.X;
Y += A.Y;
return *this;
}
inline vector_2 &
vector_2::operator-= (vector_2 const &A)
{
X -= A.X;
Y -= A.Y;
return *this;
}
inline vector_2
operator* (matrix_2x2 const &A, vector_2 const &B)
{
return vector_2 (A.aElements[0][0] * B.X + A.aElements[0][1] * B.Y,
A.aElements[1][0] * B.X + A.aElements[1][1] * B.Y);
}
inline
matrix_2x2::matrix_2x2 (void)
{
aElements[0][0] = aElements[0][1] = aElements[1][0] = aElements[1][1] =
r (0);
}
inline
matrix_2x2::matrix_2x2 (real Radians)
{
real const CosAngle = (real) cos (Radians);
real const SinAngle = (real) sin (Radians);
aElements[0][0] = CosAngle;
aElements[0][1] = -SinAngle;
aElements[1][0] = SinAngle;
aElements[1][1] = CosAngle;
}
#endif