-
Notifications
You must be signed in to change notification settings - Fork 16
/
Common.h
127 lines (113 loc) · 3.46 KB
/
Common.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
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
/*
* This file is part of NumptyPhysics
* Copyright (C) 2008 Tim Edmonds
*
* 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; either version 3 of the
* License, or (at your option) any later version.
*
* 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.
*
*/
#ifndef COMMON_H
#define COMMON_H
#include "Box2D.h"
#define ARRAY_SIZE(aRR) (sizeof(aRR)/sizeof((aRR)[0]))
#define ASSERT(a)
#define FIXED_SHIFT 16
#define FIXED_ONE (1<<FIXED_SHIFT)
#define FLOAT32_TO_FIXED(float32) ((int)(float32*(float32)FIXED_ONE))
#define FIXED_TO_INT(iNT) ((iNT)>>FIXED_SHIFT)
struct Vec2 {
Vec2() {}
Vec2( const Vec2& o ) : x(o.x), y(o.y) {}
explicit Vec2( const b2Vec2& o ) : x((int)o.x), y((int)o.y) {}
Vec2( int xx, int yy ) : x(xx), y(yy) {}
void operator+=( const Vec2& o ) { x+=o.x; y+=o.y; }
void operator-=( const Vec2& o ) { x-=o.x; y-=o.y; }
Vec2 operator-() { return Vec2(-x,-y); }
void operator*=( int o ) { x*=o; y*=o; }
bool operator==( const Vec2& o ) const { return x==o.x && y==o.y; }
bool operator!=( const Vec2& o ) const { return !(*this==o); }
operator b2Vec2() const { return b2Vec2((float32)x,(float32)y); }
Vec2 operator+( const Vec2& b ) const { return Vec2(x+b.x,y+b.y); }
Vec2 operator-( const Vec2& b ) const { return Vec2(x-b.x,y-b.y); }
Vec2 operator/( int r ) const { return Vec2(x/r,y/r); }
Vec2 operator*( int r ) const { return Vec2(x*r,y*r); }
int x,y;
};
template <typename T> inline T Min( T a, T b )
{
return a < b ? a : b;
}
inline Vec2 Min( const Vec2& a, const Vec2& b )
{
Vec2 r;
r.x = Min(a.x,b.x);
r.y = Min(a.y,b.y);
return r;
}
template <typename T> inline T Max( T a, T b )
{
return a >= b ? a : b;
}
inline Vec2 Max( const Vec2& a, const Vec2& b )
{
Vec2 r;
r.x = Max(a.x,b.x);
r.y = Max(a.y,b.y);
return r;
}
#define Sgn(a) ((a)<0?-1:1)
#define Abs(a) ((a)<0?-(a):(a))
struct Rect {
Rect() {}
Rect(bool) { clear(); }
Rect( const Vec2& atl, const Vec2& abr ) : tl(atl), br(abr) {}
Rect( int x1, int y1, int x2, int y2 ) : tl(x1,y1), br(x2,y2) {}
int width() const { return br.x-tl.x+1; }
int height() const { return br.y-tl.y+1; }
Vec2 size() const { return br-tl; }
void clear() { tl.x=tl.y=br.x=br.y=0; }
bool isEmpty() const { return tl.x==0 && br.x==0; }
void grow(int by) {
if (!isEmpty()) {
tl.x -= by; tl.y -= by;
br.x += by; br.y += by;
}
}
void expand( const Vec2& v ) { tl=Min(tl,v); br=Max(br,v); }
void expand( const Rect& r ) {
if (isEmpty()) {
*this = r;
} else if (!r.isEmpty()) {
expand(r.tl);
expand(r.br);
}
}
void clipTo( const Rect& r ) { tl=Max(tl,r.tl); br=Min(br,r.br); }
bool contains( const Vec2& p ) const {
return p.x >= tl.x && p.x <= br.x && p.y >= tl.y && p.y <= br.y;
}
bool contains( const Rect& p ) const {
return contains(p.tl) && contains(p.br);
}
bool intersects( const Rect& r ) const {
return r.tl.x <= br.x
&& r.tl.y <= br.y
&& r.br.x >= tl.x
&& r.br.y >= tl.y;
}
Vec2 centroid() const { return (tl+br)/2; }
Rect operator+( const Vec2& b ) const {
Rect r=*this;
r.tl += b; r.br += b;
return r;
}
Vec2 tl, br;
};
#endif //COMMON_H