Merge branch 'master' of http://skoegl.net/uni/numpty
[numtypysics.git] / Common.h
blobd27affd3254031fd5b04ed577a9fd584bba2199d
1 /*
2 * This file is part of NumptyPhysics
3 * Copyright (C) 2008 Tim Edmonds
4 *
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.
9 *
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.
17 #ifndef COMMON_H
18 #define COMMON_H
20 #include "Box2D.h"
21 #define ARRAY_SIZE(aRR) (sizeof(aRR)/sizeof((aRR)[0]))
22 #define ASSERT(a)
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)
29 struct Vec2 {
30 Vec2() {}
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); }
44 int x,y;
47 template <typename T> inline T Min( T a, T b )
49 return a < b ? a : b;
52 inline Vec2 Min( const Vec2& a, const Vec2& b )
54 Vec2 r;
55 r.x = Min(a.x,b.x);
56 r.y = Min(a.y,b.y);
57 return r;
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 )
67 Vec2 r;
68 r.x = Max(a.x,b.x);
69 r.y = Max(a.y,b.y);
70 return r;
73 #define Sgn(a) ((a)<0?-1:1)
74 #define Abs(a) ((a)<0?-(a):(a))
78 struct Rect {
79 Rect() {}
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 {
96 return r.tl.x <= br.x
97 && r.tl.y <= br.y
98 && r.br.x >= tl.x
99 && r.br.y >= tl.y;
101 Vec2 centroid() const { return (tl+br)/2; }
102 Rect operator+( const Vec2& b ) const {
103 Rect r=*this;
104 r.tl += b; r.br += b;
105 return r;
107 Vec2 tl, br;
111 #endif //COMMON_H