2 * This file is part of NumptyPhysics
3 * Copyright (C) 2008 Tim Edmonds
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
21 #define ARRAY_SIZE(aRR) (sizeof(aRR)/sizeof((aRR)[0]))
24 #define FIXED_SHIFT 16
25 #define FIXED_ONE (1<<FIXED_SHIFT)
26 #define FLOAT32_TO_FIXED(float32) ((int)(float32*(float32)FIXED_ONE))
27 #define FIXED_TO_INT(iNT) ((iNT)>>FIXED_SHIFT)
31 Vec2( const Vec2
& o
) : x(o
.x
), y(o
.y
) {}
32 explicit Vec2( const b2Vec2
& o
) : x((int)o
.x
), y((int)o
.y
) {}
33 Vec2( int xx
, int yy
) : x(xx
), y(yy
) {}
34 void operator+=( const Vec2
& o
) { x
+=o
.x
; y
+=o
.y
; }
35 void operator-=( const Vec2
& o
) { x
-=o
.x
; y
-=o
.y
; }
36 Vec2
operator-() { return Vec2(-x
,-y
); }
37 void operator*=( int o
) { x
*=o
; y
*=o
; }
38 bool operator==( const Vec2
& o
) const { return x
==o
.x
&& y
==y
; }
39 bool operator!=( const Vec2
& o
) const { return !(*this==o
); }
40 operator b2Vec2() const { return b2Vec2((float32
)x
,(float32
)y
); }
41 Vec2
operator+( const Vec2
& b
) const { return Vec2(x
+b
.x
,y
+b
.y
); }
42 Vec2
operator-( const Vec2
& b
) const { return Vec2(x
-b
.x
,y
-b
.y
); }
43 Vec2
operator/( int r
) const { return Vec2(x
/r
,y
/r
); }
47 template <typename T
> inline T
Min( T a
, T b
)
52 inline Vec2
Min( const Vec2
& a
, const Vec2
& b
)
60 template <typename T
> inline T
Max( T a
, T b
)
62 return a
>= b
? a
: b
;
65 inline Vec2
Max( const Vec2
& a
, const Vec2
& b
)
73 #define Sgn(a) ((a)<0?-1:1)
74 #define Abs(a) ((a)<0?-(a):(a))
80 Rect( const Vec2
& atl
, const Vec2
& abr
) : tl(atl
), br(abr
) {}
81 Rect( int x1
, int y1
, int x2
, int y2
) : tl(x1
,y1
), br(x2
,y2
) {}
82 int width() const { return br
.x
-tl
.x
+1; }
83 int height() const { return br
.y
-tl
.y
+1; }
84 void clear() { tl
.x
=tl
.y
=br
.x
=br
.y
=0; }
85 bool isEmpty() { return tl
.x
==0 && br
.x
==0; }
86 void expand( const Vec2
& v
) { tl
=Min(tl
,v
); br
=Max(br
,v
); }
87 void expand( const Rect
& r
) { expand(r
.tl
); expand(r
.br
); }
88 void clipTo( const Rect
& r
) { tl
=Max(tl
,r
.tl
); br
=Min(br
,r
.br
); }
89 bool contains( const Vec2
& p
) const {
90 return p
.x
>= tl
.x
&& p
.x
<= br
.x
&& p
.y
>= tl
.y
&& p
.y
<= br
.y
;
92 bool contains( const Rect
& p
) const {
93 return contains(p
.tl
) && contains(p
.br
);
95 bool intersects( const Rect
& r
) const {
101 Vec2
centroid() const { return (tl
+br
)/2; }
102 Rect
operator+( const Vec2
& b
) const {
104 r
.tl
+= b
; r
.br
+= b
;