4
|
1
|
|
2 // written in the D programming language
|
|
3
|
|
4 module chipmunkd.cpBB;
|
|
5
|
15
|
6 import chipmunkd.cpVect;
|
|
7 import chipmunkd.chipmunk_types;
|
4
|
8
|
|
9 struct cpBB{
|
|
10 cpFloat l, b, r ,t;
|
|
11 }
|
|
12
|
|
13 static cpBB
|
|
14 cpBBNew(const cpFloat l, const cpFloat b,
|
|
15 const cpFloat r, const cpFloat t)
|
|
16 {
|
|
17 cpBB bb = {l, b, r, t};
|
|
18 return bb;
|
|
19 }
|
|
20
|
|
21 static cpBool
|
|
22 cpBBintersects(const cpBB a, const cpBB b)
|
|
23 {
|
|
24 return (a.l<=b.r && b.l<=a.r && a.b<=b.t && b.b<=a.t);
|
|
25 }
|
|
26
|
|
27 static cpBool
|
|
28 cpBBcontainsBB(const cpBB bb, const cpBB other)
|
|
29 {
|
|
30 return (bb.l < other.l && bb.r > other.r && bb.b < other.b && bb.t > other.t);
|
|
31 }
|
|
32
|
|
33 static cpBool
|
|
34 cpBBcontainsVect(const cpBB bb, const cpVect v)
|
|
35 {
|
|
36 return (bb.l < v.x && bb.r > v.x && bb.b < v.y && bb.t > v.y);
|
|
37 }
|
|
38
|
|
39 static cpBB
|
|
40 cpBBmerge(const cpBB a, const cpBB b){
|
|
41 return cpBBNew(
|
|
42 cpfmin(a.l, b.l),
|
|
43 cpfmin(a.b, b.b),
|
|
44 cpfmax(a.r, b.r),
|
|
45 cpfmax(a.t, b.t)
|
|
46 );
|
|
47 }
|
|
48
|
|
49 static cpBB
|
|
50 cpBBexpand(const cpBB bb, const cpVect v){
|
|
51 return cpBBNew(
|
|
52 cpfmin(bb.l, v.x),
|
|
53 cpfmin(bb.b, v.y),
|
|
54 cpfmax(bb.r, v.x),
|
|
55 cpfmax(bb.t, v.y)
|
|
56 );
|
|
57 }
|
|
58
|
|
59 cpVect
|
|
60 cpBBClampVect(const cpBB bb, const cpVect v)
|
|
61 {
|
|
62 cpFloat x = cpfmin(cpfmax(bb.l, v.x), bb.r);
|
|
63 cpFloat y = cpfmin(cpfmax(bb.b, v.y), bb.t);
|
|
64 return cpv(x, y);
|
|
65 }
|
|
66
|
|
67 cpVect
|
|
68 cpBBWrapVect(const cpBB bb, const cpVect v)
|
|
69 {
|
|
70 cpFloat ix = cpfabs(bb.r - bb.l);
|
|
71 cpFloat modx = cpfmod(v.x - bb.l, ix);
|
|
72 cpFloat x = (modx > 0.0f) ? modx : modx + ix;
|
|
73
|
|
74 cpFloat iy = cpfabs(bb.t - bb.b);
|
|
75 cpFloat mody = cpfmod(v.y - bb.b, iy);
|
|
76 cpFloat y = (mody > 0.0f) ? mody : mody + iy;
|
|
77
|
|
78 return cpv(x + bb.l, y + bb.b);
|
|
79 }
|